簡體   English   中英

上傳帶有隨機名稱的圖像並從目錄中刪除舊圖像

[英]Upload images with random name and also delete older image from the directory

我正在嘗試使用隨機名稱上傳圖像,而且當我更新圖像時,我希望刪除之前的圖像。

代碼如下:

  $banner=$_FILES ['banner']['name'];
  $upload="../image/banner/";
  $target_file = $upload.basename($_FILES["banner"]["name"]);
  $imagefiletype= pathinfo($target_file,PATHINFO_EXTENSION);
  move_uploaded_file($_FILES["banner"]["tmp_name"], $target_file );

任何幫助將不勝感激。

當那時更新您的新圖像時,首先將舊圖像名稱存儲在變量中。 更新 true 后,使用“取消鏈接”功能刪除目錄中的舊圖像。

$old_image = "xyz" // store old image name 

更新查詢成功后

unlink('../image/banner/'.$old_image); // delete old image in your directory 

你可以這樣做。

$path = 'image/folder/'; $unique_name=time().uniqid(rand()); 
$File_with_location = $path . $unique_name . '.' . strtolower(pathinfo($_FILES['img']['name'], PATHINFO_EXTENSION)); 
$filename = $_FILES["img"]["tmp_name"];        
move_uploaded_file($filename,  $File_with_location);

要刪除舊圖像,您可以使用它

$path = './root/home/folder/file.jpg';
if (unlink($path)) {
    echo 'success';
} else {
    echo 'fail';
}

暫無
暫無

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

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