Jump to content

upload a file


jagguy

Recommended Posts

When I upload a file over the net I am having problems. It works but if I log onto 2 pc's and upload a filename of the same name then my program will just overwrite the other.

Now I have a function to detect existing filenames which works but not for new files of the same name.

 

So on 1 pc i can upload a filename =my.txt and on another pc I upload a file =my.txt. If I do this at the same time then I end up with 1 file called my.txt and the other has been overwritten.

 

If I do this again then

On 1 pc i can upload a filename =my.txt and on another pc I upload a file =my.txt. If I do this at the same time then I end up with 1 file called 1my.txt and the other file called 2my.txt.

 

How can I solve this for new files to upload?

 



if (file_exists('student_files/' . $filename) )
{
// append a digit to the beginning of the name
  $tmpVar = 1;
  while(file_exists('student_files/' . $tmpVar .  $filename) )
   {
// and keep increasing it until we have a unique name
   $tmpVar++;
   }
  $filename= $tmpVar  . $filename;
}


if ($_FILES['uploadedfile']['size'] < 500  || $_FILES['uploadedfile']['size'] > 3000000 )
echo "ERROR  File too  small > 500 bytes or<br> File is too big > 3mb";
else if ($ext=='jpg' || $ext=='gif' ||   $ext=='png' || $ext=='doc' ||  $ext=='txt' ||
   $ext=='xls' || $ext=='zip' || $ext=='ppt' || $ext=='exe')
   {
    // echo "<br><br>".$ext."<br>";
      if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$filename))
       {
          echo "<strong>Upload was a success!<br><br>Filename : ".$filename." <br>was entered into the system</strong>";
          echo"<br><br>(If the filename has a number added to the front  eg 2myfile.txt that means another filename of the same name had already been entered)";

     

 

 

Link to comment
https://forums.phpfreaks.com/topic/62802-upload-a-file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.