Page 1 of 1

File upload problem: unable to move with move_uploaded_file

Posted: Mon May 14, 2007 5:08 am
by sanbikinoraion
Hello,

I am trying to upload files to a linux/apache/php box. My upload script works intermittently, failing more often than succeeding. The file upload process seems to go fine, with the error code in $_FILES reporting '0', and the filesize listed as correct.

Calling is_uploaded_file on the temp name of the file reports true. However, if I try moving the file using move_uploaded_file to the target directory, move_uploaded_file often issues the warning "unable to move xxx to yyy".

The directory permissions for both the temp directory and the target directory are set to 777, PHP's safe mode is off, and I believe the directories are both chowned to the apache/php user (and no, I'm not telling you where this box is!!).

The file does not seem to appear the temporary directory after move_uploaded_file has failed, which I would have expected it to have since move_uploaded_file claims to do nothing on failure. But I don't know enough about linux to know if the temp file would remain after the PHP upload script terminates.

So, any light y'all could shed on this would be greatly appreciated.

Cheers!

Posted: Mon May 14, 2007 5:24 am
by CoderGoblin
Without giving us more information it is difficult to really help. Your code could help us and just uploading information...

Saying that a couple of things spring to mind...
file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size and max_input_time are all configuration settings which may affect things.

Another thing to try is to stop just before move_uploaded file after you have the temp file. Log in as the webserver user and try to move the file yourself.

Posted: Mon May 14, 2007 5:42 am
by sanbikinoraion
Cheers.

I believe that my config parms are in order, and I should have mentioned this; sorry.

To be clear:
file_uploads = on
upload_tmp_dir = /<appname>/data/doctemp
post_max_size = 8M
upload_max_filesize = 4M
max_input_time = 60
memory_limit = 64M

Just realized, actually, in what could be a related bug I've set php's error logging on and to log to /<appname>/data/log/php.log , but nothing has ever appeared in that file. Since that's also related to file IO, I thought I'd mention it in case someone has seen similar before.

I'm uploading files of no more than 500k, and my test documents are <100k, so I don't think timing out is an issue.

Anyway, I'm fairly sure that my code is sound, since it does actually work intermittently, which is why I didn't post it in the first place. Here is the relevant snippet:

Code: Select all

$id = $this->getId();
$extension = $this->getExtension();
$document_link = self::DOCUMENT_DIRECTORY . "doc{$id}.$extension";
$tmp_file_name = $_FILES["{$this->file_name_field}"]["tmp_name"];
$moved = move_uploaded_file($tmp_file_name, $document_link);
if ($moved != TRUE)
  {
  Debug::log("Document issue failed to upload document {$this->file_name_field}");
  }
Obviously this is happening in a class method. (DOCUMENT_DIRECTORY is set to "<appname>/data/documents")

Posted: Mon May 14, 2007 9:17 am
by sanbikinoraion
I've discovered two new things:

1. My PHP logs are getting dumped in the Apache logs, so that's where they've gone.

2. The error message that move_uploaded_file issues to the apache logs is "failed to open stream: Permission denied". There is no uploaded file to be had in the temporary upload directory.

Could it be some kind of Apache configuration error that's denying access to certain directories? How would I know?