/    Sign up×
Community /Pin to ProfileBookmark

Folks,

I need your brilliant help to teach me how to use the finfo() function when it’s to do with uploading the right file with the right max size.
I only want to allow users to upload video file no more than 10MB in size.
Now, how to write the code with finfo() ?
This is my best shot …

[code]
<?php

//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’];

$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, “$directory_path/$user/uploads/$file_name”);
finfo_close($finfo);

?>

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

[/code]

Now, I need your expertise to fill-in the rest of the missing puzzle pieces so I can learn from your sample code.

Yeah, I know I should be writing conditions where script shows error if right file type not given or file size exceeds limit. But, I know not how to start with this finfo() function. The manual was no good. Hence, I need you guys to chime in and shove your wands right this way and add your bits of pieces to help me solve the puzzle! ?
Do hop on-board!

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmFeb 21.2019 — Using the manual you will find this example:

http://php.net/manual/en/features.file-upload.post-method.php
Copy linkTweet thisAlerts:
@site-developerauthorFeb 21.2019 — @ginerjm#1601175

Thanks man!

I am experimenting with the sample code found on this tutorial:

https://www.sanisoft.com/blog/2010/05/17/how-to-use-php-fileinfo-extension-to-get-file-mime-type/

As for your manual link, I tried that method and it was unreliable. Hence was suggested to try finfo_file() instead.

Look what I mean about the problem I was encountering:

https://www.webdeveloper.com/forum/d/383994-file-upload-error

Stay tuned tonight!

Cheers Jim!
Copy linkTweet thisAlerts:
@site-developerauthorFeb 21.2019 — Folks,

What is 'Resource id #16' ?

This code of mine outputted the above line:
<i>
</i>//Verify File Type BY CHECKING File MIME type
$finfo = finfo_open();
$fileinfo = finfo_file($finfo, $file_name, FILEINFO_MIME);
finfo_close($finfo);

<i> </i> echo "$finfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> echo "$fileinfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> exit;


Even this following code echoes same. Note the "16".

<i>
</i>//Verify File Type BY CHECKING File MIME type
$finfo = finfo_open16();
$fileinfo = finfo_file($finfo, $file_name, FILEINFO_MIME);
finfo_close($finfo);

<i> </i> echo "$finfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> echo "$fileinfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> exit;


What is all this "resource" thing ?
Copy linkTweet thisAlerts:
@site-developerauthorFeb 21.2019 — Folks,

With the following script, if I upload a video file then I get no error as it's ok to upload video files. So far, so good.

Now, if I upload a txt file then I should get error but I don't! Why is that ?

ATTEMPT 1:
<i>
</i>//Verify File Type by CHECKING File MIME type
$finfo = finfo_open();
$fileinfo = finfo_file($finfo, $file_name, FILEINFO_MIME);
finfo_close($finfo);

<i> </i> echo "$finfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> echo "$fileinfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> if($fileinfo != 'video/mp4; charset=binary')
<i> </i> {
<i> </i> $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 ?
<i> </i> exit;
<i> </i> }


ATTEMPT 2:
<i>
</i>//Verify File Type by CHECKING File MIME type
$finfo = finfo_open();
$fileinfo = finfo_file($finfo, $file_name, FILEINFO_MIME);
finfo_close($finfo);

<i> </i> echo "$finfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> echo "$fileinfo";?&gt;&lt;br&gt;&lt;?php
<i> </i> if(!$fileinfo = 'video/mp4; charset=binary')
<i> </i> {
<i> </i> $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 ?
<i> </i> exit;
<i> </i> }


I need my previous post answered too!

Thanks!
Copy linkTweet thisAlerts:
@site-developerauthorFeb 26.2019 — No one here used finfo() before ? I need to know my answers, folks.

Thanks!
Copy linkTweet thisAlerts:
@ginerjmFeb 26.2019 — That means that you are trying to print something that can't be printed directly - a Resource.

As for "unreliable" - whatever do you mean???
Copy linkTweet thisAlerts:
@site-developerauthorFeb 26.2019 — @ginerjm#1601270

Someone told me unreliable. Meaning the code without finfo() is unreliable to detect wrong file type or wrong file size. This code was unreliable:

https://www.webdeveloper.com/forum/d/383994-file-upload-error

Guessing it will not do the job. Because I then foundout after many mnths of it working that it does not work anymore. That code on that thread I mean.

Meaning when I select a wrong file type or wrong file size then I should get error based on the error echoes I placed. But for many months I got the errors echoed when I deliberately selected wrong file types or wrong file sizes (bigger files). That means code was working fine. But then many mnths later one day I did not get these errors (echoes) I was supposed to get when deliberately trying to upload a wrong file type or a wrong file size (as a test). Hence, she told me that, whatever method I was using to detect file type and size was unreliable. Should use fileinfo() as it is reliable to do the job. Get it ?

I realize now that those other methods I used in the other thread, they were unreliable just like she said.

So big buddy, how-about showing me a code snippet where I am stuck and frozen like snow man ?
Copy linkTweet thisAlerts:
@ginerjmFeb 28.2019 — Alanw??? Does this mean you have adopted yet another pseudo-name to pepper us with your interminable problems?
Copy linkTweet thisAlerts:
@site-developerauthorMar 03.2019 — @ginerjm#1601331

No. I do not know who this Alan is. Maybe, he had the same problem ?
Copy linkTweet thisAlerts:
@site-developerauthorMar 03.2019 — @alanw#1601308

You should reply to Ginerjm.
Copy linkTweet thisAlerts:
@site-developerauthorMar 14.2019 — @ginerjm#1601270

Then that means I got it all wrong. So, how'd you code it then ? I'd like to see how you'd code it.

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.26,
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,
)...