3

I have following memory configuration with writable file name-

upload_max_filesize = 256M;
post_max_size = 256M;
memory_limit = 256M;

and form enctype="multi-part/form-data", permission to the folder is 777(tried with 755 and 644 to). Unfortunately could not figure out actual error -

if (move_uploaded_file($_FILES['async-upload']['tmp_name'], __DIR__.'/wp-content/uploads/'.$_FILES['async-upload']['name'])) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    // this always returns 0
    echo $_FILES['async-upload']["error"];
}

What could be possible reason for upload failure and what i am missing here?

same code is working in another project?

Veshraj Joshi
  • 3,544
  • 3
  • 27
  • 45
  • Have you activated error reporting and/or checked your error log? – deceze Mar 02 '18 at 09:01
  • yes error_reporting(E_ALL); – Veshraj Joshi Mar 02 '18 at 09:02
  • 1
    And you *have* checked your error log? – deceze Mar 02 '18 at 09:06
  • Are you checking `$_FILES['async-upload']['error']` anywhere in your code prior to the code you show us? – RiggsFolly Mar 02 '18 at 09:10
  • Try checking the access mode of all parent directories leading to the one, where you are about to store the uploaded file, for example with `namei` – helvete Mar 02 '18 at 09:11
  • I checked it for whether file is uploaded or not by php and make sure it's the only problem of not moving. @RiggsFolly – Veshraj Joshi Mar 02 '18 at 09:20
  • Then look at your error log as was suggested earlier. If you dont understand the errors, add then to your question – RiggsFolly Mar 02 '18 at 09:21
  • 1
    Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Mar 02 '18 at 09:22
  • No error and warning after prepending `error_reporting(E_ALL); ini_set('display_errors', 1);` – Veshraj Joshi Mar 02 '18 at 09:28
  • Nothing in error log @deceze – Veshraj Joshi Mar 02 '18 at 09:28
  • What's the path of your script's file, ie. is `__DIR__` what you expect it to be? – Joe Mar 02 '18 at 09:39
  • oh it's working directory.. @Joe – Veshraj Joshi Mar 02 '18 at 09:47
  • [`__DIR__`](http://php.net/manual/en/language.constants.predefined.php) is the directory of the refering file. So if your upload script lays in the wordpress root directory, it makes sense. Try echoing `__DIR__ . '/wp-content/uploads/'` to make sure. Anyway you might want to use `wp_upload_dir()['basedir'] . $_FILES['async-upload']['name']` instead. – Joe Mar 02 '18 at 09:54

1 Answers1

0

Add an M to the numbers like this to signify Megabyte

upload_max_filesize = 256M;
post_max_size = 256M;
memory_limit = 256M;

And dont forget to restart your webserver after making the changes.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149