R9102
R9102

Reputation: 727

php-how to delete files from directory if files already exist?

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>";
}

Upvotes: 14

Views: 55679

Answers (2)

Nil Llisterri
Nil Llisterri

Reputation: 867

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

Upvotes: 27

SonDang
SonDang

Reputation: 1597

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);
}

Upvotes: 3

Related Questions