how to check that file exists or not

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
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

how to check that file exists or not

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Have you actually tried entering the name of a file that doesn't exist in a browser file upload element?
timgolding
Forum Newbie
Posts: 14
Joined: Tue Jul 24, 2007 9:02 am

Post 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.";
	     }

}
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply