Web Analytics Made Easy -
StatCounter unlink() is not deleting my file - CodingForum

Announcement

Collapse
No announcement yet.

unlink() is not deleting my file

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

  • unlink() is not deleting my file

    Hi Guys,

    I have a database and one of the columns contains links to images stored on my server. I have a cron job that filters through the database and deletes rows that are old. When a row is deleted, I also want to delete whatever image was stored in the 'image' filed of that row. I have the following code where the row is deleting fine, but the image is not getting deleted. Someone told me the image path used in unlink has to be relative which is why I am using substr()

    PHP Code:
    //AN EXAMPLE OF WHAT WOULD BE IN THE IMAGE FIELD: http://www.dealbind.com/images/dealimages/6369.jpg
    $findold "SELECT * FROM deals WHERE TIMESTAMPDIFF(HOUR, `insertdate`, NOW() ) > 2";
    $execute mysql_query($findold);
    while(
    $row mysql_fetch_array($execute)){
        
    $image $row['image'];
        
    $imagesub substr($image,0,23);
        if(
    $imagesub == "http://www.dealbind.com"){
            
    unlink(substr($image,23));
        }
        
    $dealid $row['dealid'];
        
    $delete "DELETE FROM deals WHERE dealid = '$dealid'";
        
    $executedelete mysql_query($delete);


  • #2
    Something looks iffy here:

    PHP Code:
        $image $row['image']; //Get image file path/name
        
    $imagesub substr($image,0,23); //Get part of it
        
    if($imagesub == "http://www.dealbind.com"){ //IF beginning is this domain
            
    unlink(substr($image,23)); //Delete file from URL PATH??!?!?!?!?!
        

    Supposing your file url is:
    http://www.dealbind.com/images/image1.jpg

    Now on your file system that could be completely different:
    /usr/<login>/public_html/images/image1.jpg
    OR
    c:/httpdocs/images/image1.jpg

    You can't rely on the file path being taken from the URL. You need to remember that you must always use file paths for the OPERATING SYSTEM. Relative paths are supported normally but you must be sure you get them correct OR use the full path.

    Where are the images stored on the disk? - What is the directory structure?
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      Yes I was using the relative file path - relative to this file:

      PHP Code:
          $image $row['image']; //Get image file path/name
          
      $imagesub substr($image,0,23); //Get part of it
          
      if($imagesub == "http://www.dealbind.com"){ //CHECK IF IMAGE IS HOSTED ON MY SERVER (some are not)
              
      unlink(substr($image,23)); //Delete relative path ex. /images/image1.jpg
          

      The images are stored in public_html/images/dealimages/image#.jpg

      I don't know what the directory structure is called. I use the standard cPanel that comes with hostgator if that helps?

      Comment


      • #4
        So where is the script itself located?
        "Tango says double quotes with a single ( ' ) quote in the middle"
        '$Name says single quotes with a double ( " ) quote in the middle'
        "Tango says double quotes ( \" ) must escape a double quote"
        '$Name single quotes ( \' ) must escape a single quote'

        Comment


        • #5
          Originally posted by tangoforce View Post
          So where is the script itself located?
          It is located in the home directory: public_html/filterold.php

          Comment


          • #6
            So your filepath should look like images/dealimages/image#.jpg

            Sometimes you need a / at the front other times you don't. If that doesn't work then try the full path (which you'll find in your control panel).
            "Tango says double quotes with a single ( ' ) quote in the middle"
            '$Name says single quotes with a double ( " ) quote in the middle'
            "Tango says double quotes ( \" ) must escape a double quote"
            '$Name single quotes ( \' ) must escape a single quote'

            Comment


            • #7
              Originally posted by tangoforce View Post
              So your filepath should look like images/dealimages/image#.jpg
              Yes, this is what my file path is. The unlink(substr($image,23)); yields /images/dealimages/2349.jpg

              This is why I am confused as to why it's not working.

              Comment


              • #8
                Ok, run this please and tell me the output:

                PHP Code:
                print substr($image,23) .'<br>';
                print (
                file_exists(substr($image,23))) ? 'File exists' 'No file found'
                Also note that you may need to remove that first slash.
                "Tango says double quotes with a single ( ' ) quote in the middle"
                '$Name says single quotes with a double ( " ) quote in the middle'
                "Tango says double quotes ( \" ) must escape a double quote"
                '$Name single quotes ( \' ) must escape a single quote'

                Comment


                • #9
                  OK. I have set up a test using the following code, and added your 'print' lines to it:

                  mycode:
                  PHP Code:
                          $image "http://www.dealbind.com/images/dealimages/0.jpg";
                      
                  $imagesub substr($image,0,23);
                      
                  unlink(substr($image,23));
                      print 
                  substr($image,23) .'<br>';
                      print (
                  file_exists(substr($image,23))) ? 'File exists' 'No file found'
                  result:

                  Code:
                  Warning: unlink(/images/dealimages/0.jpg) [function.unlink]: No such file or directory in /home/dealbind/public_html/test.php on line 13
                  /images/dealimages/0.jpg
                  No file found

                  Comment


                  • #10
                    Originally posted by inchecksolution View Post
                    Code:
                    Warning: unlink(/images/dealimages/0.jpg) [function.unlink]: No such file or directory in /home/dealbind/public_html/test.php on line 13
                    /images/dealimages/0.jpg
                    No file found
                    Have you tried removing that extra slash / ? - The one I said was the first slash at the beginning of your file path.
                    "Tango says double quotes with a single ( ' ) quote in the middle"
                    '$Name says single quotes with a double ( " ) quote in the middle'
                    "Tango says double quotes ( \" ) must escape a double quote"
                    '$Name single quotes ( \' ) must escape a single quote'

                    Comment


                    • #11
                      Originally posted by tangoforce View Post
                      Have you tried removing that extra slash / ?
                      I think that did the trick!! The test worked. Now I will implement it and see how it goes

                      THANKS!

                      Comment


                      • #12
                        No worries.

                        Understand why this was happening though? - your error message gave it away:

                        /images/dealimages/0.jpg [function.unlink]: No such file or directory in /home/dealbind/public_html/test.php

                        Add those together and you would have
                        /home/dealbind/public_html//images/dealimages/0.jpg
                        "Tango says double quotes with a single ( ' ) quote in the middle"
                        '$Name says single quotes with a double ( " ) quote in the middle'
                        "Tango says double quotes ( \" ) must escape a double quote"
                        '$Name single quotes ( \' ) must escape a single quote'

                        Comment

                        Working...
                        X