5

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?

Alex
  • 7,538
  • 23
  • 84
  • 152

2 Answers2

4

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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
2

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.

Francis Avila
  • 31,233
  • 6
  • 58
  • 96
  • Yea, I decided to move the photo to a temp dir and then move it from there to its final destination – Alex Dec 31 '12 at 17:37