【问题标题】:Not able to move file into folder in php无法将文件移动到php中的文件夹中
【发布时间】:2014-03-02 00:11:48
【问题描述】:

我无法将文件移动到所需的文件夹中。我想将图像保存到上传的文件夹中。在 mysql 中,我有 blob 类型。 这是我的代码

$target_Path = "uploaded/";
    $target_Path = $target_Path.basename( $_FILES['image']['name'] );
    move_uploaded_file( $_FILES['image']['tmp_name'], $target_Path );

    mysqli_query($con,"UPDATE info SET photo='$target_Path' WHERE user_id='$id'");

在 mysql 中,它显示某些内容已保存但未打开,并且文件未移动到上传的文件夹中。我在我的本地主机中这样做。请帮忙。

【问题讨论】:

  • 您收到的错误消息并尝试传递文件夹的完整路径
  • 您的代码对 SQL 注入开放。请使用准备好的语句和绑定变量,或者在将变量粘贴到查询中之前至少在变量上使用mysqli_real_escape_string

标签: php mysql file upload mysqli


【解决方案1】:

尝试使用 IF 语句包装 move_uploaded_file,如果文件传输失败则抛出异常:

if (move_uploaded_file($_FILES['image']['tmp_name'], $target_Path ) === false) {
    throw new Exception([text]);
}

或者,要测试目标是否可写,您可以尝试在那里打开文件并写入:

if (($fp = fopen($target_path, 'w')) === false) {
    thrown new Exception("Failed to open file $target_path for writing");
}
fwrite($fp, file_get_contents($_FILES['image']['tmp_name']));
fclose($fp);

问题可能是文件权限或所有权之一,您需要在命令行中使用 chmod 或 chown 进行修复。

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 2014-06-12
    • 1970-01-01
    • 2015-08-06
    • 2016-10-11
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多