如何在localhost上保存上传的文件?

时间:2011-12-17 13:29:48

标签: php html file-upload localhost

我一直在使用从HTML5 / JS上传图像文件并使用PHP脚本将其保存在localhost(/ var / www /)中,但我无法保存它。当_FILES对象中存在对象时,函数move_uploaded_file始终返回false。我是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";
  }

2 个答案:

答案 0 :(得分:5)

尝试

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

因为您需要指定文件名,而不仅仅是目录。

请参阅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");
    }
}
?>

答案 1 :(得分:0)

试试这个

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