为什么即使目录权限0755、0777和0644也无法移动上载的文件?

时间:2019-04-17 22:27:33

标签: php file-upload error-handling

早安,

由于某些原因,即使在手册的帮助下,我也无法使move_uploaded_file起作用。 我得到我的错误: “您的视频文件“ blah”未能上传!您可以尝试其他时间。”。

在这里触发IF:

if(!move_uploaded_file($file_tmp, 
"$directory_path_and_user_dir/$file_name")) 
                { 
                    //Notify user their Id Verification Video File 
uploading failed. 
                    echo "Your Video File \"$file_name\" has failed to 
be uploaded! You may try some other time."; 
                    exit(); 
                } 
                else 
                { 
                    //Notify user their Id Verification Video File was 
uploaded successfully. 
                    echo "Your Video File \"$file_name\" has been 
uploaded successfully!"; 
                    exit(); 
                } 

这是我的代码...

<?php 
//Required PHP Files. 
include 'header_account.php'; //Required on all webpages of the Site. 


if (!$conn) 
{ 
    $error = mysqli_connect_error(); 
    $errno = mysqli_connect_errno(); 
    print "$errno: $error\n"; 
    exit(); 
} 

if($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
        if(!isset($_FILES["id_verification_video_file"])) 
        { 
            echo "Upload your video file"; 
        } 
        {
            //Feed Id Verification Video File Upload Directory     path. 
        $directory_path = 'uploads/videos/id_verifications/'; 
        //Make Directory under $user in 'uploads/videos/id_verifications' Folder. 
        $directory_path_and_user_dir = "uploads/videos/id_verifications/$user"; 

        if(!is_dir($directory_path_and_user_dir)) //IS THIS LINE CORRECT ?
        { 
            $db_user = 'root'; 
            $mode = 0755; 
            mkdir($directory_path_and_user_dir,$mode, TRUE); //IS THIS LINE CORRECT ?
        } 

        //Grab Uploading File details. 
        $Errors = Array(); //SHOULD I KEEP THIS LINE OR NOT ?
        $file_name = $_FILES["id_verification_video_file"]["name"]; 
        $file_tmp = $_FILES["id_verification_video_file"]["tmp_name"]; 
        $file_type = $_FILES["id_verification_video_file"]["type"]; 
        $file_size = $_FILES["id_verification_video_file"]["size"]; 
        $file_error = $_FILES['id_verification_video_file']['error']; 

        //Grab Uploading File Extension details. 
        $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);             

        $directory_path_and_user_dir_and_user_file = "$directory_path_and_user_dir/$file_name"; 

        //if(file_exists("$directory_path_and_user_dir_and_user_file")) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD GIVES THE ECHO THAT IS 26 LINES BELOW HERE: "Your Video File \"$file_name\" has been uploaded successfully!"
        if(file_exists($directory_path_and_user_dir_and_user_file)) ////THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD SHOWING BLANK WHITE PAGE.
        { 
            $Errors[] = "Error: You have already uploaded a video file to verify your ID!"; 
            exit(); 
        } 
        else 
        { 
            //Feed allowed File Extensions List. 
            $allowed_file_extensions = array("mp4" => "video/mp4"); 

            //Feed allowed File Size. 
            $max_file_size_allowed_in_bytes = 1024*1024*100; //Allowed limit: 100MB. 
            $max_file_size_allowed_in_kilobytes = 1024*100; 
            $max_file_size_allowed_in_megabytes = 100; 

            $max_file_size_allowed = "$max_file_size_allowed_in_bytes"; 

            //Verify File Extension. 
            if(!array_key_exists($file_extension, $allowed_file_extensions)) 
            { 
                $Errors[] = "Error: Select a valid video file format. Select an Mp4 file."; 
            } 
            //Verify MIME Type of the File. 
            elseif(!in_array($file_type, $allowed_file_extensions)) 
            { 
                $Errors[] = "Error: There was a problem uploading your file $file_name! Make sure your file is an MP4 video file. You may try again."; //IS THIS LINE CORRECT ?
            } 
            //Verify File Size. Allowed Max Limit: 100MB. 
            elseif($file_size>$max_file_size_allowed) 
            { 
                $Errors[] = "Error: Your Video File Size is larger than the allowed limit of: $max_file_size_allowed_in_megabytes."; 
            } 

            //Move uploaded File to newly created directory on the server. 
            if(!move_uploaded_file($file_tmp, "$directory_path_and_user_dir/$file_name")) 
            { 
                //Notify user their Id Verification Video File uploading failed. 
                echo "Your Video File \"$file_name\" has failed to be uploaded! You may try some other time."; 
                exit(); 
            } 
            else 
            { 
                //Notify user their Id Verification Video File was uploaded successfully. 
                echo "Your Video File \"$file_name\" has been uploaded successfully!"; 
                exit(); 
            } 
        } 
    } 
} 

?>

<form METHOD="POST" ACTION="" enctype="multipart/form-data"> 
<fieldset> 
<p align="left"><h3><?php $site_name ?> ID Video Verification Form</h3> 
</p> 
<div class="form-group"> 
<p align="left"<label>Video File: </label> 
<input type="file" name="id_verification_video_file" 
id="id_verification_video_file" value="uploaded 'Id Verification Video 
File.'"></p> 
</div> 
</fieldset> 
<p align="left"><button type="submit" class="btn btn-default" 
name="id_verification_video_file_submit">Submit!</button></p> 
</form> 

</body> 
</html> 

<?php 
include 'footer_account.php'; //Required on all webpages of the Site. 
?>

Q1。注意我对代码的评论。我在那边有问题。请回答我的问题。

Q2。伙计们,我的错误数组可以吗?注意我在哪里回显错误。他们有什么问题吗?

Q2。提供了以下信息:

[code]
$Errors = Array(); 
$Errors[] = "Error: " . $_FILES["id_verification_video_file"] ["ERROR"]; 
print_r($_FILES); ?><br><?php 
print_r($_ERRORS); 
exit(); 
[/code]

不确定是否正确。请注意大写和非大写。对我来说似乎不正确。应该怎么办?

0 个答案:

没有答案