【问题标题】:move_uploaded_file - failed to open stream - no such file or directorymove_uploaded_file - 无法打开流 - 没有这样的文件或目录
【发布时间】:2020-10-18 00:58:21
【问题描述】:

这是我的表单代码:

<form method='POST' enctype="multipart/form-data">
   <input type='file' name='newPicture'>
   <button type='submit' name='submit' id='uploadPicBtn'>Upload Photo</button>
</form>

这是我的 php 代码:

<?php
    if (isset($_POST['submit'])) {
       if (isset($_FILES['newPicture'])) {
            $file = $_FILES['newPicture'];
            $serverpath = "images/".$file['name'];

            move_uploaded_file($file['tmp_name'], $serverpath);
       }
    }
?>

它们都在同一个 .php 文件 (index.php) 中。

我收到此错误: 警告:move_uploaded_file(images/anime.jpg):打开流失败:第 7 行的 C:\xampp\htdocs\account-settings\index.php 中没有这样的文件或目录

警告:move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpB4E0.tmp' to 'images/anime.jpg' in C:\xampp\htdocs\account-settings\index.php on line 7

怎么了?

【问题讨论】:

  • 你测试过指定的目录确实存在吗?

标签: php html forms file-upload


【解决方案1】:

用途:

$parent = dirname(__DIR__);
$serverpath = $parent."\\images\\".$file['name'];

代替:

$serverpath = "images/".$file['name'];

因为它会在与 php 脚本相同的文件夹中搜索文件。 $parent 存储:C:\xampp\htdocs\(站点的根目录)

【讨论】:

    猜你喜欢
    • 2013-01-13
    • 2016-04-05
    • 2017-05-04
    • 1970-01-01
    • 2015-07-29
    • 2015-11-13
    相关资源
    最近更新 更多