douwen1915 2011-12-17 13:29
浏览 356
已采纳

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

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";
  }
  • 写回答

2条回答 默认 最新

  • drwkqwa82140 2011-12-17 13:33
    关注

    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");
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波