SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2004
    Location
    Fort Lauderdale
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    deleting file - without knowing its ext

    Hi,

    I have a directory with files:
    Code:
    images/articles/temp
    article_username_image1.gif
    article_username_image2.jpg
    article_username_image_graphics.jpg
    and
    Code:
    images/articles/temp/thumb
    article_username_image1_thumb.jpg
    article_username_image1_thumb.gif
    On the form, I have a checkbox next to the image. If the user select that checkbox and hits submit, I delete that image.

    Since I store two versions of the image. First in the temp directory and second in the thumb directory - how do I delete them both upon finding out the checkbox selected is to delete image1

    This is what I have, but I have to repeat the same code for all images that I want to delete. Is there a better version:
    PHP Code:
    if ( $_REQUEST['deleteImage1'] ) {
    $image_='image1';
    foreach ( 
    glob"$searchArticleDir*" ) as $filename ) {
    list(
    $article_$username_$image_num$ext_)= split('[_.]'basename($filename));

    if ( 
    $image_num == $image_ ) {
    $full_path$tempArticlePath   basename$filename );                            
    unlink$full_path );
    $bodytag      str_replace("temp""temp/thumb""$full_path");
    $bodytag2     str_replace($image_$image_."_thumb""$bodytag");
    unlink$bodytag2 );




    is there a better way to do this? I have a max of 4 images that can be deleted at once...

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im kinda sleepy, but im not seeing why your reading all the files in the directory

    functions are nice when you need to repeat an operation, so you dont have to write it out over and over

    PHP Code:
    <?php

    function delete_images($image_num) {
        
    $path 'images/articles/temp';
        
    unlink("$path/$image_num.gif");
        
    unlink("$path/$image_num.jpg");
        
    unlink("$path/thumb/$image_num"_thumb.gif");
        
    unlink("$path/thumb/$image_num"_thumb.jpg");
    }

    if (isset(
    $_REQUEST['deleteImage1'])) {
        
    delete_images('image1');
    }
    if (isset(
    $_REQUEST['deleteImage2'])) {
        
    delete_images('image2');
    }
    ?>
    if you always want to eliminate having to write out
    PHP Code:
    if (isset($_REQUEST['deleteImage1'])) {
        
    delete_images('image1');

    over and over, i can show you how to do that too by using a loop.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2004
    Location
    Fort Lauderdale
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL -you are right. What was I thinking???


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
  •