Page 1 of 1

Uploading files using PHP...

Posted: Sat Oct 26, 2002 11:01 am
by juncmodule
Okay, this works just dandy...except that you can overwrite files...how do I check to see if the file already exists, give a error if it does, and then repost the form?

Code: Select all

<HTML>
<HEAD>
<TITLE>Submit.php</TITLE>
</HEAD>
<BODY>
<?php

if ($File) {
   print ("File Name: $File_name<P>\n");
   print ("File Size: $File_size<P>\n");
   if (copy ($File, "users/$File_name")) {
   print ("Your file was successfully uploaded!<P>\n");
   } else {
      print ("Your file could not be uploaded.<P>\n");
   }
   unlink ($File);
}

print ("Upload a file to the server:\n");
print ("<FORM ACTION=\"submit.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n");
print ("File <INPUT TYPE=FILE NAME=\"File\" SIZE=20><BR>\n");
print ("<INPUT TYPE=SUBMIT NAME=\"SUBMIT\" VALUE=\"Submit!\"></FORM>\n");
?>
</BODY>
</HTML>
thanks,
-junc

Re:Uploading files using PHP...

Posted: Sat Oct 26, 2002 10:00 pm
by grey wolf
as i've never worked with uploaded files, i'll have to refer you to the manual:

http://www.php.net/manual/en/features.file-upload.php

a lot of people have also provided a lot of good tips at the bottom of the page.

Re:Uploading files using PHP...

Posted: Sun Oct 27, 2002 4:12 pm
by AGI1122
To check to see if the file already exists use this command:

Code: Select all

if (file_exists("file.ext")) {
 // Does exist
}
else {
 // Doesn't exist
}

Re:Uploading files using PHP...

Posted: Sun Oct 27, 2002 10:49 pm
by juncmodule
Ugh...I tried looking at the upload section of the PHP manual last night and it made my head hurt. For the most part the manual seems to be very well laid out, just not that part I guess. That seemed like such a simple thing to be looking for. Thanks Chris. I had set up a regular expression to try to keep people from overwriting stuff...but that isn't exactly 100%. Anyway, below is my finished script for others to use if they like. I did still have one minor flaw happening...the message at the bottom of the script was supposed to be a error message, but it kept on showing up as soon as the page was loaded. I assume that somehow it is running a empty variable through my conditional...or something. Well, screw it, it works...my first PHP script. Yay. Happy me! ;D

Code: Select all


<HTML>
<HEAD>
<TITLE>Submit.php</TITLE>
</HEAD>
<BODY>
<?php
print ("Upload your file to the server!:\n");
print ("<FORM ACTION=\"submit.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n");
print ("File <INPUT TYPE=FILE NAME=\"File\" SIZE=20><BR>\n");
print ("<INPUT TYPE=SUBMIT NAME=\"SUBMIT\" VALUE=\"Submit!\"></FORM>\n");
$Pattern = "\.zip$";
if (eregi($Pattern, $File_name)) {
    if (file_exists("uploads/$File_name")) {
      // Does Exist
      print ("That file name is already in use, please try another one.");
      }
      else {
      // Does not exist
      if ($File) {
         print ("File Name: $File_name<P>\n");
         print ("File Size: $File_size<P>\n");
         if (copy ($File, "uploads/$File_name")) {
         print ("Your file was successfully uploaded!<P>\n");
         } else {
            print ("Your file was not uploaded\n");
         }
         unlink ($File);
      }
   }
}
else {
   print ("Zip files only please. If you hit submit and this message doesn't go away then your upload failed.<BR>\n");
}   
?>
</BODY>
</HTML>