【问题标题】:PHP image upload - several errorsPHP图像上传 - 几个错误
【发布时间】:2021-07-25 08:08:55
【问题描述】:

所以,我正在尝试使用 move_uploaded_file() 上传图像。代码如下:

HTML 表单:

        <form method="post" action="?page=prod&action=register" enctype="multipart/form-data">
            <fieldset class="field-prods">
                <label>Name:</label> <br>
                <input type="text" name="txtName">
            </fieldset>

            <fieldset class="field-prods">
                <label>Price:</label> <br>
                <input type="number" name="nmbPrice">
            </fieldset>

            <fieldset class="field-prods">
                <label>Image:</label> <br>
                <input type="file" name="fileImage">
            </fieldset>

            <button type="submit" class="btn-prods">Register</button>
        </form>

PHP 脚本:

if ($_POST != null) {
            if (!empty($_POST['txtName']) && !empty($_FILES['fileImage']) && !empty($_POST['nmbPrice'])) {
                Product::insert($_POST['txtName'], $_FILES['fileImage'], $_POST['nmbPrice']);

                $target = "img/prods/" . $_FILES['fileImage']['name'];
                $fileTmpName = $_FILES['fileImage']['tmp_name'];
                move_uploaded_file($fileTmpName, $target);
    }
}

但我收到了很多警告:

*Warning: Array to string conversion*
*Warning: move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: No such file or directory*
*Warning: move_uploaded_file(): Unable to move "F:\Programs\Xampp\tmp\php7CE5.tmp" to "img/prods/livraria-print1.jpg"*

有人可以帮我吗?

【问题讨论】:

  • 那些是 warnings ,这与 error ... 不同
  • 除此之外:信息非常清楚,那么您的实际问题是什么?
  • @arkascha 图像不会进入文件夹...我需要一些帮助
  • 我们理解这一点,因为那是你写的。但是你得到的信息在这里很清楚:临时文件或命运文件夹不存在。这是我们无法改变的。你需要检查并修复它。

标签: php image file-upload


【解决方案1】:

#数组到字符串的转换

此警告在 insert 函数中:您传递了数组 ($_FILES['fileImage']) 而不是字符串。

好像要保存图片的地址,如果是这样的话,最好先保存图片,然后尝试保存图片的地址。

# move_uploaded_file(img/prods/livraria-print1.jpg):打开流失败:...

这个警告很清楚。检查您的目录并确保您使用的路径正确。

# move_uploaded_file(): 无法移动

这是因为前一个警告。

【讨论】:

  • 非常感谢我的朋友。还没有想过插入功能……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2015-01-07
  • 1970-01-01
  • 2016-03-08
  • 2015-11-02
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多