Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.