【问题标题】:PHP file is not uploadingPHP文件没有上传
【发布时间】:2017-08-22 17:33:57
【问题描述】:

我有一个可以上传图片的表单。但是由于某种原因,我无法上传图像。我已经更新了文件权限(777 权限),但文件仍然没有上传。该表单似乎正在工作,因为文本(未包含在表单中正在更新)

<form action="manage_content.php?project=5" method="post" enctype="multipart/form-data">
    <div class="width">
        <p>Project Image: <br>
        <input type="file" name="image_file" id="file" class="inputfile" />
        <label for="file">Choose a file</label>
        </p>
    </div>
    <input type="submit" name="update_project" value="Update Current Project">    
</form>

PHP 表单处理。

if (isset($_POST['update_project'])) {
    $required_fields = array("project_name", "date", "content");
        validate_presences($required_fields);

        $fields_with_max_length = array("project_name" => 30);
        validate_max_length($fields_with_max_length);

    $id = $current_project["id"];
    $project_name = mysql_prep($_POST["project_name"]);
    $date = mysql_prep($_POST["date"]);
    $content = mysql_prep($_POST["content"]);

    $target_dir = "images/";
$target_file = $target_dir . basename($_FILES["image_file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    print_r($_FILES);
    echo "<br>" . $target_file;

    if (empty($errors)) {
            if($_FILES["image_file"]["error"] == 4) {
            //means there is no file uploaded
            $query = "";
            $result = mysqli_query($connection, $query);

            if ($result && mysqli_affected_rows($connection) >= 0) {
                $_SESSION["message"] = "Subject Updated";
            //  redirect_to("manage_content.php?subject=$current_subject[id]");
            } 
        } else {
            $query = "";
            $result = mysqli_query($connection, $query);

            if ($result && mysqli_affected_rows($connection) >= 0) {
                $_SESSION["message"] = "Subject Updated";
            //  redirect_to("manage_content.php?subject=$current_subject[id]");

            } 
        }

    } else {
            $message = "Subject creation failed.";
            }  
    } else {
        // probably a get request
        // end of post submit
    }

?>

print_r($_FILES) 给我:

Array ( [image_file] => Array ( 
  [name] => 1.jpg 
  [type] => image/jpeg 
  [tmp_name] => C:\xampp\tmp\phpF5AA.tmp 
  [error] => 0 
  [size] => 24397 ) 
) 
and echo $target_file gives: images/1.jpg

【问题讨论】:

  • 将上传的文件移动到文件夹的代码在哪里?
  • 您遇到错误了吗?你的日志是怎么说的?
  • 谢谢你解决了。 :)

标签: php file file-upload


【解决方案1】:

确保您要上传到的目录与上传文件的程序具有相同的权限和所有权。它应该具有用户的写入权限。如果该文件已经存在,则同样如此,您必须具有写入权限才能替换该文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 2015-03-31
    • 2015-11-09
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多