Jump to content

Archived

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

digipit

unlink Command HELP!!!

Recommended Posts

[b]Here's the code I've got for deleting a record in the MySQL database as well as the image associated with it on the server. The portion that deletes from the database is working perfectly, but I can't get it to delete the image off the server. This is a picture gallery I've created for a client. Getting it to delete the actual image is the last thing keeping me from finishing the project. All and any help is appreciated.[/b]

[!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]<?
$db_user_name = "*****";
$db_database_name = "****";
$db_password = "****";

$picture_id = $_GET['picture_id'];
$picture_name = $_GET['picture_name'];
$picture_url = $title . '.' . $_GET['picture_url'];

mysql_connect( 'localhost', $db_user_name, $db_password) or die('<br>Failed connect: ' . mysql_error());

mysql_select_db($db_database_name) or die('<br>Failed database: ' . mysql_error());

mysql_query("DELETE FROM pg_gallery WHERE picture_id = $picture_id") or die('<br>Failed query: ' . mysql_error());

mysql_close();

unlink('../../images/gallery/'.$picture_url);

echo 'File removed successfully.';
?>[!--colorc--][/span][!--/colorc--]

Share this post


Link to post
Share on other sites
The code syntax looks fine, but the relative path that you are using with unlink might be whats giving you trouble. Do you have delete privileges to "../../images/gallery/" directory?

Share this post


Link to post
Share on other sites
I guess I do. I am the admin, but I've seen where others said to "chmod" it to 777. The folder itself is set to 777 but the individual images are not. I'm not sure how to go about doing this. I've also tried putting in a file directory path for the server but it still gives me fits. Any thoughts? Thanks.

Share this post


Link to post
Share on other sites
Should you be using "$picture_name" instead of "$picture_url"?
What is the contents of those vars?

Share this post


Link to post
Share on other sites
picture_name is what the user adds in as a caption for the image. picture_url is the column in the db containing the actual name of the file (i.e. myimage.jpg). That's what I'm trying to delete and running into a world of trouble with it.

Share this post


Link to post
Share on other sites
I still am concerned about the relative path. Try something like this to check if you really have the correct filename:
[code]
$tempvar = '../../images/gallery/'.$picture_url);
if (file_exists($tempvar))
{
echo $tempvar.' is a valid file';
}
else
{
echo $tempvar.' does not exist';
}
[/code]

Share this post


Link to post
Share on other sites
This is what I got: "../../images/gallery/ is a valid file". The weird thing is that it didn't show the file name though. Is there something wrong with the variable? Thanks for your help!

Share this post


Link to post
Share on other sites
$picture_url is an empty var, so you were trying to unlink a directory. What I would do is echo the contents of all of the vars related to the creation of the $picture_url variable, like $title and $_GET['picture_url']. You could insert this code at various places in your code to try to find which variable is not working.
[code]
echo  '<PRE>';
print_r($GLOBALS);
echo '</PRE>';
[/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.