Current location: Hot Scripts Forums » Programming Languages » PHP » Checking a file exists


Checking a file exists

Reply
  #1 (permalink)  
Old 04-22-06, 06:52 PM
lee lee is offline
Newbie Coder
 
Join Date: Sep 2003
Location: England
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Checking a file exists

I am using a very basic uploading script and just need to know how to check whether a filename already exists or not before uploading it, and if it does exist, then upload the file with a different filename. Here is my code:

PHP Code:

<? if ($Upload)


{

$upload_path getcwd();

echo 
"File uploaded!";

copy($file"$upload_path/uploads/$file_name");

}

?>

 

 

<form action="<? echo $PHP_SELF ?>" method="post" enctype="multipart/form-data" name="form1">

  <p>

    <input type="file" name="file">

</p>

  <p>

    <input type="submit" name="Upload" value="Upload">

</p>
</form>
Also, are there any security issues regarding this script? I will be allowing anyone to upload files.

Thanks!
Reply With Quote
  #2 (permalink)  
Old 04-22-06, 07:01 PM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Ouch, use this:
http://www.acecoolco.com/media_view_...p?page=1&id=28
Description: Shows you how I made my upload files form(s) in the Media Administration area. (Does NOT require globals to be on.)

That one requires globals, the one I am providing does not...
You will need to change some of the html as I havent updated my tutorials yet.
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #3 (permalink)  
Old 04-22-06, 07:03 PM
Christian's Avatar
Christian Christian is offline
Community VIP
 
Join Date: Mar 2005
Location: ProgrammingTalk
Posts: 2,449
Thanks: 0
Thanked 6 Times in 5 Posts
http://us2.php.net/manual/en/function.file-exists.php

With your code above people will be allowed to upload any type of file! Even PHP ones, here is some info from php.net about allowing and dis-allowing certin file types and checking file size.
__________________
:: ImperialBB :: New version in the works! :: http://www.imperialbb.com ::

:: Have a question about the board? The Rules? An Infraction/Warning? :: Contact Form ::
Reply With Quote
  #4 (permalink)  
Old 04-23-06, 12:44 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
...and just need to know how to check whether a filename already exists or not before uploading it, and if it does exist, then upload the file with a different filename.
Once the submit button on a file-type form is pressed the file will be uploaded (hopefully with success) to a temporary location on the server. Checking if the file name already exists and naming it differently before moving it from the temp location to the final location is fairly easy.

If you literally want to check if the file name exists before uploading it, you would need to make a "file name verification form", that just submits the file name. Your code would then check to see if the name exists. If it does, you would loop back and ask the user to enter another name (or you could automate this by adding a prefix or suffix to the 1st name and increment it until you find a file name that does not exist.) Once you have a file name that does not exist, you would use PHP to place this name into the VALUE= part of the file name field in the actual file upload form.

---------------- don't read below this line unless you are sitting down and have a health heart --------------------

If you have multiple users uploading into the same folder, I can see the possibility with both the "before" and "after" methods of checking for a duplicate file name that between the time you check if a file name exists and when you move the temporary file to the final location/name, that someone else could have uploaded a file with the same name.

The fix for both of the methods would be the same. Instead of using the file_exists() function, you would need to fopen the file for append "a." This will either open an existing file or create a new one. Use the filesize() function to find the size of the resultant file. If it is zero, then the file did not pre-exist, but it does now (reserving the file name.)

This file name reservation problem will be more pronounced if you use the method where you check if the file name exists before the upload, due to the time it will take for the upload to happen. It could take several seconds to several minutes for the upload to occur. If the file name is not reserved, someone else could upload a shorter file with the same name. Then when the code attempts to save the file that took longer, it will be faced with a duplicate file name.

Does this seam complex - yes. But that is life when you can have multiple concurrent users trying to save files to one location. You could of course leave this file name reservation logic out and live with an occasionally overwritten file.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error message not getting displayed. sanjeet Windows .NET Programming 0 12-20-05 10:48 AM
Checking the contents of a file vana Perl 1 08-02-05 03:01 AM
checking file size before uploading sjems PHP 3 04-28-05 11:12 AM
checking if a value exists endusto PHP 2 03-28-05 06:11 PM
how to check if a file exists in the include path wizkid PHP 2 01-04-05 06:47 PM


All times are GMT -5. The time now is 07:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.