How to save an uploaded file on localhost?

孤街浪徒 提交于 2019-12-01 19:27:03

问题


I have been using to upload an image file from HTML5/JS and using PHP script to save it in localhost(/var/www/) but i cant save it. the function move_uploaded_file always returns false while there is exists an object in _FILES object. I am a newbie in 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";
  }

回答1:


Try

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

Because you need to specify the name of file, not only the directory.

See example from 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");
    }
}
?>



回答2:


try this

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...";
     }


来源:https://stackoverflow.com/questions/8545068/how-to-save-an-uploaded-file-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!