簡體   English   中英

PHP-如何從目錄中刪除文件,如果文件已經存在?

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

我嘗試刪除文件(如果已存在)。

但是我最終沒有結果。

誰能幫我這個!!!

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

整理您的代碼,請嘗試以下操作:

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

請記住,PHP不會遞歸刪除目錄內容,您應該使用像這樣的函數

也許你錯過了其他條件? 和file_name變量:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM