Post by MarcPinnell » Fri Jan 12, 2018 9:18 am

Developing a custom module for a client that allows them to upload a PDF and a book title that can be downloaded by clients that register on the site. Running OC 3.02 and the rest of the site works fine. This module will display the contents of the database of books, I can add a new listing to the database, etc. But I cannot get an upload to work. When I try, the $_FILES is empty. What am I missing? Surely it isn't this hard! :)

The testing code below produces:

Code: Select all

file count=0 array(0) { }
The submit form:

Code: Select all

<form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-freebie">
    <div class="form-group">
    <label for="pdftitle">PDF Title</label>
    <input type="text" class="form-control" id="pdftitle" name="pdftitle" placeholder="Title of PDF Goes Here">
</div>
<div class="form-group">
    <label for="pdftitle">PDF Upload</label>
    <input type="file" id="pdfupload" name="pdfupload">
</div>
<button type="submit" class="btn btn-primary pull-right">Submit</button>
</form>
The processor (in the controller file):

Code: Select all

if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    $this->model_extension_feed_freebies->add($this->request->post);
    $directory = DIR_IMAGE . 'downloads';
    $filename = basename(html_entity_decode($this->request->post['pdftitle'], ENT_QUOTES, 'UTF-8'));

echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
exit();

if (move_uploaded_file($_FILES["pdfupload"]["tmp_name"], $directory . '/' . $filename . '.pdf')) {
    echo "The file has been uploaded.";
    exit();
} else {
    echo "Sorry, there was an error uploading your file.";
    exit();
}
Thanks in advance for any help!

New member

Posts

Joined
Wed Jul 26, 2017 1:12 am

Post by straightlight » Fri Jan 12, 2018 9:52 am

There is no need to re-invent the wheel with new upload codes in Opencart. A good example to follow would be from the admin/controller/tool/upload.php file in the upload() method. :)

The most generated errors being found on Opencart forum originates from contributed programming. The increased counters are caused by posted redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by MarcPinnell » Fri Jan 12, 2018 10:49 pm

straightlight wrote:
Fri Jan 12, 2018 9:52 am
There is no need to re-invent the wheel with new upload codes in Opencart. A good example to follow would be from the admin/controller/tool/upload.php file in the upload() method. :)
Thanks I will check that out.

New member

Posts

Joined
Wed Jul 26, 2017 1:12 am

Post by MarcPinnell » Sat Jan 13, 2018 1:32 am

straightlight wrote:
Fri Jan 12, 2018 9:52 am
There is no need to re-invent the wheel with new upload codes in Opencart. A good example to follow would be from the admin/controller/tool/upload.php file in the upload() method. :)
Ok, so I switched to the method found in the referenced upload.php, but still not getting anywhere.

Getting this error on the code below (line 116 is the echo):
Notice: Undefined index: pdfupload in /home/designs/public_html/math/admin/controller/extension/feed/freebies.php on line 116

Code: Select all

if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    $this->model_extension_feed_freebies->add($this->request->post);
    echo $this->request->files['pdfupload']['tmp_name'];exit();

New member

Posts

Joined
Wed Jul 26, 2017 1:12 am

Post by MarcPinnell » Sat Jan 13, 2018 2:56 am

Thank you @straightlight for setting me straight. After I stopped and took a step back and rethought my process I got it figured it and it is now working. Where I was hung up was I wanted to rename the uploaded file with the contents of the text form field in the form. Finally dawned on me I could use the OC uploader (which is a ajax/json thing that I was trying to avoid because of my need to rename), upload the file, then on form submission, rename it with php!

Thanks again!

New member

Posts

Joined
Wed Jul 26, 2017 1:12 am
Who is online

Users browsing this forum: No registered users and 9 guests