[英]php delete file using unlink before database row update
我正在設置一個CRUD,並意識到如果用戶想要上傳新圖像,我將需要刪除存儲在目錄中的圖像。
我有一個帶有表單的網頁,該表單使用id從數據庫行中提取信息,然后將更新的值發布到麻煩所在的腳本。
我正在嘗試找到需要刪除的文件:
$target_dir = "images/photo/";
$del_image = $_FILES["image"];
並嘗試使用以下方法設置文件的權限:
$change = chmod($del_image,0644);
然后用這個嘗試刪除文件:
$delete = unlink($target_dir.$image);
在我用這個更新所有內容之前:
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$file = $target_dir . basename($_FILES["ud_image"]["name"]);
$uploadOk = 1;
if (move_uploaded_file($_FILES["ud_image"]["tmp_name"], $file))
{
echo '<script type="text/javascript">';
echo 'alert("News Items Saved")';
echo '</script>';
} else {
echo "Sorry, there was an error with your file.";
}
$id = intval($_GET['id']);
$ud_headline = $_POST["ud_headline"]; //mysql_real_escape_string()
$ud_body = $_POST["ud_body"]; //mysql_real_escape_string()
$ud_image = $_POST["ud_image"]; //mysql_real_escape_string()
$query="UPDATE news SET
headline = '$ud_headline',
body = '$ud_body',
image = '$ud_image'
WHERE id='$ud_id'";
$mysqli->query($query)or die($mysqli->error);
if($mysqli->affected_rows>=1){
echo "<script type='text/javascript'>";
echo "alert('News Item Updated');";
echo 'document.location.href = "/pc.v.2/admin-news.php";';
echo "</script>;";
}
else
{
echo "<script type='text/javascript'>";
echo "alert('News Item Not Updated'. $mysqli->error);";
echo "</script>";
//echo "Error deleting record: " . $conn->error;
}
我得到的錯誤告訴我,我甚至沒有正確找到目錄,更不用說文件了。
這是形式:
<form action="update.php" method="post" class="newNews">
<input type="hidden" name="ud_id" value="<?=$id;?>">
<!-- <input type="hidden" name="old_id" value="<?=$image;?>"> -->
<label for="title">Title</label>
<input type="text" name="ud_headline" value="<?=$headline;?>"/><br />
<label for="text">Body</label>
<textarea name="ud_body" rows="15" cols="21" value=""><?=$body;?></textarea><br />
<p>Current Photo</p>
<img src="<?=$target_dir.$image?>" alt=''><br />
<input type="file" name="ud_image" class="newsImage" ><br />
<input type="submit" name="submit" value="Update news item" class='addNew' />
</form>
我怎樣才能解決這個問題?
PHP的move_uploaded_file()
實際上會覆蓋舊文件,因此您實際上不需要執行冗余的unlink()
。
由於我們無法訪問您的服務器,因此我們無法確切地告訴您出了什么問題。 但它可能是其中一個(或多個):
$target_dir
前面加上$_SERVER['DOCUMENT_ROOT'].'/'
或服務器的根路徑,例如'/var/www/'
。 我這樣做是為了排除故障:
// Derive target paths
$target_dir = $_SERVER['DOCUMENT_ROOT'].'/images/photo/';
$target_path = $target_dir . basename($_FILES["ud_image"]["name"]);
// Check target dir exists and is writable
if (!file_exists($target_dir )) {
// Try to automatically create the folders
umask(0);
if (!mkdir($target_dir , 0777, true)) { // or whatever permissions
// Do your error handling here
echo 'Sorry, target directory does not exist and we could not create it automatically.';
exit(1);
}
}
if (!is_writable($target_dir)) {
// Do your error handling here
echo 'Sorry, the directory is not writable.';
exit(1);
}
// Check for file errors
if (!isset($_FILES["ud_image"]["tmp_name"])) {
echo 'Sorry, no upload file detected.';
exit(1);
}
if ($_FILES["ud_image"]["error"] > 0) {
echo 'Sorry, there was an error with the file. Error code: '.$_FILES["ud_image"]["error"]);
exit(1);
}
// Move uploaded file
if (!move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_path)) {
// Do your error handling here
echo "Sorry, there was an error with the upload operation.";
exit(1);
}
// If you reach here, the upload should have succeeded. Go on to do whatever else you need to do
echo '<script>alert('News Item saved')</script>';
順便說一句,服務器端代碼中的echo
語句可能不是一個好主意,但除此之外。 最好將表示和邏輯分開,以便於維護和簡化故障排除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.