0

I need to get this script to check if the uploaded file is a video file or not and whether the file size is too big or not over the limit. Therefore, need to replace the getimagesize with something else that gets the video file size. How can I accomplish this? Which function to use here? getvideosize function does not exist. This is where I am stuck.

<?php 

if($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
        //Check whether the file was uploaded or not without any errors. 
        if(!isset($_FILES["id_verification_video_file"]) && 
$_FILES["id_verification_video_file"]["Error"] == 0) 
        { 
            $Errors = Array(); 
            $Errors[] = "Error: " . $_FILES["id_verification_video_file"] 
["ERROR"]; 
        print_r($_FILES); ?><br><?php 
        print_r($_ERRORS); 
        exit(); 
    } 
    else 
    { 
        //Feed Id Verification Video File Upload Directory path. 
        $directory_path = "uploads/videos/id_verifications/"; 
        //Make Directory under $user in 'uploads/videos/id_verifications' 
        Folder. 
        if(!is_dir($directory_path . $user)) //IS THIS LINE CORRECT ?
        { 
            $mode = "0777"; 
            mkdir($directory_path . $user, "$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']; 

$file = $_FILES["id_verification_video_file"]["name"];

// in PHP 4, we can do:
$fhandle = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($fhandle,$file); // e.g. gives "video/mp4" 

// in PHP 5, we can do:

$file_info = new finfo(FILEINFO_MIME); // object oriented approach!
$mime_type = $file_info->buffer(file_get_contents($file));  // e.g. gives 
"video/mp4"

 switch($mime_type) {
 case "video/mp4":
 // my actions go here...
}


//  Let's assume that the name attribute of the file input field I have 
used is "id_verification_video_file"

$tempFile =  $_FILES['id_verification_video_file']['tmp_name'];  // path of 
the temp file created by PHP during upload. I MOST LIKELY GOT THIS LINE 
WRONG AT THE END PART. HOW TO CORRECT THIS ?
$videoinfo_array = getimagesize($tempFile);   // returns a false if not a 
valid image file

if ($videoinfo_array !== false) {
$mime_type = $videoinfo_array['mime'];
switch($mime_type) { 

 case "video/mp4":
 // your actions go here...
 move_uploaded_file("$file_tmp", "$directory_path" . "$user/" . 
"$file_name"); //IS THIS LINE CORRECT ?
//Notify user their Id Verification Video File was uploaded successfully. 
echo "Your Video File \"$file_name\" has been uploaded successfully!"; 
exit(); 

    }
}
else {
    echo "This is not a valid video file";
}

 }
    }

    ?> 

<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. 
?> 

Best I done so far is above. I'd appreciate if you guys can add the correct lines where they should be and add comments so I can easily spot your changes and learn from the corrections.

EDIT: Folks, I managed to fix a lot of things on my current update. But, one new problem. The move_uploaded_file() is failing. Why is that ? Do have a look. I actually wrote my questions to you in my code's comments in CAPITAL. If you could kindly answer these questions then I'd be grateful and hopefully we could close this thread as SOLVED asap.

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

<?php 

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

if($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
        //Check whether the file was uploaded or not without any errors. 
        if(!isset($_FILES["id_verification_video_file"]) && 
$_FILES["id_verification_video_file"]["Error"] == 0) 
        { 
            $Errors = Array(); 
            $Errors[] = "Error: " . $_FILES["id_verification_video_file"] 
["ERROR"]; 
            print_r($_FILES); ?><br><?php 
            print_r($_ERRORS); 
            exit(); 
        } 
        else 
        { 
            //Feed Id Verification Video File Upload Directory path. 
            $directory_path = "uploads/videos/id_verifications"; 
            //Make Directory under $user in 
'uploads/videos/id_verifications' Folder if it doesn't exist. 
            if(!is_dir("$directory_path/$user")) //IS THIS LINE CORRECT ?
            { 
                $mode = "0777"; 
                mkdir("$directory_path/$user", $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"]; 
echo "File Type: $file_type<br>"; //Outputs: "". WHY $file_type SHOWS 
BLANK VALUE WHEN UPLOADING VIDEO FILES ? WORKS WITH OTHER FILES, LIKE 
JPEG.
            $file_size = $_FILES["id_verification_video_file"]["size"]; 
            $file_error = $_FILES['id_verification_video_file']['error']; 
echo "File Name: $file_name<br>"; //Outputs: "id_check.mp4"

            //Grab Uploading File Extension details. 
            $file_extension = pathinfo($file_name, PATHINFO_EXTENSION); 
echo "File Extension: $file_extension<br>"; //Outputs: "mp4"
            if(file_exists($directory_path . "$user/" . $file_name)) 
//WHICH LINE IS CORRECT ? THIS ONE OR THE NEXT ONE ?
            //if(file_exists($directory_path . $user . '/' . $file_name)) 
//WHICH LINE IS CORRECT ? THIS ONE OR THE PREVIOUS ONE ?
            { 
                $Errors[] = "Error: You have already uploaded a video 
file to verify your ID!"; 
                exit(); 
            } 
            else 
            { 
                //Feed allowed File Extensions List. 
                $allowed_file_extensions = array("video/mp4"); 

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

                $max_file_size_allowed = 
"$max_file_size_allowed_in_bytes"; 

                //Create a fileinfo respource. 
                $finfo = finfo_open(FILEINFO_MIME_TYPE); 
                //Apply the fileinfo resource and the finfo_file() 
function to the uploading given file. 
                $mime = finfo_file($finfo,$file_name); 
                //Close the fileinfo resource. 
                finfo_close($finfo); echo "Mime: $mime<br>"; //exit; 
//Outputs: video/mp4

            //Verify File Extension. 
            //if(!in_array($file_extension, $allowed_file_extensions)) 
die("Error 1: Select a valid video file format. Select an Mp4 file."); 
            //Verify MIME Type of the File.                 
            if(!in_array($mime, $allowed_file_extensions)) die("Error 2: 
Select a valid video file format. Select an Mp4 file."); 
            elseif(!in_array($file_type, $allowed_file_extensions)) 
die("Error 3: 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: 1MB. 
            if($file_size>$max_file_size_allowed) die("Error 4: 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/$user/$file_name")) die("Error 5: Your file failed to 
upload! Try some other time."); 
            else 
            { 
                move_uploaded_file($file_tmp, 
"$directory_path/$user/$file_name"); //WHY IS NOT THIS LINE OF CODE 
MOVING THE FILE TO DESTINATION ?
                //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. 
?> 

I get echoed when trying to upload an mp4 file: Error 3: There was a problem uploading your file id_check.mp4! Make sure your file is an MP4 video file. You may try again.

Should I set the folder permissions to 0644 from 0777 ? I am being told I should not allow any files to be executable in the folder by users (file uploaders) and so I should set it to readable & writeable only to "0644". I need your expert opinion on this.

Alistair
  • 9
  • 3
  • @showdev, That other thread got to do with gedID3 and this has nothing to do with it. Frankly, I don't know what the getID3 is and so that thread is of no help to me. Plus, it's not my thread. – Alistair Mar 14 '19 at 16:25
  • 1
    Are you referring to file size or video dimensions? You asked for a video version of `getimagesize()`, which returns image dimensions, but you also ask for file size. The [possible duplicate](https://stackoverflow.com/questions/4847752/how-to-get-video-duration-dimension-and-size-in-php) I posted relates to video dimensions. If you want file size, try [filesize()](http://php.net/manual/en/function.filesize.php). – showdev Mar 14 '19 at 16:31
  • I just want to build a web form that allows users to upload video files only and the file size should not be more than 100MB. I was told finfo() php function is solid. So, trying to build one with that. I'd appreciate it if each of you show code samples how you'd achieve the purpose so I can learn different ways of doing the same thing. Learn from each and everyone of you. Users should not be able to upload viruses, etc. That is the big aim. Thanks! – Alistair Mar 14 '19 at 18:08
  • @showdev, I was asking for video file size. I need the script to check whether the video file size is more than 100MB or not. If it is then show error for the user to get him to make his video shorter. That is all. Did you understand that or not ? – Alistair Mar 15 '19 at 10:30
  • [filesize — Gets file size](http://php.net/manual/en/function.filesize.php). The `$_FILES` array also contains file size information, e.g. `$_FILES['userfile']['size']`. See [POST method uploads](http://php.net/manual/en/features.file-upload.post-method.php). It looks like you've already defined a variable `$file_size` using that method. – showdev Mar 15 '19 at 10:42
  • Folks, Pls check my EDIT on my original post for my latest updated code. I get echoed: "Error 3: There was a problem uploading your file id_check.mp4! Make sure your file is an MP4 video file. You may try again." Thanks! – Alistair Mar 15 '19 at 22:09

0 Answers0