topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • October 17, 2019, 10:43 PM
  • Proudly celebrating 13 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Why Checking File Extensions in Array Misfires ?  (Read 804 times)

The Code Queryer

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
Why Checking File Extensions in Array Misfires ?
« on: April 06, 2019, 12:37 PM »
Php Seniors,

Why is this line malfunctioning ?
if(!array_key_exists($file_extension, $allowed_file_extensions)) die("Error: Select a valid video file format. Select an Mp4 file.");

I allowed the file type "mp4" and then fed the script (trying to uploade an mp4 file) an mp4 file.
I should get mssg upload successful and only get echoed errors if the selected file is not a video file. Since, I selected a video file to upload via the html form then I should not get any errors echoed. Common sense!
But guess what ? I get error message echoed: "Select a valid video file format. Select an Mp4 file."

Why is that ?

Here my code:
$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_extension = pathinfo($file_name, PATHINFO_EXTENSION);
if(file_exists("$directory_path . $user/ . $file_name"))
{
$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');

//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 resource.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
//Apply the FileInfo resource and the finfo_file() function to a given file.
$mime = finfo_file($finfo,$file_name);
//Close the fileinfo resource.
finfo_close($finfo);

//Verify File Extension.
if(!array_key_exists($file_extension, $allowed_file_extensions)) die("Error: Select a valid video file format. Select an Mp4 file.");


The following code fails to move the FILE to it's intended place:
//Move uploaded File to newly created directory on the server.
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();

Before, attempted this with no luck too:

//Move uploaded File to newly created directory on the server.
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();

The Code Queryer

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
Re: Why Checking File Extensions in Array Misfires ?
« Reply #1 on: April 06, 2019, 12:40 PM »
Php Lovers,

You're welcome to show me a code sample using file_info() to only accept mp4 and .wav video files. This way, from your sample others like me can learn. :Thmbsup:

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,584
    • View Profile
    • Donate to Member
Re: Why Checking File Extensions in Array Misfires ?
« Reply #2 on: April 06, 2019, 05:57 PM »
Are you sure you uploaded a .mp4 file?  Did you check just the extension of that file to determine if that file is mp4 or did you check the actual content?

MP4 is a container, so it can hold video, multiple audio tracks, subtitles etc. It might be that some of the content in the .mp4 file you uploaded is sufficiently messed up for the database to not recognize the uploaded file as an .mp4 file. Because I can tell you already that .mp4 files are not created equally. That is not a MP4 problem alone, every container file format has this problem.

Video or audio files, well, binary file formats are stored in databases as a BLOB. You should limit BLOBs as much as you can get away with. These increase your database storage needs exponentially and it is data your database cannot look through...which is the whole point of using a database in the first place.

Also, the term 'upload' suggests that you use a web server in combination with your PHP installation. When making adjustments in your configuration to allow for .mp4 files, did you restart the web server as well?

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 8,834
    • View Profile
    • Donate to Member
Re: Why Checking File Extensions in Array Misfires ?
« Reply #3 on: April 07, 2019, 04:41 AM »
Php Lovers,

You're welcome to show me a code sample using file_info() to only accept mp4 and .wav video files. This way, from your sample others like me can learn. :Thmbsup:
-The Code Queryer (April 06, 2019, 12:40 PM)

It seems to me that a lot of your questions would be better suited (and be more likely to get helpful answers) on a site such as https://stackoverflow.com/.