Page 1 of 1

upload file problem....please help

Posted: Wed Jun 12, 2002 4:41 pm
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

Posted: Wed Jun 12, 2002 4:51 pm
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; 

?>

Posted: Wed Jun 12, 2002 5:47 pm
by qads
nope that does't work, it stops the upload of any kind.

Posted: Wed Jun 12, 2002 5:53 pm
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

Posted: Wed Jun 12, 2002 7:06 pm
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)
  ...