user1324106
user1324106

Reputation: 181

How to display a message if file already exists

I have a javascript function in one page and in another page I have a php script which concentrates on file uploading. At the moment my files are uploading successfully which is great news. My question though is that I want to use the php to check if a file already exists in the folder but I want to use the javascript function to display an message to state file already exists if this is true. How can this be done?

Below is the php code which checks if file exists:

if (file_exists("upload/" . $_FILES["fileImage"]["name"]))
  {

  }

Below is the Javascript function when file stops uploading. at moment if file doesn't upload it displays a message stating there is an error while uploading file and it displays a message if file is successfully uploaded, but I want an extra message where if file doesn't upload but this is because the file already exists, then I want it to display a message stating file already exists.

 function stopImageUpload(success){
          var result = '';
          if (success == 1){
             result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
          }
          else {
             result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
          }
}

P.S Please no one put in their answer why don't I just put an echo in the php code, I don't want to do it like that because the user never actually navigates to the php script.

Upvotes: 1

Views: 3051

Answers (3)

DavChana
DavChana

Reputation: 1976

@safarov Can you show me an example of a function to see if file already exists, then write different name for new uploaded file?

Save/upload a simple text file named "filenamecounter.txt", containing only text 1 in it.

<?php
    //Get the original name of file
    $tmp_name = $_FILES["filename"]["tmp_name"];

    //Get/Read File Name Counter
    $filenamecounter = file_get_contents('filenamecounter.txt');

    //If it is less than 10, add a "0"/Zero to make it like 01,02,03
    if (strlen($filenamecounter) <= 1){
        $filenamecounter = str_pad($filenamecounter, 2, '0', STR_PAD_LEFT);
        }

    //Assign Filename + Variable
    $name = "filename".$filenamecounter.".txt";

    //Save file with new name
    move_uploaded_file($tmp_name, $name);

    //write quotecounter to file.
    $filenamecounter++;
    file_put_contents("filenamecounter.txt", $filenamecounter);
?>

Upvotes: 1

pawel-kuznik
pawel-kuznik

Reputation: 456

In yours PHP code that checks if file exists do

else{
echo 2;
}

In yours JS code in else clausule do

if(success == 2){
result = '<span class="emsg">File already exist!</span><br/><br/>';
}

That is a quick solution, but it gives You a way to do more complex file handling via JS/PHP. For example. When PHP returns a data 1, then everything is ok, when 2 then file exists, when 3 then file is too large, when 4 then file with bad extension, when 5 then something else, and so on.

This method I have encountered when learning C/C++(in C this way is like a standard thing). This way You can give info how some parts of code went.

Still, I would generete a a random name for file, if the name is irrelevant, and if name of file is important then wold use AJAX to check it, and display info about it, or maybe append a number after file name (file(1).xyz, file(2).xyz, file(3).xyz). That depends on what You are trying to achieve.

Upvotes: 1

mpm
mpm

Reputation: 20155

You say.

P.S Please no one put in their answer why don't I just put an echo in the php code, I don't want to do it like that because the user never actually navigates to the php script.

you need to execute some php code anyway. so you will have to do this one way or another. then , display the information to the user using whatever way you want.

since we dont have all the code i assume you have a input[type=file] in the html code , so you need to use ajax with the value of the input. send it to your server , check if the filename already exists , then respond with true or false with ajax from php and execute the code in javascript that will tell the user if the file exists or not. you can use jQuery to do that :

        $("#myInput").on("change",function(event){
             $.getJSON("checkFileService.php",{filename:$(event.currentTarget).val()},
        function(datas){
         if(datas.exists === true){
          doSomething();
         else{
          doSomethingElse();
         }
        }
       }

Check the jQuery ajax api for more infos

you'll have to write a php script that outputs some json string like {"exists":true} in order for the client script to work.

Upvotes: 1

Related Questions