www.webdeveloper.com
Results 1 to 5 of 5

Thread: How do I run a delete images script?

  1. #1
    Join Date
    Oct 2009
    Posts
    13

    How do I run a delete images script?

    Hello,

    I have what may be an itty-bitty dumb question, but Iíve been at this for two days and Iím kind of stuck. I donít even know what key words to Google any more. Iím trying to create an option on a gallery page that allows the user to delete an image and thumbnail from their server and delete data from a MySQL database (in this case the MySQL row contains the image name which has been rewritten to an image_id that auto increments and corresponds with the number given to each image/thumbnail combo uploaded to their respective directories).

    I found this code which looks perfect:

    PHP Code:
    1.    $img_dir 'image_directory_name/'
    2.    $img_thmb 'thumbnail_directory_name/';// if you had thumbnails 
    3.      
    4.    $image_name 
    $row['image_name'];//assume that this is the image_name field from your database 
    5.      
    6.    
    //unlink function return bool so you can use it as conditon 
    7.    if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){ 
    8.        //assume that variable $image_id is queried from the database where your image record your about to delete is... 
    9.        $sql "DELETE FROM table WHERE image_id = '".$image_id."'"
    10.        $qry mysql_query($sql); 
    11.    }else{ 
    12.       echo 'ERROR: unable to delete image file!'
    13.    
    I have saved it as delete_images.php, updated my directories and swapped $row[Ďimage_idí] in for $row[Ďimage_nameí].

    Now for my problem; I canít seem to find anywhere that tells me how to connect this script with a link on the previous page (gallery.php) which is printing all available image_ids into src html tags so the user can view them.

    PHP Code:
    1. $result mysql_query('SELECT * FROM images') or die(mysql_error());
    2. 
    3. $odd 
    true;
    4.  while ($rows mysql_fetch_array($result)) {
    5.  echo ($odd == true) ? '<tr class="odd_row">' '<tr class="even_row">';
    6.  $odd = !$odd
    7.  extract($rows);
    8.  echo '<td><img src="' $thumbdir '/' $image_id '.jpg"></td>';
    9.  echo 'Options[<a title="Delete File" href="javascript:;" 
    10. onClick="cf=confirm(\'Are you sure you want to delete?\');if (cf)
    11. window.location="delete_images.php?action=gallery&image_id="; 
    12. return false;">DELETE</a>]'
    ;
    12. }?> 
    I know my main problem lies with
    PHP Code:
    delete_images.php?action=gallery&image_id
    and I just donít know how to indicate that based on whatever the current id is, I want the delete_images.php page to run the unlink function using that same $image_id. I may have screwed this up somewhere, because php isnít my strong point, but I would really appreciate any help or even tutorials on how delete images from a server. If I could figure out how the two pages connect, I think I can troubleshoot myself through any problems on the delete_images.php page.

    Thank you!

  2. #2
    Join Date
    Oct 2009
    Posts
    13

    Okay Update

    Fixed the gallery page with this bit:
    PHP Code:
    echo '<td><a href="delete_images.php?file=' $image_id '.jpg">' 'DELETE</a></td>'
    and changed the directory in the delete_images.php to an absolute path for the directory:
    PHP Code:
    $dir $_SERVER['DOCUMENT_ROOT']. '/CP/' 'images/'
    So now we're down to this unlink error message:
    Warning: unlink(/home/mysite/public_html/CP/images/10.jpg) [function.unlink]: No such file or directory in /home/mysite/public_html/CP/delete_images.php on line 34
    ERROR: unable to delete image file!


    What I don't understand is why? I mean the file (10.jpg) is located in the folder 'images' which is in the folder 'CP' and the root is right. What else is wrong with the directory path for it to spit this error out at me?
    Last edited by Ayla; 05-30-2010 at 03:41 PM.

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    to check if $dir is correct, do the following if you already haven't done so.

    Code:
     
    $dir = $_SERVER['DOCUMENT_ROOT']. '/CP/' . 'images/';
     
    echo $dir; die();
    But imo a better way to build the path to the delete file is something like:
    PHP Code:
    $dir '/CP/images/';   //starting with a / means from your site's home directory
     
    $file2del $dir.$filename2delete

  4. #4
    Join Date
    Oct 2009
    Posts
    13
    I did check the directory. It looks legit. It ends right in the images folder where I need the $image_id to pull from.

    Are you saying to sub:
    PHP Code:
    $file2del $dir.$filename2delete
    for
    PHP Code:
    $image_name $row['image_name']; 
    Because I did end up changing that part to:
    PHP Code:
    $image_num $_GET['file']; 
    So that it could pull from the link on the previous page. So when you say $filename2delete, that would be my $image_id (because that's what the field is called in the mysql database).

    I've been messing with this all day, so now I have:
    PHP Code:
    $dir $_SERVER['DOCUMENT_ROOT']. '/CP/images/';
    //echo $dir; die();
    $thumbdir $dir 'thumbs/';

    //$image_num = $row['image_id'];//assume that this is the image_name field from your database 
    $image_num $_GET['file'];  
     
    //unlink function return bool so you can use it as conditon 
    if(unlink($dir.$image_num) && unlink($thumbdir.$image_num))

       
    //assume that variable $image_id is queried from the database where your image record your about to delete is... 
        
    $sql "DELETE FROM images WHERE image_id = '".$image_id."'"
        
    $qry mysql_query($sql); 
        
        print 
    '<p>Your image has been deleted.<br /><a href="gallery.php">Return to Delete Images</a></p>';
    }
    else

       echo 
    'ERROR: unable to delete image file!'

    I guess I'm just not quite sure where to apply your recommendation?

  5. #5
    Join Date
    Mar 2010
    Posts
    2,803
    No I wan't suggesting you substitute my code for yours.

    My code was just an example of how you could create a path name to a file without using $_SERVER['DOCUMENT_ROOT'] in case that was causing the problem.

    With

    I know my main problem lies with
    PHP Code:
    delete_images.php?action=gallery&image_id=
    you just need to do some basic Debugging 101:

    First you need to build the correct query string paramaters to attach to the url. You should be able to google the info if you need to.

    Then:

    1) in delete_images.php make sure the values of the passed parameters action and image_id are correct using echo statements or a debugger.

    2) use echo or a debugger to check the values of

    $dir.$image_num
    $thumbdir.$image_num

    The database record will be deleted only if both unlink() functions return a value of true.




Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles