Page 1 of 1

Can't delete physical file on server...

Posted: Fri Dec 23, 2011 11:47 pm
by orbdrums
Hello all,

I am using a checkbox form to delete profile images but I can't seem to get the php code to remove the physical file from my server. The records are removed from my SQL table but I can't the actual image files to delete. Here is my code:

Code: Select all

<?php
header("Location: image-delete-form.php");
	session_start();

$objConnect = mysql_connect('127.0.0.1:8889','admin_ch') or die(mysql_error());
$objDB = mysql_select_db("Images");
$curr_mbr = $_SESSION['SESS_LOGIN'];

	for($i=0;$i<count($_POST["chkDel"]);$i++)
	{
		if($_POST["chkDel"][$i] != "")
		{
			$strSQL = "DELETE FROM $curr_mbr WHERE img_name = '".$_POST["chkDel"][$i]."' ";
			$objQuery = mysql_query($strSQL);
                        $del_file = "../uploader/uploaded_files/$_POST["chkDel"]";
			unlink($del_file);
		}
	}
mysql_close($objConnect);
?>
Thanks in advance for any help.

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 2:14 am
by Christopher
It is either a permissions problem or the path is incorrect. PHP runs as the same user that the web server.

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 2:24 am
by orbdrums
Thanks for the reply Christopher. I have attempted many syntax changes without success. Here is the the part that I don't understand, I can write the physical file to the disk, I can create and delete the "link" record from SQL, but I can't seem to unlink the file from the server. I think what is happening is the array that I create in my "form" is not being properly posted to my processor file. I may be way off base here, and it wouldn't be the first time, but I can't seem to use the same variables to display AND function. I'm very new to php, by the way.
Thanks,
Clark

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 2:46 am
by orbdrums
I'm going to post my form php and then the processor file that I use. I hope this helps.

Code: Select all

<?
	session_start();
?>
<html>
<head>
<title>Image Delete</title>
</head>
<body>
<script language="JavaScript">
	function onDelete()
	{
		if(confirm('Do you want to delete ?')==true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
</script>
<form name="frmMain" action="image-delete-proc.php" method="post" OnSubmit="return onDelete();">
<?
$objConnect = mysql_connect('127.0.0.1:8889','admin_ch') or die(mysql_error());

$curr_mbr = $_SESSION['SESS_LOGIN'];

$objDB = mysql_select_db("Images");
$strSQL = "SELECT * FROM $curr_mbr ORDER BY img_date";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="800" border="1">
  <tr>
    <th width="80"> <div align="center">Image Date </div></th>
    <th width="250"> <div align="center">Image Description </div></th>
    <th width="200"> <div align="center">Image Name </div></th>
    <th width="150"> <div align="center">Profile Image </div></th>

    <th width="50"> <div align="center">Delete </div></th>
  </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
    <td><div align="center"><?=$objResult["img_date"];?></div></td>
    <td><?=$objResult["img_desc"];?></td>
    <td><?=$objResult["img_name"];?></td>
    <td><div align="center"><img src="/uploader/uploaded_files/<?=$objResult["img_name"]?>" HEIGHT = "75" WIDTH = "125"></img><br /></div></td>

    <td align="center"><input type="checkbox" name="chkDel[]" value="<?=$objResult["img_name"];?>"></td>
  </tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<input type="submit" name="btnDelete" value="Delete">
</form>
</body>
</html>
And here is the processor file...

Code: Select all

<?php
header("Location: image-delete-form.php");
	session_start();

$objConnect = mysql_connect('127.0.0.1:8889','admin_ch') or die(mysql_error());
$objDB = mysql_select_db("Images");
$curr_mbr = $_SESSION['SESS_LOGIN'];

	for($i=0;$i<count($_POST["chkDel"]);$i++)
	{
		if($_POST["chkDel"][$i] != "")
		{
			$strSQL = "DELETE FROM $curr_mbr WHERE img_name = '".$_POST["chkDel"][$i]."' ";
			$objQuery = mysql_query($strSQL);
			$del_file = "/uploader/uploaded_files/".$_POST["chkDel"][$i]."";
			unlink($del_file);
		}
	}
mysql_close($objConnect);
?>
Thanks again for any help.
C

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 6:11 pm
by phpHappy
I didn't read past

Code: Select all

header("Location: image-delete-form.php");
because that is as far as you can go. You shouldn't go back to the form until you do the work.

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 6:21 pm
by orbdrums
The header() function has to be the first instruction in the file, if you're going to use it, otherwise it won't work. Besides, everything works properly except the unlink() function. The SQL record is removed but the physical file on the server remains. I want to unlink() the file when I am deleting the record from my table.

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 11:51 pm
by phpHappy
header() doesn't have to be the first thing in a file, but has to be the first output to work.
still, you shouldn't redirect until the work is done - you can catch stuff that way and know what is happening
remove ."" from the end of your file name

Re: Can't delete physical file on server...

Posted: Sat Dec 24, 2011 11:57 pm
by phpHappy

Code: Select all

$del_file = '/uploader/uploaded_files/'.$_POST['chkDel'][$i];