Results 1 to 5 of 5

Thread: How do I run a delete images script?

  1. #1
    Join Date
    Oct 2009

    How do I run a delete images script?


    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 
    4.    $image_name 
    $row['image_name'];//assume that this is the image_name field from your database 
    //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!'
    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());
    3. $odd 
    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:
    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

    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
    to check if $dir is correct, do the following if you already haven't done so.

    $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
    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
    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); 
    '<p>Your image has been deleted.<br /><a href="gallery.php">Return to Delete Images</a></p>';

    '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
    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.


    I know my main problem lies with
    PHP Code:
    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.


    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


    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.23731 seconds
  • Memory Usage 2,921KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (10)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates