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

Link to comment
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

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.