Keeping tmp uploaded file - php

I'm using pluploader with Laravel, and on my administrative the way it works when creating a new article is doing the following steps:
Type the details required to create new article
Select the photos to upload for the article
When Create button is clicked, the following actions are done:
Save the upload details for each photo in a session array. That means that if 3 files are to be uploaded, each upload has an individual POST action
Once the photos have been saved in that session array, it creates the record in the database for the article
It sends the id of the article created to a function which should get the photos and move them to their folder, and add the photo record in the database
Now, the problem is that with each POST the tmp_filename, the temporary file, is deleted so when it tries to actually move the photo... there is no photo to move.
Am I right, and if so, how can I work around that issue? Any way of retaining the tmp deletion until X function has ended?

Am I right,
Yes you are right. PHP deletes the uploaded tempfile after the request finished. This is also clearly documented in the PHP manual:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.
You find that information here: POST method uploadsDocs.
and if so, how can I work around that issue?
Knowing this does suggest that you should keep a copy of or rename the file if you want to keep it.
Any way of retaining the tmp deletion until X function has ended?
Well as written, the deletion will kick in when the request finished. So in PHP normally all functions are executed before the request finishes, so even the X function if you call it in the same request.
If you don't call it in the same request, you need to introduce session management and copy or rename the tempfile before it is automatically deleted. As it is common with any other operation in PHP that should be done over multiple requests. See SessionsDocs.
See as well:
php - multi-step form with file upload

The easiest thing you can do is simply move the file somewhere else after it is uploaded.
However, I suggest you do this instead:
When a photo is uploaded, create the photo record right away and send back a record id to the browser.
Have the browser insert an input field with a reference to the photo id (e.g., <input type="hidden" name="photos[]" value="1234">.
When the document form is saved, associate the document with those photo ids.
This way you don't even need a session.

Related

Html/PHP File Uploader in step 1, but uploading in last step

so basically I have a multistep form with X steps.
In the first step, I offer a file upload form to the user.
This just behaves like a normal HTML file uploader:
User selects file
File Box, shows pathname
If the user clicks on "next" the file should not be uploaded.
If the user reaches the last step of the form, the files he/she has choosen in step-1 should be uploaded now.
I have no idea if this is actually possible. There are also a couple of problems
The value from input type="file" gives something like C:\fakepath\filename.ext
Saving the above in a session for later upload does not make sense.
How can I get the uploader to work in the last step?
Yes, this is actually possible, and saving data between pages make sense, otherwise, how could you remember what he sets in the file input?
If it is a simple HTML page, I assume your are using PHP. Register files in a temporary variable or session, put it in a hidden input in the HTML code if you don't use session.
At the last step, set your files and upload it.
Good luck.

Uploading image with form to related table

I can get my head around the best way to sort this problem.
I have a form that creates a new record in the database (using php and MySQL) this is all fine, but as part of this form I want the user to be able to upload some files. These files would be stored in another table within the DB (allowing multiple entries) and linking back to the original record.
I could upload the images on the next page, after the main record has been created, but really want this nice and simple, all on one form.
Another issue is that if the images are loaded into a temp place, before form submission, I don't want this images staying there forever if the form is left, or cancelled.
I am using Fine uploader to handle the file management, and php to save and place.
Why don't you use a temporary table / directory with a peridodic check-and-delete-old script?
Create a temp table with (id, identifier_key, data, timestamp), and delete old records from it periodiaccly using a cron script (or from your main web script, using a probability, like php's default session handler works - once in a 1000 request or something similar.)

File upload over multiple pages

I'm creating a php form that works in this way:
1) The user fills the form and add a file to upload;
2) The submitted information is shown to the user for confirmation;
3) Upon confirmation, a email is sent with all the data, including the file.
My problem is that I get access to the uploaded file on the second step, but not on the third step. Due to security reasons, it's not possible to resend the file on the second step by creating a input file field with a default value (the uploaded file).
It's possible to achieve what I'm trying to do, without having to copy the uploaded file to another folder?
The solution to my question was to place the actual file upload only on confirmation page. There was no way to "carry" the file upload from a page to another in the way I wanted to.
Thanks for the replies.

Showing Users Previous Uploads to Choose from

Not really sure where to start on this one and a day of google searches hasn't helped much.
I am working on a submission form where my logged in users can submit data in form fields and attach a number of files. These files should be available to the users across many submissions so as not to clog up my database with duplicate files and for user convenience not having to reupload files many times. The files will be mostly PDFs and word docs.
My question is, what is the most elegant solution to letting my user select a previously submitted file for their current submission and to "attach" or associate that file with multiple submissions? Some drop down box that shows previous uploads seems best and if you agree any code snippets would be super helpful.
I've decided to store the document files as blobs in their own table. I'm running php and MySQL on Linux with godaddy.
Thanks for advice on where to look for code samples in advance. Just haven't been able to find what I'm looking for. Is JavaScript necessary here?
A better way to handle file uploads is to simply store the file-name (which should be generated as unique) in the database and store the actual file in a directory. That way, you won't have this issue of storing it in a BLOB. Is there any reason you are doing that over this method?
Now, if you output a SELECT form element with a list of filenames stored in the database, a user can simply select one and all you need to do is store that filename in the new row. Then, all rows that point to that file name will point to the same file, with no need to upload it.
If you want to allow multiple file uploads or "attachments" in this case, then you are going to need to create a one-to-many relationship in the database. Each submission row will have many rows in another table that point to it, all referencing the corresponding files uploaded. Such that any submission can have zero, 1, 2, or more files attached. If they select files that already exist, great, just copy the username as stated above. IF its a new file, then you should upload that file and add its name to the database.
Javascript isn't required but it would be nice - to add interactivity to your form. If you want to allow for a user to click say, "Add New File", and have a new file upload form element appear.
Start with a a form. Add a select with the list of files associated with that user. Add another form element that allows for file upload. Have all of your other stuff as normal.
On page view, populate the select with the filenames and put the row IDs for that file in the <option value="file_id_xxx">....
In your server script, detect if a file was uploaded; if so, complete the process of storing the file and set the file reference as the associated file for the submission; if not, check that a file was selected from the dropdown and if so, save the reference from the select with previous uploaded files with the submission instead.
Javascript may help in disabling the select or file upload input, depending. But it's not necessary.

Proper way to handle uploads using AJAX form

I'm using Valum's AJAX uploader, which is quite nice. I have a form that lets you fill out some info and optionally attach files.
I have a hidden input on the form that has a randomly generated "token" (5 character alnum). The uploads are sent to a tmp folder and the info about those files (name, dir, token) are kept in a uploads_tmp table in a database.
Then, when the user successfully submits the form, those files are moved to a more permanent location and the rows from the uploads_tmp are moved to the uploads table.
If the user submits and there are errors with the form, my script knows there are uploads from a previous attempt via the token. So there's no need to re-upload files.
Is this the right way of doing it or am I going about it all wrong? I'm using PHP (CodeIgniter to be exact).
There isn't any one correct way of doing something like this. Your method seems like a good intuitive one, but really it's down to whatever works for you and your situation.

Categories