File copy and delete functions
1, modify the front-end code
Add get parameters to the copy and delete buttons respectively, so that they can send the file id ( file) and action type action
The code is as follows:
<?php <a href="?file=<?php echo $v['file_id'];?>&action=copy">復(fù)制</a>| <a href="?file=<?php echo $v['file_id'];?>&action=del">刪除</a>
2, get the get parameters for copy and delete operations
First obtain the get parameter file_id in index.php, then query the file table netdisk_file, and obtain the corresponding file information that needs to be copied and deleted.
Perform the copy operation of the file copy() and delete operation unlink()
The code is as follows:
<?php //獲取get參數(shù) $file_id=isset($_GET['file'])?intval($_GET['file']):0; //復(fù)制和刪除功能 $action=isset($_GET['action'])?trim($_GET['action']):""; if($action=="del"){ // unset(); $sql="select *from netdisk_file where file_id=$file_id"; $del_file=fetchRow($sql); unlink($del_file['file_save']); //刪除數(shù)據(jù)庫(kù)里的數(shù)據(jù) $sql="delete from netdisk_file where file_id=$file_id"; if(!mysql_query($sql)){ echo '數(shù)據(jù)庫(kù)數(shù)據(jù)刪除失敗'; }; }elseif ($action=="copy"){ $sql="select *from netdisk_file where file_id=$file_id"; $copy_file=fetchRow($sql); $filesavename=$copy_file['file_save']; if(file_exists("$filesavename.bak")){ echo '文件名沖突,復(fù)制失敗'; } if(!copy("$filesavename","$filesavename.bak")){ echo "復(fù)制失敗"; }else{ $file_copy_name=$copy_file["file_name"]; $file_copy_size=$copy_file["file_size"]; $file_copy_id=$copy_file["folder_id"]; $sql="insert into netdisk_file (file_name,file_save,file_size,file_time,folder_id) values('$file_copy_name.bak','$filesavename.bak',$file_copy_size,now(),$file_copy_id)"; if(!mysql_query($sql)){ unlink($uploadfile_save); echo "寫(xiě)入數(shù)據(jù)庫(kù)出錯(cuò)"; } } }
3, effect display
Copy display:
Click the page before copying:
Click to finish After copying:
Changes in the database:
Delete display:
Page before deletion:
Page after deletion:
##Corresponding database Changes have also occurred