upload file problem....please help

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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

upload file problem....please help

Post by qads »

ok i got my script working :D

Code: Select all

<?
if ($_FILES&#1111;'file'] != "") &#123;
	
	copy($_FILES&#1111;'file']&#1111;'tmp_name'], "".$_FILES&#1111;'file']&#1111;'name']) 
		or die("Couldn't copy the file!");  
	
&#125; else &#123;
	
	die("No input file specified");
&#125;

?>
that loads the file which is specified in a form on another page.

now how do i make sure that only .zip and .txt files are uploaded :?:


thanks in adv
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try using..
i'm not sure about the wildcards im using, but it should work

Code: Select all

<?
if ($_FILES&#1111;'file'] != "") &#123; 
 if ($_FILES&#1111;'file'] == "&#1111;&#1111;:alnum:]]+.zip" || $_FILES&#1111;'file'] == "&#1111;&#1111;:alnum:]]+.txt" ) &#123; 
   copy($_FILES&#1111;'file']&#1111;'tmp_name'], "".$_FILES&#1111;'file']&#1111;'name']) 
      or die("Couldn't copy the file!");  
&#125; else&#123;
echo "make sure it's zip or txt";
&#125;   
&#125; else &#123; 
    
   die("No input file specified"); 
&#125; 

?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

nope that does't work, it stops the upload of any kind.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Use ereg() to validate it against the uploaded file thusly:

Code: Select all

if (!ereg('(.jpg|.gif)$', $userfile)) &#123; 
    echo 'File is not a valid .gif or .jpg image!'; 
&#125;
:D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may also check against the mime-type

Code: Select all

$accepted = array('application/x-zip-compressed', 'text/plain', ...);
if (in_array($_FILES&#1111;'file']&#1111;'type'], $accepted)
  ...
Post Reply