Page 1 of 1

how to check that file exists or not

Posted: Thu Aug 02, 2007 4:00 am
by eshban
i am using " browse file control " in my php form, how can i know that user selects valid file. It may be possible that user just type any fake string in "browse" button, so how can i validate this.

Posted: Thu Aug 02, 2007 4:04 am
by onion2k
Have you actually tried entering the name of a file that doesn't exist in a browser file upload element?

Posted: Thu Aug 02, 2007 4:41 am
by timgolding
This is a function i built for validating an image upload.

Code: Select all

if(!empty($_FILES["uploadedfile"]))
  {
   if (!getimagesize($_FILES["uploadedfile"]["tmp_name"])) {
      echo "file is not an image.";
   }
   elseif($_FILES['uploadedfile']['size']>1048576)
   {
      echo "This file is too large";
   }
 }
else
{
 $uploaddir=$_SERVER['DOCUMENT_ROOT'];
 $uploaddir.=basename( $_FILES['uploadedfile']['name']);
  if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $uploaddir))
	    {
	      echo "File uploaded! <br />";
	      echo $uploaddir;
	    }
	    else
	    {
	      echo "There was a problem when uploding the new file, please contact admin about this.";
	     }

}

Posted: Fri Aug 03, 2007 7:50 am
by eshban
thanks for the code.

can you give me the complete code.

i got errors when i run your code. please help in this regard.

Thanks

Posted: Fri Aug 03, 2007 8:12 am
by superdezign
eshban wrote:thanks for the code.

can you give me the complete code.

i got errors when i run your code. please help in this regard.

Thanks
Did you listen to onion2k? It's not possible for a user to upload a file that doesn't exist.