Search code examples
phpjqueryajaxfile-permissionsajaxuploader

Real Ajax Uploader Error: Cannot write on file


I'm building a site that requires the ability to upload large .wav files. I figured the best way to upload these files was via a jQuery plugin that could chunk the data to the server. I chose Real Ajax Uploader to do this.

I put the following together and it works great for smaller files:

$('.demo').ajaxupload({
    url : '/upload.php',
    remotePath: '/remote/path/',
    maxFiles: 1,
    maxFileSize: '250M',
 });

The problem occurs when I have a larger file that involves "chunking". For some reason the script keeps giving me this error:

Cannot write on file.

I've traced it down to this location in the upload.php file:

...
//start of the file upload, first chunk
if ($currByte == 0) {
    $tempFile           = tempnam($this->tempPath, 'axupload');
    $this->tempFileName = basename($tempFile);
}

// some rare times (on very very fast connection), file_put_contents will be unable to write on the file,
// so we try until it writes for a max of 5 times
$try = 5;
while (file_put_contents($tempFile, $fileChunk, FILE_APPEND) === false && $try > 0) {
   usleep(50);
   $try--;
}

//if the above fails then user cannot write file due to permission or other problems
if (!$try) {
   $this->message(-1, 'Cannot write on file.');
}
...

I assumed this meant that $tempFile was not writable, so I added this bit:

...
//start of the file upload, first chunk
if ($currByte == 0) {
    $tempFile           = tempnam($this->tempPath, 'axupload');
    $this->tempFileName = basename($tempFile);
 }

 chmod($tempFile, 0755);
 ...

I tried it again and I still had a problem, so I changed the permissions of /tmp to 755, thinking maybe that was the case. Again, nothing.

I could try to just change the location of my /tmp directory and have a more writable environment, but if there's an easier solution I'd rather go with that.

Thank you.


Solution

  • First of all upload a small file in a directory and check whether it uploads successfully or not. If successful, than search for the php.ini setting file and check the following:

    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    php_value max_input_time 300
    php_value max_execution_time 300
    

    update all the above settings to meet your requirement and after that restart the server and then try again.