<FORM ENCTYPE="multipart/form-data" ACTION="4_1_action.php" METHOD="POST"> <INPUT NAME="file" TYPE="file"> <INPUT name="ok" TYPE="submit" value="ΠŸΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ Ρ„Π°ΠΉΠ»"> </FORM> <?php if(isset($_FILES["file"])) { $catalog = "image"; if (is_dir($catalog)) { $myfile = $_FILES["file"]["tmp_name"]; $myfile_name = $_FILES["file"]["name"]; if(!copy($myfile, $catalog)) echo "Ошибка ΠΏΡ€ΠΈ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ Ρ„Π°ΠΉΠ»Π° ".$myfile_name; } else mkdir('image'); } ?> 

When you select a file and press the button, it gives an error:

Warning: copy () [function.copy]: you can’t get a directory in Z: \ home \ prakt \ www \ 4_1_action.php on line 9 Error copying file 04.jpg

Tell me what's wrong?

  • Than copy does not fit? - Andrei
  • copy - Copies the source file to a file named dest ....... and you bother it with the second parameter ..... Fill in the end with a slash and a name. - Alexey Shimansky
  • The error ceased to be issued, but there is no copied file in the folder. - Andrei
  • This means that the path to the directory is incorrectly set, /images may be needed, or maybe mysupersite/cawabunga/images ..... depending on the structure of the project and how the routing is configured ..... because this was the only error .... almost ... your else redundant .... you can write like this if (!is_dir($catalog)) mkdir('image'); $myfile = $_FILES["file"]["tmp_name"]; $myfile_name = $_FILES["file"]["name"]; if(!copy($myfile, $catalog.'/'.$myfile_name)) echo "Ошибка ΠΏΡ€ΠΈ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ Ρ„Π°ΠΉΠ»Π° ".$myfile_name; if (!is_dir($catalog)) mkdir('image'); $myfile = $_FILES["file"]["tmp_name"]; $myfile_name = $_FILES["file"]["name"]; if(!copy($myfile, $catalog.'/'.$myfile_name)) echo "Ошибка ΠΏΡ€ΠΈ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ Ρ„Π°ΠΉΠ»Π° ".$myfile_name; where after mkdir('image'); need to press enter - Alexey Shimansky

1 answer 1

Look in the documentation: http://php.net/manual/ru/function.copy.php

copy - Copies the source file to a file named dest .

You are trying to give the second parameter directory.

You need to add a slash and a name to the end: copy($myfile, $catalog.'/'.$myfile_name)


Also note that the else condition is redundant. You can check for the existence of a directory at the beginning, and if it is not created. The rest of the constructions for copying the file leave below.

Eventually. If you imagine that you have a structure like this:

 - index.php (с Ρ„ΠΎΡ€ΠΌΠΎΠΉ) - 4_1_action.php (ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ) - images (ΠΏΠ°ΠΏΠΊΠ° с ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°ΠΌΠΈ, Π² ΠΎΠ΄Π½ΠΎΠΉ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ с 4_1_action.php) - photo_1.jpg - photo_2.jpg ... ... 

then the handler code will be:

 if(isset($_FILES["file"])) { $catalog = "image"; if (!is_dir($catalog)) mkdir('image'); $myfile = $_FILES["file"]["tmp_name"]; $myfile_name = $_FILES["file"]["name"]; if (!copy($myfile, $catalog. '/'.$myfile_name)) echo "Ошибка ΠΏΡ€ΠΈ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ Ρ„Π°ΠΉΠ»Π° ".$myfile_name }