3

I'm trying to save Files on my Server. It works perfect with small files but not with large ones.

In JavaScript I'm sending the files with a XMLHttpRequest as follows

var ajax = new XMLHttpRequest();
ajax.open('POST', configuration.backend + 'ajax/uploadVideo', true);
ajax.setRequestHeader('X-File-Name', input.files[0].name);
ajax.setRequestHeader('X-File-Size', input.files[0].size);
ajax.send(input.files[0]);

in PHP this script saves the input to the harddrive:

$filename = $_SERVER['HTTP_X_FILE_NAME'];
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$new_filename = uniqid().'.'.$extension; 
$fh = fopen(DOCROOT.'/public/videos/'.$new_filename, 'w');
fwrite($fh, file_get_contents('php://input'));
fclose($fh);

The script works perfectly with smaller files, but not with larger ones. It just creates a file with 0 Bytes. (8.7MB ist not working while 5.7MB works). I don't get any PHP errors. Error reporting level is E_ALL & ~E_DEPRECATED.

I've also tried to increase the values of post_max_size, upload_max_filesize and memory_limit, but no change made any effect.

  • 1
    Forgive me for asking such an obvious question, but did you restart apache after you changed your php.ini? – Dan H Jan 22 '14 at 00:10
  • 1
    I don't see any `file_get_contents`, but I see a `file_put_contents`. The `put` is the issue, yes? – Ohgodwhy Jan 22 '14 at 00:11
  • Well, he's uploading, not downloading I think... – Dan H Jan 22 '14 at 00:11
  • @DanH Yes, but the question title is wrong then. Seems OP is using `fopen('php://input')` instead of `file_get_contents('php://input')` – Phil Jan 22 '14 at 00:13
  • @DanH I've changed the settings with `ini_set()` – Andreas Schrottenbaum Jan 22 '14 at 00:13
  • Sorry for the inconvenience. I've updated my script. Now it's with `file_get_contents()` (but still not working ;)) – Andreas Schrottenbaum Jan 22 '14 at 00:16
  • 1
    I'm thinking upload_max_filesize cannot be changed with ini_set(). – Dan H Jan 22 '14 at 00:18
  • 2
    Quick question, how would you know you have any errors when the script is fired from ajax but I don't see anything that handles the error responding? have you checked the `network` tab in your `inspector` to see what `response code` is returned? And to answer the above, just `echo ini_get('upload_max_filesize');` – Ohgodwhy Jan 22 '14 at 00:18
  • Also many shared hosting environments require you to instantiate the `suPHP` directive in your `.htaccess` file before you can make any `ini` adjustments, especially if not in the root directory of the public web folder. – Ohgodwhy Jan 22 '14 at 00:21
  • You can't set `upload_max_filesize` with ini_set unless you're using <= PHP 4.23. See [List of php.ini directives](http://www.php.net/manual/en/ini.list.php) – itsmejodie Jan 22 '14 at 00:23
  • @Ohgodwhy Wrong way around. With suPHP / suEXEC, you need a [custom `php.ini` file in the appropriate directory](http://blog.philipbrown.id.au/2009/08/php-suexec-and-custom-php-ini-files/). Without it, you can generally use `php_value` in a `.htaccess` file – Phil Jan 22 '14 at 00:24
  • @Phil what are you talking about? with suPHP each directory that needs to make ini adjustments need to have their own ini file. – Ohgodwhy Jan 22 '14 at 00:27
  • @Ohgodwhy err, that's what I just said. You mentioned suPHP and `.htaccess` which is incompatible unless you're talking about setting the `PHPRC` environment variable – Phil Jan 22 '14 at 00:28
  • @Phil No...I said that you need to put the `suPHP` directive in the `.htaccess` – Ohgodwhy Jan 22 '14 at 00:29
  • @Ohgodwhy Sorry, I assumed you were talking about setting `php_value` / `php_flag` params in `.htaccess` – Phil Jan 22 '14 at 00:30
  • You were right. `init_set('upload_max_filesize')` didn't take any effect. After increasing the limit in the php.ini and restarting the server, the error still exists.Response status from the server is 200 OK @Ohgodwhy – Andreas Schrottenbaum Jan 22 '14 at 00:32
  • If it's not taking effect in either directory, contact your host about how to enable `suPHP` so those `.ini` settings work. – Ohgodwhy Jan 22 '14 at 00:33
  • 2
    @Ohgodwhy Sounds like OP is using their own server – Phil Jan 22 '14 at 00:34
  • You're right. It's on my local machine. What confuses me most, is the fact, that no error is thrown and the file is created with 0 Bytes. – Andreas Schrottenbaum Jan 22 '14 at 00:35

2 Answers2

1

Just found the error. It was post_max_size to change in the php.ini (not just with ini_set()), not the upload_max_filesize.

-1

After some quick looking, check this other Stack answer out:

Changing upload_max_filesize on PHP

I believe you need to adjust your upload sizes in php.ini and restart apache, ini_set() cannot be used to change all PHP settings.

Community
  • 1
  • 1
Dan H
  • 606
  • 4
  • 6
  • Correct, `upload_max_filesize` is `PHP_INI_PERDIR` changeable but linking to another SO question / answer as your answer is not cool. Mark this question as a duplicate instead – Phil Jan 22 '14 at 00:21
  • Sorry, I'm new to this site, I didn't know I could do that. My bad. – Dan H Jan 22 '14 at 17:19