Jump to content

validating flie types


postbil.com

Recommended Posts

Hello Phpfreaks..

 

I have a little problem with a validating of different file types. At my site I had made an script there make its possible for the user to upload their own picture. But now I need a way to ensure that it just JPEG and GIF files the user can upload..

I had reed the manual but I don’t find something useful..

Are there somebody there can / will help me?

Thanks a lot!!

 

 

Postbil.com       

 

Link to comment
https://forums.phpfreaks.com/topic/179624-validating-flie-types/
Share on other sites

Here at the code if it can be a help..

 

<?php

 

if ($_FILES['minfil']) //Har brugeren forsøgt at uploade noget?

{

    //Bestem hvor filen skal smides hen og og hvad den skal hedde

    $destination = "Image/" . $_FILES['minfil']['name'];

 

    //Forsøg at flyttede den uploadede fil har dens midlertidige destination til den nye

    if (move_uploaded_file($_FILES['minfil']['tmp_name'], $destination))

    {

        echo "Filen" . $_FILES['minfil']['name'] . " blevet uploadet";

    }

    else

    {

        echo "Der er sket en fejl";

    }

}

 

?>

 

<form action="eks3.php" method="post" enctype="multipart/form-data">

    <input type="hidden" name="MAX_FILE_SIZE" value="100000">

    Vælg fil: <input name="minfil" type="file">

    <input type="submit" value="Upload fil">

</form>

Hi postbil.com,

 

I've added a simple if statement to your code to check the filetype being uploaded.  If it's .JPG or .GIF the script will continue.  If not, it will echo an error message.

 

<?php

if ($_FILES['minfil']) //Har brugeren forsøgt at uploade noget?
{
             //Perform a check to see which filetype has been uploaded.  If it's a .JPG or a .GIF continue.
	if( $_FILES["minfil"]["type"] == "image/pjpeg" || $_FILES["minfil"]["type"] == "image/jpeg" || $_FILES["minfil"]["type"] == "image/gif" )
	{
    //Bestem hvor filen skal smides hen og og hvad den skal hedde
    $destination = "Image/" . $_FILES['minfil']['name'];

    //Forsøg at flyttede den uploadede fil har dens midlertidige destination til den nye
    if (move_uploaded_file($_FILES['minfil']['tmp_name'], $destination))
    {
        echo "Filen" . $_FILES['minfil']['name'] . " blevet uploadet";
    }
    else
    {
        echo "Der er sket en fejl";
    }
}
//If the filetype being uploaded is not a .JPG or a .GIF then echo the error below.
else
    {
        echo "Please upload a valid filetype.  Valid filetypes are .JPG and .GIF";
    }
}

?>

<form action="eks3.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000">
    Vælg fil: <input name="minfil" type="file">
    <input type="submit" value="Upload fil">
</form> 

 

Hope this helps.

By the way, using mime-types is not the best way. Mime types can be easily spoofed, and some browsers don't even send them. Some even send different ones for specific types.

 

I usually restrict file type like so

$allowed = array("jpg", "jpeg", "gif");//make an array for allowed file types
$ext = end(explode('.', $_FILES['minfile']['name']));//get the extension of the file

//check it

if (!in_array($ext, $allowed)){//its not an allowed file type
echo "File type not accepted.";
exit();
}
//continue with upload

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.