unlink help please .

Started on the old forum in September 2012 · view the original in the archive · old links redirect here

8 replies · 573 views · started 14 years ago · 1 min read

5 participants
nclemale36 Lead · 537 posts #1

tried adding this to my delete option in my gallerys

      $imageurl = mysql_fetch_array(mysql_query("SELECT imageurl, uid FROM dave_usergallery WHERE id='".$whoimage."'"));
      $delpath = "$imageurl[0]";
      unlink($delpath);
   
   // unlink("$imageurl[0]");

but am getting this error Warning: unlink() [function.unlink]: Unable to access have tried a few other variations as you can see.

all i want is to be able to delete the image file lol .

softwarefreak Senior · 349 posts #2

Well, the problem seems to be in the file path,

You may try sumthing like ds

if (file_exists($delpath))
{
   unlink($delpath);
}

I'm not sure how you have stored the paths in db :|

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

softwarefreak Senior · 349 posts #4

nclemale36 wrote: imageurl is generally like this .

../usergallery/filename <<<< there 

so the file is in a separate folder

if the paths are stored in a way like ds /usergallery/img.jpg

$delpath = $_SERVER['DOCUMENT_ROOT'] . "/usergalllery/img.jpg";
if (file_exists($delpath))
{
   unlink($delpath);
}

Try this

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

CreativityKills Lead · 833 posts #5

I always try to have a bootstrap.php file in the document root of my site and then include that in all php pages. In the bootstrap file i always add something like

<?php
defined('DOCROOT') or define('DOCROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);

So in any other php file where the bootstrap file is included, you can easily know how to get to the path you want to delete.

 if (file_exists($file = DOCROOT.'usergallery/image.jpg')) unlink($file);
nimeshka Developer · 180 posts #6

It is with the file path I think...How if you can first echo the path and check whether it is correct??

External image (may be gone)

I'm Proud to be a Sri Lankan!

nclemale36 Lead · 537 posts #7

found this problem in another thread .the code i had works perfectly except it needed to be above the delete from gallery database code as if the database is deleted first then the unlink aint got a url to unlink from .

CreativityKills Lead · 833 posts #8

nclemale36 wrote: found this problem in another thread .the code i had works perfectly except it needed to be above the delete from gallery database code as if the database is deleted first then the unlink aint got a url to unlink from .

A nugget of knowledge you should also take with you. Lets follow your work flow for a minute. When you put the file delete logic ahead of the database delete logic, imagine what happens when for some reason the database entry does not get deleted.

Its better to:

$sql_results = array(/* the result from database */);
$image_path = $sql_results['image_url'];

if (mysql_query("DELETE ...."))
{
    try
    {
        unlink($image_path);
    }
    catch (Exception $e)
    {
        // Do something here. May be log the error in $e->getMessage()
    }
}
something else Fellow · 2,296 posts #9

With some server settings you will find unlink will not work unless you change the working directory to the one that the file is in. if you find you have this problem (unlink doesn't work) use something like:

$cwd = getcwd();
chdir("images");
unlink($file);
chdir($cwd);

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close