PHP文件没有上传

时间:2017-08-22 17:33:57

标签: php file file-upload

我有一张表格可以上传图片。但由于某种原因,我无法上传图像。我已更新文件权限(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

1 个答案:

答案 0 :(得分:2)

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