PHP 刪除檔案

來源:互聯網
上載者:User
include("conn/conn.php");
$id=$_GET[id];
$tupian=$_GET[tupian];
$user = new shanchu();
$user->delFileUnderDir();
$sql=mysql_query("delete from image where id=$id");

class shanchu {
//迴圈目錄下的所有檔案
function delFileUnderDir( $dirName="../admin/upimages" )
{
if ( $handle = opendir( "$dirName" ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != "." && $item != ".." ) {
if ( is_dir( "$dirName/$item" ) ) {
delFileUnderDir( "$dirName/$item" );
} else {
if( unlink( "$dirName/$item" ) )
echo "";

}
}
}
closedir( $handle );
}
}
}
?>

現在是刪除檔案夾下的所以檔案,我想要的是單個檔案的刪除操作。
還有幫忙加下資料庫欄位的刪除結合。


回複討論(解決方案)

http://www.phpthinking.com/archives/238 可以參考下


可以在我發的代碼的基礎下改嗎?

首先你需要找出所有檔案,然後列表出來
例如
file1 刪除
file2 刪除
file3 刪除
file4 刪除
點刪除時,把檔案名稱帶過去。例如刪除

接受到filename之後,使用unlink($filename);就可以刪除單個檔案了。

刪除單個檔案樣本
//顯示版面設定
if($_SESSION["tb_forum_name"]==""){//記錄登陸名資訊
echo "";
exit;
}else{
?>
//查詢資料庫資料表資訊
if($page){
$page_size=6; //每頁顯示2條記錄
$query="select count(*) as total from tb_photo where up_id"; //從資料庫中讀取資料
$result=mysql_query($query);
$message_count=mysql_result($result,0,"total"); //擷取總的記錄數
$page_count=ceil($message_count/$page_size); //擷取總的頁數
$offset=($page-1)*$page_size;
$query=mysql_query("select * from tb_photo where up_id order by up_id desc limit $offset, $page_size");
while($myrow=mysql_fetch_array($query)){
?>
//這裡就表示圖片設定資訊其他資訊(略)
" width="40" height="40" />
//點擊刪除按鈕,根據其點擊的ID號發送的刪除頁面處理刪除這一條資料資訊
&&picpath=" class="STYLE1">刪除圖片資訊
//接收選擇ID號刪除相應的資料庫中的資料
$delete_id=$_GET[delete_id];//接收要刪除的ID號
$picpath='../'.$_GET[picpath];//刪除儲存在目錄中和資料庫同時儲存的圖片
$former = substr(strrchr($picpath,'/'),1);//設定儲存的圖片路徑
if(file_exists($picpath)){
unlink($picpath);//查詢處理圖片儲存的路徑資訊
}//CodeGo.net/
$query=mysql_query("delete from tb_photo where up_id='$delete_id'");//根據資料庫資訊查詢要刪除的資料ID
if($query==true){//如果存在資料就執行刪除
echo "";
}else{
echo "";
}//注意:table資訊(省略)
?>

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.