php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42550 Uploaded files empty UNTIL PHP EXITS?
Submitted: 2007-09-04 20:50 UTC Modified: 2007-09-12 01:00 UTC
From: tnp at shaman dot co dot uk Assigned:
Status: No Feedback Package: Filesystem function related
PHP Version: 5.2.4 OS: Debian Etch
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
18 - 5 = ?
Subscribe to this entry?

 
 [2007-09-04 20:50 UTC] tnp at shaman dot co dot uk
Description:
------------
Scenario:
Trying to move potentially large files into MySQL database using the 

'Insert into....content=LOAD_FILE('$tmpnam')' 

command where 

$tmpname=$_FILES['tmp_name'];

gave an empty content.

using the command 
move_uploaded_file($tmpname,"tmp/foo")

and Load_File('/tmp/foo') 

also failed..badly. the browser hung..

using
copy($tmpname,'/tmp/foo');
insert..content=LOAD_FILE('/tmp/foo') 
WORKED!!

I suspect the new files do NOT exist (properly) for a third party program like Mysqld to find..until PHP exits and flushes its buffers.

However it is not possible to delay calling Mysql until after the script exits.

What would be nice is a sync() call that would force all open file handles to be updated to the OS.

I have added the workaround note to the move_uploaded_file() docs.

I am happy to test any proposed solutions - at least for the next few days while I am working on this. The email is VALID
 



Reproduce code:
---------------
// this code sits inside a loop which sets $index to correspond to one of many possible file uploads

$filename=$_FILES[$index]["name"]; //orig filename
$filesize= $_FILES[$index]["size"];     // the size in bytes of the uploaded file
$tmpname=$_FILES[$index]["tmp_name"]; // the name of the temporary copy of the file stored on the server
 // If we have a new file, then we archive the old one and insert the new
if ($filename != "" && $filesize != 0) // we have a new file
    {
//    copy($tmpname,"/tmp/foo"); workaround
   $query="update project_files set current='no' where id='".$file_id."'";
    mysql_query($query); // Poof! Gone!	
    $query=sprintf("insert into project_files set project_id='%s',current='yes',date='%s' ,user='%d', size='%d', description='%s', name='%s', content=LOAD_FILE('%s')", 
    	$project_id,
    	date('Y-m-d'),
    	$employee_id,
         $filesize,
         $filedescription,
         $filename,
  //     "/tmp/foo");//hack but works
         $tmpname); // doesn't work. Empty content
          mysql_query($query);
         }




Expected result:
----------------
I would have hoped that the uploaded file would have ended up in project_files.content. It was in fact null unless I used the copy() command to create a DUPLICATE of $tmpname..

move_uploaded_file() was even worse.It behaved as though the file wasn't even there at all, rather than zero length.

I COULD have added the stream directly to SQL with a content=addslashes() from the file, but the thought of adding slashes to e.g. 10Mbyte bitmap, and having Mysql API remove them was too ghastly to contemplate.



Actual result:
--------------
 project_files.content=0;
!! :-)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-04 22:26 UTC] jani@php.net
Can you please provide a short and self-contained script that we can just copy'n'paste and run to see what the actual problem is?
From first glance this seems like you just haven't really understood how the file uploading works.
 [2007-09-12 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 16:01:26 2019 UTC