Jump to content

[SOLVED] file delete help


dare87

Recommended Posts

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

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.

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 );

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");
?>

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

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.

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");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.