OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 6:00 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Uploading files using PHP...
PostPosted: Sat Oct 26, 2002 11:01 am 
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:
<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


Top
  
 
 Post subject: Re:Uploading files using PHP...
PostPosted: Sat Oct 26, 2002 10:00 pm 
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.


Top
  
 
 Post subject: Re:Uploading files using PHP...
PostPosted: Sun Oct 27, 2002 4:12 pm 
To check to see if the file already exists use this command:

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


Top
  
 
 Post subject: Re:Uploading files using PHP...
PostPosted: Sun Oct 27, 2002 10:49 pm 
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:

<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>



Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 142 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group