Log in

View Full Version : Unlink to Delete File From Server



toad78
02-04-2009, 09:49 PM
I'm having trouble deleting a filename from a server after a record deletion is set. Can someone take a look at this and tell me what I'm doing wrong? The record on the db deletes, but not the file on the server.


if ((isset($_POST['empl_id'])) && ($_POST['empl_id'] != "")) {
$deleteSQL = sprintf("DELETE FROM empl_dnlds WHERE empl_id=%s",
GetSQLValueString($_POST['empl_id'], "int"));

$image_path = '../../../info/docs/employment/';

if ((isset($_POST['empl_id'])) && file_exists($image_path.$_POST['empl_id'])) {
unlink($image_path.$row_getApp['empl_dnld_fn']);
}
mysql_select_db($database_wvgsadmin, $wvgsadmin);
$Result1 = mysql_query($deleteSQL, $wvgsadmin) or die(mysql_error());

$deleteGoTo = "empl_app_list.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}

$colname_getApp = "-1";
if (isset($_GET['empl_id'])) {
$colname_getApp = $_GET['empl_id'];
}

Nile
02-04-2009, 10:40 PM
Are you sure your doing it right?

For example: /examples/page1/page2/page3/page4/page5

Now, I want to get back to page3, I would do:


../ (page4) ../ (page3)

So for example, in this case your going to page 2, then going to info/docs/empoyment/$file.

toad78
02-04-2009, 11:21 PM
Are you sure your doing it right?

I've found out the pickle:
I forgot to add a hidden field in my form for the filename. I've ran it through a few tests and it works great now. WOOHOO!

But now I have the problem of trying to delete duplicate files. For example, say someone inserts a new record with the same filename. I have the insert form coded to where if the filename is a duplicate, create a date/timstamp on it. Well, let's say they want to delete the file with the record, but the record has a date/timestamp on it. For some reason, my delete form doesn't delete the date/timestamp file that is related to it. It deletes the record fine, just not the file.

I thought maybe I should change the "unlink($image_path.$_POST['empl_dnld_fn']);" to "unlink($image_path.time($_POST['empl_dnld_fn']));" IF THE FILE HAS A DATE/TIMESTAMP. But I don't know if this would work. Do you have any suggestions?


if ((isset($_POST['empl_id'])) && ($_POST['empl_id'] != "")) {
$deleteSQL = sprintf("DELETE FROM empl_dnlds WHERE empl_id=%s",
GetSQLValueString($_POST['empl_id'], "int"));

$image_path = '../../../info/docs/employment/';
if (isset($_POST['empl_dnld_fn']) && file_exists($image_path.$_POST['empl_dnld_fn'])) {
unlink($image_path.$_POST['empl_dnld_fn']);
}

mysql_select_db($database_wvgsadmin, $wvgsadmin);
$Result1 = mysql_query($deleteSQL, $wvgsadmin) or die(mysql_error());

$deleteGoTo = "empl_app_list.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}

Nile
02-05-2009, 12:50 AM
I don't completely get what you mean...
Can you be more descriptive? If you need to determine what time the file has been created, use:


<?php
$filename = $_POST['file'];
echo date ("m/d/y h:m", filemtime($filename));
?>

toad78
02-05-2009, 06:16 AM
What I have is a file upload form that if the file is a duplicate being uploaded, it creates a date/timestamp added to the filename using 'PHPUPLOAD_DIR.time().$file);'

In my delete record form, I have the selected record to delete with a filename associated with it. If that filename was a duplicate that has the date/timestamp on it, for some reason my delete form is not deleting the duplicate file from the server that is associated with that specific record.

I hope this helps!

Nile
02-05-2009, 10:32 PM
What do you mean by 'duplicate', do you mean:


Folder1 --
--Index.php
--Index.php

toad78
02-05-2009, 10:59 PM
More like:
contract.pdf
2009-02-05-225853contract.pdf (duplicate)

Nile
02-05-2009, 11:33 PM
Ahh... I see:
(This is very dangerous):


<?php
$unlink_vars = glob("*".$_POST['file']);
foreach($unlink_vars as $file){
unlink($file);
}
?>


WARNING: Using the glob() and unlink() function together can sometimes result in bad things such as deleting all the files.
MAKE SURE THAT $_POST['file'] ISN'T BLANK!

Now - there are some bugs. For example:


Folder1--
--index.php
--pic.png
--picture.png

If someone types 'png', it'd delete pic.png and picture.png.

toad78
02-10-2009, 10:52 PM
I decided to take a different route and got it working in a different way.

But I have another question:
I am using the unlink function to delete a related filename in a record. BUT, what if I have a specific file that I don't want deleted?

For example, in my insert form, I have a list of images from a folder. Whatever is picked for that record is linked. But what if it is a file called 'nophotoavailable.jpg'? If I have a record that doesn't have a photo and they select this particular file and eventually want to delete the record, but I don't want this file deleted, how do I construct unlink to work in this case?

I currently have this:

$image_path = '../../../info/docs/employment/';
if (isset($_POST['empl_dnld_fn']) && file_exists($image_path.$_POST['empl_dnld_fn'])) {
unlink($image_path.$_POST['empl_dnld_fn']);
}

and am not sure how I don't want file 'nophotoavailable.jpg' (which is coming from 'empl_dnld_fn') to NOT be deleted if the record gets deleted.

I appreciate any insight!

Thank you!
toad78