dare87 Posted November 8, 2007 Share Posted November 8, 2007 I have a form on my site that allows people to send me a message and a file if the choose. I get the message via email but the file is upload to the server and renamed to a tmp name. I also have all the information about the file uploaded to a db. I then have a section on my site where it lists all the files that were uploaded and gives me a download link. I also have it so I can delete the file. But all it is really doing is deleting the information from the db. is there a way to make it delete the db info and the physical file? Here is what I have. <?php // Include the PHP script that contains the session information. include('../includes/session_admin.php'); // Get the user id from the URL. $id = $_GET['id']; // Connect to the database. require_once ('../../../xsmysql_connect.php'); // Set up the query. $query = "DELETE FROM uploads WHERE upload_id=$id"; // Run the query. $results = @mysql_query ($query); // Reload the page. $url = '../attachments.php'; header("Location: $url"); ?> When the files are uploaded they are placed in /uploads/ and given the tmp name of there upload_id If you have questions let me know. Thanks for all the help D Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/ Share on other sites More sharing options...
adam291086 Posted November 8, 2007 Share Posted November 8, 2007 you need to use the unlink() function for example unlink("../adam/photos/" . $photo_location) where $photo_location is my file name Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387409 Share on other sites More sharing options...
beyzad Posted November 8, 2007 Share Posted November 8, 2007 I have a form on my site that allows people to send me a message and a file if the choose. I get the message via email but the file is upload to the server and renamed to a tmp name. I also have all the information about the file uploaded to a db. I then have a section on my site where it lists all the files that were uploaded and gives me a download link. I also have it so I can delete the file. But all it is really doing is deleting the information from the db. is there a way to make it delete the db info and the physical file? Here is what I have. <?php // Include the PHP script that contains the session information. include('../includes/session_admin.php'); // Get the user id from the URL. $id = $_GET['id']; // Connect to the database. require_once ('../../../xsmysql_connect.php'); // Set up the query. $query = "DELETE FROM uploads WHERE upload_id=$id"; // Run the query. $results = @mysql_query ($query); // Reload the page. $url = '../attachments.php'; header("Location: $url"); ?> When the files are uploaded they are placed in /uploads/ and given the tmp name of there upload_id If you have questions let me know. Thanks for all the help D http://us2.php.net/manual/en/function.delete.php But you may need to change the permission of the file first. Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387410 Share on other sites More sharing options...
dare87 Posted November 8, 2007 Author Share Posted November 8, 2007 where in the code would I put the unlink? Sorry I am somewhat new to php and sql Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387412 Share on other sites More sharing options...
beyzad Posted November 8, 2007 Share Posted November 8, 2007 where in the code would I put the unlink? Sorry I am somewhat new to php and sql $query="select * from upload where upload_id=$id"; $result=mysql_query($query); $rows=mysql_fetch_assoc($result); $file_name=$rows[file]; // If file is your filed name unlink ( PATH $file_name ); Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387414 Share on other sites More sharing options...
dare87 Posted November 8, 2007 Author Share Posted November 8, 2007 like this? <?php // Include the PHP script that contains the session information. include('../includes/session_admin.php'); // Get the user id from the URL. $id = $_GET['id']; // Connect to the database. require_once ('../../../xsmysql_connect.php'); // Set up the query. $query = "DELETE FROM uploads WHERE upload_id=$id"; // Run the query. $results = @mysql_query ($query); $rows=mysql_fetch_assoc($result); $upload_id=$rows[file]; // If file is your filed name unlink ( /uploads/$upload_id ); // Reload the page. $url = '../attachments.php'; header("Location: $url"); ?> Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387416 Share on other sites More sharing options...
adam291086 Posted November 8, 2007 Share Posted November 8, 2007 ok, is your database storing the name of the file? Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387421 Share on other sites More sharing options...
dare87 Posted November 8, 2007 Author Share Posted November 8, 2007 yes upload_id is what the file is named Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387423 Share on other sites More sharing options...
rajivgonsalves Posted November 8, 2007 Share Posted November 8, 2007 should be unlink ( "/uploads/$upload_id" ); Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387429 Share on other sites More sharing options...
dare87 Posted November 8, 2007 Author Share Posted November 8, 2007 should be unlink ( "/uploads/$upload_id" ); when i tried that it gave me Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/.../public_html/xs/auxiliary/delete.php on line 18 Warning: unlink(/uploads/) [function.unlink]: No such file or directory in /home/.../public_html/xs/auxiliary/delete.php on line 21 Warning: Cannot modify header information - headers already sent by (output started at /home/nayliner/public_html/xs/auxiliary/delete.php:18) in /home/.../public_html/xs/auxiliary/delete.php on line 25 Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387434 Share on other sites More sharing options...
adam291086 Posted November 8, 2007 Share Posted November 8, 2007 right so what you need to do before the delete from the data base is $sql= "Select upload_id FROM uploads WHERE upload_id=$id"; $result = @mysql_query($sql) or die("Error retrieving records: " . mysql_error()); while ($row = mysql_fetch_array($result)){ $photo_location = $row['upload_id']; } if (@unlink("YOU NEED TO PUT THE FILE DIRECTORY DETAILS HERE ../what ever" . $photo_location)){ } else { die("Error deleting photo"); } Then put in the delete from the database statement. Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387435 Share on other sites More sharing options...
dare87 Posted November 8, 2007 Author Share Posted November 8, 2007 It works!!! thanks all Thanks a million - Adam... <?php // Include the PHP script that contains the session information. include('../includes/session_admin.php'); // Get the user id from the URL. $id = $_GET['id']; // Connect to the database. require_once ('../../../xsmysql_connect.php'); $sql= "Select upload_id FROM uploads WHERE upload_id=$id"; $result = @mysql_query($sql) or die("Error retrieving records: " . mysql_error()); while ($row = mysql_fetch_array($result)){ $photo_location = $row['upload_id']; } if (@unlink("../uploads/" . $photo_location)){ } else { die("Error deleting photo"); } // Set up the query. $query = "DELETE FROM uploads WHERE upload_id=$id"; // Run the query. $results = @mysql_query ($query); // Reload the page. $url = '../attachments.php'; header("Location: $url"); ?> Link to comment https://forums.phpfreaks.com/topic/76491-solved-file-delete-help/#findComment-387439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.