/    Sign up×
Community /Pin to ProfileBookmark

Why Checking File Extensions in Array Misfires ?

Php Folks,

Why is this line malfunctioning ?

[code]
if(!array_key_exists($file_extension, $allowed_file_extensions)) die(“Error: Select a valid video file format. Select an Mp4 file.”);
[/code]

I allowed the file type “mp4” and then fed the script (tried uploading) an mp4 file. I should get mssg upload successful but instead get error message:
“Select a valid video file format. Select an Mp4 file.”

Why is that ?

[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.”);
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 15.2019 — First thing I see to fix is this:
<i>
</i>if(file_exists("$directory_path . $user/ . $file_name"))

Looks like what you probably want is:
<i>
</i>if(file_exists($directory_path . "$user/" . $file_name))

You can find these things through some simple debugging, outputting variables and such to see if they really look like what you think they should, rather than making assumptions and then trying to debug by reading code.

It would also help to functionalize a lot of these things, which allows you to test functions independently (i.e. divide and conquer).
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @NogDog#1601761

"It would also help to functionalize a lot of these things, which allows you to test functions independently (i.e. divide and conquer)." - NogDog.

Q1. What do you mean ? You mean I should add blocks of codes and build my own functions and then just call the functions (reference them) whenever needed and save myself from having to repeat the codes over and over again ?

Q2. My pal, on the following, where do you suggest I add the dbl quotes, if I should add any ? These lines are malfunctioning too.
<i>
</i>if(!is_dir($directory_path . $user)) //IS THIS LINE CORRECT ?
{
$mode = "0644";
mkdir($directory_path . $user, "$mode", TRUE); //IS THIS LINE CORRECT ?
}

I think once upon a time someone suggested the "$mode" should have it. You agree to that ?

I listened to root and switched the user permissions from 0777 to 0644. Now, we are getting somewhere.

Q3. The following code fails to move the FILE to it's intended place:
<i>
</i>//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, it was like the following, which also failed to do it's job:
<i>
</i>//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();
Copy linkTweet thisAlerts:
@NogDogMar 15.2019 — Double quotes around a single variable are essentially meaningless, though they probably won't hurt anything in most cases. It only really makes sense to quote them within the scope of a larger string literal:
<i>
</i>16:23 $ php -a
Interactive shell

php &gt; $foo = 'This is a test.';
php &gt; echo $foo;
This is a test.
php &gt; echo "$foo";
This is a test.
php &gt; echo "Hello, World. $foo";
Hello, World. This is a test.
php &gt;

As far as functionalizing (to make up a word?) -- if you put atomic bits of functionality into discrete function definitions, then you can explicitly test those functions on their own to confirm they behave correctly, as opposed to hoping things work 80 lines deep into a bunch of stream-of-consciousness code. It also can make it easier to follow the logic flow of the main program, _if you name your functions well_. A simplistic example:
<i>
</i>$fileName = $_FILES['userfile']['name'];
if(file_type($fileName) != 'mp4')) {
// throw error
}
else {
// do something with file...
}

function file_type($fileName)
{
// do stuff to get the file type, maybe even using another function
return $fileType;
}

If you put the function definitions in a separate include file, then you can write tests that can be run separately to confirm that each function does exactly what it's supposed to do. Yes, "real" programmers often write more lines of test code than they do of "actual" code.
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @NogDog#1601790

You mean:
<i>
</i>php &gt; echo $foo; //Ok.
php &gt; echo "$foo"; //Unnecessary.
php &gt; echo "Hello, World. $foo"; Should use dbl quotes in such cases.

I actually, do what you suggested in your last example:
<i>
</i>php &gt; echo "Hello, World. $foo";

But, I also do this which you no like cos I got in a bad habit while learning when to use dbl quotes and when sngl etc:
<i>
</i>php &gt; echo "$foo";


Now, you are welcome to reply to my other threads.

Thanks!
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...