Jump to content

[SOLVED] Checking File Types


supanoob

Recommended Posts

Hey, im using an upload script i found and am wanting to mod it to check file type and size. I was wondering if you could tell me how to do this?

 

This is the code i have, and i want it to check make sure the it is a jpeg, jpg or gif and less than 25KB in size.

<?php

if ($_GET['action'] == 'uploaded')
{

// Your file name you are uploading
$file_name = $HTTP_POST_FILES['ufile']['name'];

// random 4 digit to add to our file name
// some people use date and time in stead of random digit
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables

$new_file_name=$random_digit.$file_name;

//set where you want to store files
//in this example we keep file in folder upload
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "images/characters/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{

echo "Successful<BR/>";
//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";


}
else
{
echo "Error";
}
}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/70151-solved-checking-file-types/
Share on other sites

im using PHP version 5.2.3

 

In that case you'll want to change each instance of $HTTP_POST_FILES to $_FILES for a start.  Then as MadTechie says, you can use $_FILES['ufile']['size'] to get the size.

 

As for how you want to chose the file type, this can be either by extension or mime type.

 

Regards

Huggie

im using PHP version 5.2.3

 

In that case you'll want to change each instance of $HTTP_POST_FILES to $_FILES for a start.  Then as MadTechie says, you can use $_FILES['ufile']['size'] to get the size.

 

As for how you want to chose the file type, this can be either by extension or mime type.

 

Regards

Huggie

 

any examples on how to do that mime? or the extensions?

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.