How to save the downloaded file to localhost?

I used to download an image file from HTML5 / JS and using a PHP script to save it to localhost (/ var / www /), but I cannot save it. the move_uploaded_file function always returns false as long as the object exists in the _FILES object. I am new to php:

$target = "upload/"; if(!empty($_FILES)) { if(move_uploaded_file($_FILES['image_file']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['image_file']['name']). " has been uploaded..."; } else { echo "Sorry, there was a problem uploading your file. {$error}"; } } else { echo "_files is empty"; } 
+4
source share
2 answers

Try

 move_uploaded_file($_FILES['image_file']['tmp_name'], $target.$_FILES['image_file']['name']) 

Because you need to specify a file name, not just a directory.

See an example from the PHP documentation

 <?php $uploads_dir = '/uploads'; foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "$uploads_dir/$name"); } } ?> 
+5
source

try it

 if(is_uploaded_file($_FILES['image_file']['tmp_name'])) { move_uploaded_file($_FILES['image_file']['tmp_name'], $target.$_FILES['image_file']['name']); echo "The file ". basename( $_FILES['image_file']['name']). " has been uploaded..."; } 
0
source

Source: https://habr.com/ru/post/1386825/


All Articles