-1
我有一個系統,允許用戶上傳PDF文件並將文件的名稱保存在數據庫中。從服務器刪除文件名稱不在數據庫中
如何讓這個腳本工作,以便用戶可以刪除名稱不在我的數據庫中的服務器的所有文件?
這就是我實際擁有的,但腳本會刪除所有文件。我不知道如何繼續。
<?php
include 'fimg.class.php';
require('../../../php/cone.php');
//header('Location: ../produccion.php');
$directory = "upload/";
//get all image files with a .jpg extension.
$images = glob($directory . "*");
foreach($images as $image)
{
$name = explode('_',$image);
$name = 'photos/' . $name[0];
$sql = mysql_query("SELECT imagen FROM r_alumnos");
if(mysql_num_rows($sql) == 0)
unlink($image);
}
?>
請[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[編寫](http://en.wikipedia.org/ wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –
使用'WHERE'子句。 –
什麼是數據庫中的圖像文件名?完整路徑或只是文件名?還要確保你只使用'glob($ directory。「* .jpg」)獲得'jpg'文件;' –