Help me figure out the question / problem here:

the file that is uploaded to the server does not appear in the webroot/uploads/ folder, although the path is registered, the transfer function itself does not produce any errors ...

the oddity is that the file $full_path appears in the webroot folder, with a set of just numbers ...

the path itself in the database is saved correctly (in the form: webroot/uploads/1496868491.png )

decided to look var_dump($_FILES['img_file']) ;

 array(5) { ["name"]=> string(6) "t1.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(50) "C:\openserver\OpenServer\userdata\temp\php5875.tmp" ["error"]=> int(0) ["size"]=> int(2387) } 

the code itself.

 path = 'webroot/uploads/'; // директория для загрузки $ext = array_pop(explode('.',$_FILES['img_file']['name'])); // расширение $new_name = time().'.'.$ext; // новое имя с расширением $full_path = $path.$new_name; // полный путь с новым именем и расширением echo "<pre>"; echo var_dump($_FILES['img_file']); echo "</pre>"; if($_FILES['img_file']['error'] == 0){ if(move_uploaded_file(($_FILES['img_file']['tmp_name']), '$full_path')){,,,,} 

In search of a solution, I stumbled upon the council to check the folder for writing.

 if (!is_writable($path)) { die("Запись в каталог запрещена"); } else {if(move_uploaded_file(....)){...} 

So, the check issued that $ path is write protected, who knows how to give permission to write? (the question is what can be done through cmd, or using php)

  • You can change the permissions of php with the chmod function, but the result of the operation will depend on whether the user from whom the nginx / apache process is running has sufficient permissions. But, this is bad practice. The upload folder must exist and have write permissions initially. - Vitaly

3 answers 3

Following the advice (B. Bohdan) added to the path variable the following $_SERVER["DOCUMENT_ROOT"].

it turned out

 $path = $_SERVER['DOCUMENT_ROOT']."/webroot/uploads/"; 

! update! after such manipulations when calling the address of the picture from the database, the picture itself did not pull up from the directory because the path was as follows

C: /openserver/OpenServer/domains/localhost/webroot/uploads/1496908169.png

accordingly, it did not satisfy the ultimate goal.

after editing received the following code.

 $path = "/webroot/uploads/"; // директория для загрузки $ext = array_pop(explode('.',$_FILES['img_file']['name'])); // расширение $new_name = time().'.'.$ext; // новое имя с расширением $full_path = $path.$new_name; // полный путь с новым именем и расширением /*echo "<pre>"; echo var_dump($path); echo "</pre>";*/ if($_FILES['img_file']['error'] == 0){ if(move_uploaded_file($_FILES['img_file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].$full_path)){ // Если файл успешно загружен, то вносим в БД // Можно сохранить $full_path (полный путь) или просто имя файла - $new_name 

In my case, everything worked, and now the images fall into the folder (C: /openserver/OpenServer/domains/localhost/webroot/uploads/1496908169.png), and the path to them is written to the database in this form (/ webroot / uploads / 1496909914 .png) =)

    The operation of this superglobal variable depends on the correctness of the server settings. Its use is recommended by the standard.

    Why should '$ full_path' in the last line be wrapped in single quotes? I do this:

     //куда загружать? $dir = $_SERVER["DOCUMENT_ROOT"] . "/webroot/uploads"; //имя файла (с расширением) $name = basename($_FILES["img_file"]["name"]); //расширение файла $ext = array_pop(explode(".", $name)); //новое имя (с расширением) $new_name = time() . "." . $ext; //постоянное место хранения файла $file_dir = $dir . DIRECTORY_SEPARATOR . $new_name; //временное место хранения файла $tmp_name = $_FILES["img_file"]["tmp_name"]; //переместить файл в постоянное место хранения if (is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name, $file_dir)) { echo "Файл успешно загружен по такому адресу: " . $file_dir; } else { echo "Не удалось загрузить файл по такому адресу: " . $file_dir; } 

    And even better - refer to the documentation, everything is described in detail there: php.net

    • if you remove the quotes in '$ full_path', you will get 2 errors ... Warning: move_uploaded_file (webroot / uploads / 1496902947.png): C: \ openserver \ OpenServer \ cms \ localhost \ models \ seller.php on line 33 Warning: move_uploaded_file (): Unable to move 'C: \ openserver \ OpenServer \ userdata \ temp \ phpEC51.tmp' to 'webroot / uploads / 1496902947.png' in C: \ openserver \ OpenServer \ domains \ localhost \ models \ seller.php on line 33 - keks24
    • The path is best formed using $ _SERVER ["DOCUMENT_ROOT"]. In extreme cases, you can use dirname ( FILE ) (with two underscores at the beginning and end of FILE). Have you tried my version? Or do you need just such a code structure as in your version? - B. Bohdan
    • Your option has not yet been tried ... - keks24