upload file question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

upload file question

Post by ecaandrew »

how would i take this code and make it so it only accepts JPEG/GIF files ONLY

thanks...

Code: Select all

if($_FILES['thumb']['size'] > 0)
	{
		$temp1	= explode(".", $_FILES['thumb']['name']);
		$thumb	= $ident . "." . $temp1[(count($temp1) - 1)];
		
		copy($_FILES['thumb']['tmp_name'], $thumb_dir . $thumb)
			or die ("Could not copy " . $thumb_dir . $thumb);
		$thumb_url .= $thumb;
	}
	else
	{
		$thumb_url = "";
	}
thanks
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Just add:

Code: Select all

echo $_FILES['thumb']['type'];
And see for a moment what the output is, and then i hope you can figure out what to do.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

hint: check the type and compare it to what you want. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

everyone throw in a bit hint until he can get it :P

hint: it will require an IF
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

Code: Select all

if ( $_FILES['thumb']['type'] == "application/zip" ) {

    if($_FILES['thumb']['size'] > 0) {
        $temp1    = explode(".", $_FILES['thumb']['name']);
        $thumb    = $ident . "." . $temp1[(count($temp1) - 1)];
        
        copy($_FILES['thumb']['tmp_name'], $thumb_dir . $thumb)
            or die ("Could not copy " . $thumb_dir . $thumb);
        $thumb_url .= $thumb;
    } else {
        $thumb_url = "";
    }
} else {
   print "this file has to be a zip file.";
}
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

bingo! :)
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

thx now to do multiple file types, do i have to do two IF's or can i put like

and OR statement?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can use multiple if's or you can make an array of file types and use in_array() to see if its in the array.
Post Reply