【问题标题】:Why isn't move_upload_file is always returning false in the given code?为什么 move_upload_file 在给定代码中不总是返回 false ?
【发布时间】:2017-07-08 07:24:41
【问题描述】:

我正在尝试使用 php 和 mysql 上传图片,下面是使用的代码..

index.php

<form action="submit.php" enctype="multipart/form-data" method="post">

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>

</tr>

<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>


</tbody></table>

</form>

提交.php

<?php

include("mysqlconnect.php");

function GetImageExtension($imagetype) {
    if (empty($imagetype))
        return false;
    switch ($imagetype) {
        case 'image/bmp': return '.bmp';
        case 'image/gif': return '.gif';
        case 'image/jpeg': return '.jpg';
        case 'image/png': return '.png';
        default: return false;
    }
}

if (!empty($_FILES["uploadedimage"]["name"])) {

    $file_name = $_FILES["uploadedimage"]["name"];
    $temp_name = $_FILES["uploadedimage"]["tmp_name"];
    $imgtype = $_FILES["uploadedimage"]["type"];
    $ext = GetImageExtension($imgtype);
    $imagename = date("d-m-Y") . "-" . time() . $ext;
    $target_path = "images/" . $imagename;



    if (move_uploaded_file($_FILES['uploadedimage']['tmp_name'], $target_path)) {
        $detail = date("Y-m-d");
        $sql = "INSERT INTO `image_upload`(`id`, `image`, `detail`) VALUES (NULL,'$target_path','$detail')";
        if (!$conn->query($sql)) {
            echo $conn->error;
        } else {
            echo "Successfully inserted. ";
        }
    } else {

        exit("Error While uploading image on the server");
    }
}
?>

表结构:

#   Name        Type    Collation     Attributes    Null Default  Extra
1   id(Primary) int(11)                              No   None    AUTO_INCREMENT
2   image       blob                                 Yes  NULL      
3   detail     varchar(500) utf8_general_ci          Yes  NULL

每次执行此操作时,它总是显示“在服务器上上传图像时出错”,我不明白为什么。有人可以让我知道我哪里出错了,我该如何改进我的实施? 提前致谢。

【问题讨论】:

  • 检查你的目录是否存在以及是否有写权限
  • 已经给了目录权限,但它仍然没有执行。

标签: php mysql


【解决方案1】:

在移动文件之前添加这一行。

if (!file_exists($target_path )) { mkdir($target_path , 0777, true); }

如果不存在,这将添加文件夹.. 具有所有权限。

【讨论】:

  • 我尝试添加它,但它仍然给出相同的输出
  • 它没有给出任何错误,它只是转到 move_uploaded_file 代码的 else 块:else { exit("Error While uploading image on the server"); }
【解决方案2】:

将图像的表结构更改为 varchar(255) 并检查是否有效

【讨论】:

    猜你喜欢
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多