我正在尝试上传具有随机名称的图像,而且在更新图像时,我也希望删除先前的图像。
以下代码:
$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 );
任何帮助将不胜感激。
答案 0 :(得分:0)
当时更新新图像时,请先将旧图像名称存储在变量中。更新为true后,请使用“取消链接”功能删除目录中的旧图像。
$old_image = "xyz" // store old image name
更新查询成功后
unlink('../image/banner/'.$old_image); // delete old image in your directory
答案 1 :(得分:0)
您可以这样做。
$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';
}