Unlink image array not working? Stumped!

Ok I have tested my script. I have an array which contains two images. I break and echo the array out and it holds the correct image names. Am I using the correct path to the folder containing the images? I use …/product_images to go up one folder. I am stumped. The record gets deleted from the database but the images remain in the folder on my localhost.

The thing that really stumps me is I am getting two errors so it does see the two images in the array and does try to delete them.

errors are as follow: Warning: Unlink failed (No such file or directory) in c:\apache\htdocs\hybo\siteadmin\addproduct.php on line 290

Warning: Unlink failed (No such file or directory) in c:\apache\htdocs\hybo\siteadmin\addproduct.php on line 290

Code included. Thanks

        if($_POST[s2] == "Delete" )
        {
                mysql_query("delete from products where ItemID = '$_POST[ItemID]' ") or die(mysql_error());
                        $ImageStr = $_POST[Image];    /*echo "$ImageStr"; break;*/
                        if(!empty($ImageStr))
                        {
                                $DeleteImages = explode("|", $ImageStr);
                                while(list(,$vi) = each($DeleteImages))
                                {
                                        unlink("../product_images/$vi");

                                        $mi++;
                                }

                        }
        }
}

Am I using the correct path to the folder containing the images?

I don’t know.
Try displaying the image in your code, so you can see if the path is correct.

And maybe this user comment in the php manual may be of help.

Ok I fixed it and got it working 100%. The odd thing is I had to move the MySql query after I delete the files but that didn’t work unless I added the file type? I had to add .jpg here is the fixed script. Is there anyway I can save the file with the extension at the end of the file name? I can only put one file type extension such as jpeg in my script but what if the file isn’t a jpg? Can anyone tell me how I can save the image name with the extension?

        if($_POST[s2] == "Delete" )
        {
                $ImageStr = $_POST[Image];    /*echo "$ImageStr"; break;*/

                        if(!empty($ImageStr))
                        {
                                $DeleteImages = explode("|", $ImageStr);
                                while(list(,$vi) = each($DeleteImages))
                                {
                                        unlink("../product_images/$vi.jpg");

                                }

                        }
              mysql_query("delete from products where ItemID = '$_POST[ItemID]' ") or die(mysql_error());
        }
}

Please take a look at [fphp]mysql_real_escape_string[/fphp] to sanitize user input.

Right now it’s pretty easy to delete all your images at once with a simple POST.

Ok I figured out how to attach the file extension onto the name when I upload the image and now the script is running 100% and I do not have to hard code any file extension. Thanks for the help.

The only user that will be inputting any info is the site admin. I highly doubt he himself would want to harm his own website. When I have front end users that put in any input I know how to protect the database fine thanks for the advice though.

Ok being you are so smart tell me how you can delete all my images at once with one post?? I am really curious now that you said "

"

I’m not gonna post that in public. Check your private messages in a minute or so :slight_smile: