SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    deletion problem

    Hi everyone - here's today's query for you!

    I'm fiddling with a script to try to crowbar it into doing what I want but something's not working right.

    It's within a form which updates a db with text and uploads or deletes a photo (thumbnail and larger version) as necessary.

    Here's the code:

    This is the link under the photo that should allow deletion if clicked:

    PHP Code:
    echo '<a href="?action=deletepic&amp;thumb='$photoname .'&amp;bigimage=' $bigfilename .'"> 
    ($photoname and $bigfilename both produce paths e.g. ../thumbs/photo.jpg)

    Here's the deletion bit itself:
    PHP Code:
    ...
    if (
    $deletePic)
    {
       
    $thumb_to_delete $_GET['thumb'];
       
    $image_to_delete $_GET['bigimage'];

          if (@
    unlink($thumb_to_delete)) // remove thumb
          
    {
             
    $message['thumb_gone'] = 'Thumbnail removed';
          } else {
             
    $error['thumb_not_gone'] = 'Thumbnail could not be removed';
          }

          if (@
    unlink($image_to_delete)) // remove thumb
          
    {
             
    $message['image_gone'] = 'Image removed';
          } else {
             
    $error['image_not_gone'] = 'image could not be removed';

    }
    ... 
    Currently it's just not deleting....
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Are you using the full paths the images? It appears you're not.

    Edit: Doh, just seen "($photoname and $bigfilename both produce paths e.g. ../thumbs/photo.jpg)". Oops.

    Try adding a file_exists check there too, for sanity's sake. If it returns true and your still not removing the file, chances are it's down to permissions.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Echo the values of $thumb_to_delete and $image_to_delete to see if they are received correctly.
    Get rid of those @ in front of the unlink, so you might see some error messages

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think POST ist better than GET to delete something. I would use POST. For example:
    Code:
    <form action="..." method="post">
       <input type="hidden" name="thumb" value="$photoname" />
       <input type="hidden" name="bigimage" value="$bigfilename" />
       <input type="submit" value="delete" />
    </form>
    Why POST? Because of browser-favorites and search-engines.

    Also add error_reporting(E_ALL|E_STRICT); for testing at the beginning of your script.

  5. #5
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone - I'll give it a go and report back.
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  6. #6
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still having problems!

    I've reduced it to one image for the moment - just until it's working.

    Currently got:

    PHP Code:
    if ($deletePic)
     {
     
    $file = ($_server['DOCUMENT_ROOT'].$_GET['thumb']);

     
    $fh fopen($file'w') or die("can't open file");
     
    fclose($fh);
     
    unlink($file);
     } 
    The link under each photo and within a form is still the same:

    PHP Code:
    echo '<a href="?action=deletepic&amp;thumb='$photoname .'&amp;bigimage=' $bigfilename .'"> 
    I tried it in the same directories with the basic unlink script and hard-coded photo name and it worked so I'm guessing that would rule out permissions? The variable $photoname is producing the correct string on hover.

    Thanks!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Give this a whirl, I sense we should be using 'realpath', but lets get this out of the way.
    PHP Code:
    <?php
    #delete.php
    if(false === empty($_GET['thumb']))
    {
        if(
    true === file_exists($_GET['thumb']))
        {
            
    unlink();
        }
        else
        {
            
    trigger_error(
                
    sprintf(
                    
    "File: '%s' does not exist.",
                    
    $_GET['thumb']
                ),
                
    E_USER_ERROR
            
    );
            exit;
        }
    }
    header('Location: http://www.somesite.com/listImages.php');
    exit;
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Anthony

    Thanks for helping out with this.

    I'm still having no joy - no error messages either... When the link is clicked the whole form disappears but displays no errors.

    This chunk of code is set within a much larger script. The script is a series of text fields used to add text to a db plus an image upload script. Under all that, within the form is a list of the current images and under each image is the delete link.
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •