12

I tried deleting file if its already existing .

But i ended up with no result.

Can any one help me with this!!!

$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
if (!file_exists($path_user)) {
    if (mkdir( $path_user,0777,false )) {
        //
    }
} 

unlink($path_user);

if(move_uploaded_file($file['tmp_name'],$path_user.$path)){
    echo "Your File Successfully Uploaded" . "<br>";
}
erikvimz
  • 5,256
  • 6
  • 44
  • 60
R9102
  • 687
  • 1
  • 9
  • 32

2 Answers2

22

Organize your code, try this:

$path = 'filename.ext'; // added reference to filename
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';

// Create the user folder if missing
if (!file_exists($path_user)) {
   mkdir( $path_user,0777,false );
}
// If the user file in existing directory already exist, delete it
else if (file_exists($path_user.$path)) {
   unlink($path_user.$path);
}

// Create the new file
if(move_uploaded_file($file['tmp_name'],$path_user.$path)) {                 
    echo"Your File Successfully Uploaded"."<br>";
}

Keep in mind that PHP will not recursively delete the directory contents, you should use a function like this one

Nil Llisterri
  • 787
  • 7
  • 19
  • i need to delete only the file inside directory.if i use this will it delete the file?? because unlink function returns TRUE on success, or FALSE on failure. – R9102 May 26 '16 at 10:19
  • This will create the dir if it doesn't exist. Then it will check if the file exists, and if it exists will delete it. Then it will move the new file to the location. If the directory has the right permissions the unlink will always return true, because there will be no access problems. – Nil Llisterri May 26 '16 at 10:44
3

Maybe you missing else condition ?? And file_name variable :

$file_name = 'sample.jpg';

$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';

if (!file_exists($path_user.$file_name)) 
{                   
  if (mkdir( $path_user,0777,false )) {

  }

} else {

  unlink($path_user.$file_name);
}
SonDang
  • 1,468
  • 1
  • 15
  • 21
  • Exactly what i am doing is if user click on edit button and upload the same file which is already exist .its not getting updated ....because both the files having same name..but file is getting uploaded successfully(if it is updated i trigger email) – R9102 May 26 '16 at 10:28
  • No.It says file uploaded successfully but there was an error while updating. – R9102 May 26 '16 at 10:39
  • not sure what you mean??? Why 2 status successfully and error together? Was it echoed out "Your File Successfully Uploaded" string?? – SonDang May 26 '16 at 10:43
  • Exactly what i am doing is if a user edits any of the filed in the form on save(Button) it gets updated and triggers mail.if filed is not changed it shows an error .Yes if file uploads i have echoed.Here what its doing is same file name right so it thinks no file change so no update..and normally its giving file upload. – R9102 May 26 '16 at 10:49
  • if any suggestion please let me know. – R9102 May 26 '16 at 10:49