Jump to content


Photo

problem with unlink()


  • Please log in to reply
4 replies to this topic

#1 Mor0ninc

Mor0ninc
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 June 2006 - 08:17 PM

I am setting up a place for myself to post personal items for sale. I can post a name, description, price, and a picture. The problem I am having is that I am able to upload the photo and update my database with it's path, but I cannot delete the file from my server when I delete the record from my database. I am using the unlink command.

if ((isset($_POST['hiddenID'])) && ($_POST['hiddenID'] != "")) {
  $dir = "uploads";
  $file = $row_items['image'];
  $remove = $dir . "/" . $file;
  unlink($remove) or die("Image could not be deleted");

That is the section that deals with the deletion of the file, I added the " or die("image could not be deleted");" so that I wouldn't have to re-create my test item everytime the file deletion failed. If I remove the "or die..." then everything works ok except it leaves the image file on the server, but still removes it's path from the database.

What i don't understand is that if I change "$file" to equal the actual image file name it works perfectly fine. That tells me that it is not the rights to the folder. I wouldn't think anything in my php.ini file is wrong seeing as it works when I type the filename in for "$file".

PHP 5.1.4
IIS 6.0
MySQL 5.0.18

#2 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 20 June 2006 - 08:50 PM

[!--quoteo(post=386139:date=Jun 21 2006, 08:17 AM:name=MoR0Ninc)--][div class=\'quotetop\']QUOTE(MoR0Ninc @ Jun 21 2006, 08:17 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What i don't understand is that if I change "$file" to equal the actual image file name it works perfectly fine. That tells me that it is not the rights to the folder. I wouldn't think anything in my php.ini file is wrong seeing as it works when I type the filename in for "$file".
[/quote]

I'm not sure what is causing your problem, but if I was you I'd be doing an 'echo $file'; and see what I get. Bet you don't get the file name which is what you are after, so go through your code until 'echo $file'; returns the correct file name. You can just comment out/amend bit's of your code until 'echo $file'; gives the correct file name and then you should be able to see the answer :-)


#3 Mor0ninc

Mor0ninc
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 June 2006 - 11:56 AM

Thanks for the response, I tried doing that to a few things that I thought may cause the issue. You are right though, it is not getting the file name, and I am not really sure why not. I even looked up some examples and found some laid out pretty much like mine.

I did an echo on $row_items['image'] and it retrieves the image name from the database, but when I do an echo on $file it comes back blank.

I tried taking the $file out of the picture and still nothing.

if ((isset($_POST['hiddenID'])) && ($_POST['hiddenID'] != "")) {
  $dir = "uploads";
  //$file = $row_items['image'];
  $remove = $dir . "/" . $row_items['image'];
  unlink($remove) or die("Image could not be deleted");

I don't get it.

#4 Mor0ninc

Mor0ninc
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 June 2006 - 01:02 PM

I just tried getting straight to the point

if ((isset($_POST['hiddenID'])) && ($_POST['hiddenID'] != "")) {
  //$dir = "uploads";
  //$file = $row_items['image'];
  //$remove = $dir . "/" . $row_items['image'];
  unlink("uploads/" . $row_items['image']) or die("Image could not be deleted");

but I can do
<?php echo "uploads/" . $row_items['image']; ?>
and I get "uploads/test.jpg"

if I change my code to this, then it will work
if ((isset($_POST['hiddenID'])) && ($_POST['hiddenID'] != "")) {
  //$dir = "uploads";
  //$file = $row_items['image'];
  //$remove = $dir . "/" . $row_items['image'];
  unlink("uploads/test.jpg") or die("Image could not be deleted");
but why can't i get the variables to work in the unlink()?

#5 Mor0ninc

Mor0ninc
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 June 2006 - 06:38 PM

I GOT IT!

I declared my $dir variable outside the IF statement.

and on the "action" of my form I added the unlink statement.

That worked but then I figured I really don't need the variables for my directory or file, so on my form I just used the variable for the query that was already created.
<form id="deleteConfirm_form" name="deleteConfirm_form" method="post" action="<?php unlink("uploads/" . $row_items['image']) ?>">






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users