unlink help please .

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    unlink help please .

    tried adding this to my delete option in my gallerys

    Code:
          $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 .
    Wapchat4u


    Topsites4u

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

    You may try sumthing like ds

    PHP Code:
    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

    Comment


      #3
      imageurl is generally like this .
      Code:
      ../usergallery/filename <<<< there
      so the file is in a separate folder
      Wapchat4u


      Topsites4u

      Comment


        #4
        Originally posted by nclemale36 View Post
        imageurl is generally like this .
        Code:
        ../usergallery/filename <<<< there
        so the file is in a separate folder
        if the paths are stored in a way like ds /usergallery/img.jpg
        PHP Code:
        $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

        Comment


          #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 Code:
          <?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.

          PHP Code:
           if (file_exists($file DOCROOT.'usergallery/image.jpg')) unlink($file); 

          Comment


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


            I'm Proud to be a Sri Lankan!

            Comment


              #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 .
              Wapchat4u


              Topsites4u

              Comment


                #8
                Originally posted by nclemale36 View Post
                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:
                PHP Code:

                $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()
                    
                }

                Comment


                  #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:
                  PHP Code:
                  $cwd getcwd();
                  chdir("images");
                  unlink($file);
                  chdir($cwd); 

                  Comment

                  Working...
                  X