Jump to content

Archived

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

Mor0ninc

problem with unlink()

Recommended Posts

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.

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

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

Share this post


Link to post
Share on other sites
[!--quoteo(post=386139:date=Jun 21 2006, 08:17 AM:name=MoR0Ninc)--][div class=\'quotetop\']QUOTE(MoR0Ninc @ Jun 21 2006, 08:17 AM) [snapback]386139[/snapback][/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 :-)

Share this post


Link to post
Share on other sites
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.

[code]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");[/code]

I don't get it.

Share this post


Link to post
Share on other sites
I just tried getting straight to the point

[code]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");[/code]

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

if I change my code to this, then it will work
[code]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");[/code]
but why can't i get the variables to work in the unlink()?

Share this post


Link to post
Share on other sites
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.
[code]<form id="deleteConfirm_form" name="deleteConfirm_form" method="post" action="<?php unlink("uploads/" . $row_items['image']) ?>">[/code]

Share this post


Link to post
Share on other sites

×

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.