簡體   English   中英

如何從PHP中的文件夾中刪除圖像並從phpMyAdmin中刪除文件名?

[英]How to delete an image from folder in PHP and delete file name from phpMyAdmin?

我是PHP的初學者,我需要做的是從我的上載文件夾中刪除,以及從phpMyAdmin中刪除數據庫中的一行信息。 我知道我必須實現一個取消鏈接,但是我不確定如何將其放置在我的代碼中。 任何幫助,將不勝感激。 謝謝。

$dbc = mysqli_connect('localhost', 'root', 'root', 'myimages');

$files = glob("uploads/*.*");

if (isset($_GET['id']) && is_numeric($_GET['id']) ) { 

$query = "SELECT title FROM imagedata WHERE id={$_GET['id']}";

if ($r = mysqli_query($dbc, $query)) {
    $row = mysqli_fetch_array($r); 

    print '<form action="delete_image.php" method="post">
    <p style="color: red;">Are you sure you want to delete this image?</p>
    <p><h4>' . $row['title'] . '</h4><br>
    <input type="hidden" name="id" value="' . $_GET['id'] . '">
    <input type="submit" name="submit" value="Delete this image"></p>
    </form>';
} else { 
    print '<p style="color: red;">Could not retrieve the image because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}

} elseif (isset($_POST['id']) && is_numeric($_POST['id'])) { 

$query = "DELETE FROM imagedata WHERE id={$_POST['id']} LIMIT 1";
$r = mysqli_query($dbc, $query); 

if (mysqli_affected_rows($dbc) == 1) {
    print '<p>The image has been deleted.</p>';
} else {
    print '<p style="color: red;">Could not delete the image because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}

} else { 
print '<p style="color: red;">This page has been accessed in error.</p>';
} 

mysqli_close($dbc);

您使用什么列名來存儲文件路徑? 您將需要在第一個查詢中檢索它:

$query = "SELECT title FROM imagedata WHERE id={$_GET['id']}";

成為:

$query = "SELECT title,path FROM imagedata WHERE id={$_GET['id']}";

然后可以在數據庫查詢上方使用unlink()

unlink( $row['path'] ); // if you store full path + filename
unlink( '/path/to/your/uploads/folder/here/' . $row['path'] ); // if you store just the file name and not folder
$query = "DELETE FROM imagedata WHERE id={$_POST['id']} LIMIT 1";
$r = mysqli_query($dbc, $query); 

如果您有文件名,則可以嘗試類似

unlink($file); 

從數據庫中刪除它之后

然后打印已被刪除的消息

暫無
暫無

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

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