如何将上传的文件保存在本地主机上?

发布于 2024-12-21 19:53:31 字数 526 浏览 0 评论 0原文

我一直使用从 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";
  }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2024-12-28 19:53:31

尝试一下

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

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

请参阅 PHP 文档 中的示例

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

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");
    }
}
?>
极度宠爱 2024-12-28 19:53:31

试试这个

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

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...";
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文