move_uploaded_file问题

时间:2018-04-21 11:33:29

标签: php file-upload

我的PHP代码有问题,它不能正确运行我的move_uploaded_file。因此,它始终使用header("Location: /Chat/?upmovfailed=4&upmov=1");而不是移动文件并转到header("Location: /?success=1");。当我试图回应$_FILES["fileToUpload"]["tmp_name"]时,我得到一个空白

session_start();

$target_dir = "uploadedbyuser/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$time = time();
$newname = $_SESSION['user'].'u'.$time;
$targetuploadname = $target_dir . $newname;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_SESSION['user'])) {
    if ($_FILES["fileToUpload"]["size"] > 500000000) {
        header("Location: /Chat/?upmovfailed=1&upmov=1");
        die("");
        $uploadOk = 0;
    }
    if($imageFileType != "mp4") {
        header("Location: /Chat/?upmovfailed=2&upmov=1");
        die("");
        $uploadOk = 0;
    }
    if ($uploadOk == 0) {
        header("Location: /Chat/?upmovfailed=3&upmov=1");
        die("");
    } else {
        if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetuploadname .'.'.$imageFileType )) {
            header("Location: /?success=1");
        } else {
            header("Location: /Chat/?upmovfailed=4&upmov=1");
            die("");
        }
    }
} else {
    header("Location: /Chat/?upmov=1");
    die("");
}

1 个答案:

答案 0 :(得分:0)

仅适用于move_uploaded_file()有问题的人。检查您的<form>是否包含method="POST"enctype="multipart/form-data"属性。如果仍然失败,请检查您的php.ini设置。重要的部分是file_uploadsupload_max_filesizepost_max_size。如果你在linux下也确保你为upload-directory设置了正确的权限。