www.webdeveloper.com
Results 1 to 6 of 6

Thread: Remove image from server/ ftp with php

  1. #1
    Join Date
    Jun 2014
    Posts
    6

    Unhappy Remove image from server/ ftp with php

    Hi everyone, its since 2 days i'm stuck with a little problem that i cant seem to find.

    I wrote this code to delete an image from the ftp when a link is clicked,
    used unlink to remove the file but nothing happens it just reloads the page and the image is still in place.

    Could someone be so kind to guide help me? Thank you

    Code:
    function supprimerImage(id){
    	$('an_photo_'+id).hide('fast');
    	mettreEnSession ("an_photo_"+id, "unset", "reload");	
    }
    PHP Code:
    if((isset($_GET['image']))&&($_GET['image']=='suppression')&&(isset($_GET['pic']))&&(!empty($_GET['pic']))){
            
            
    $dirpicsupp=opendir("../img/annonce/".$pathImg."/".substr($_GET['id'], 01)."/");
            while(
    $picsupp readdir($dirpicsupp))
            {
                if(
    ereg("^".$_GET['id']."-".$_GET['pic'], $picsupp)==true)
                {
                    
    unlink($_SERVER['DOCUMENT_ROOT'].'../img/annonce/'.$pathImg.'/'.substr($_GET['id'], 01).'/'.$picsupp);
                }
            }
            
    closedir($dirpicsupp);
            
            echo 
    '<div class="supp-annonce">
            <p>IMAGE SUPPRIMEE !</p>
            <p><img src="http://www.particimmo.com/img/validation_selection.png" width="40" height="40" alt="" /></p>
            </div>
            <meta http-equiv="Refresh" content="1; URL=annonce_modifier2.php?id='
    .$_GET['id'].'&t='.$_GET['t'].'">';
        } 
    Upload is working fine

    PHP Code:
    <?php
                
    for ($i=1$i <= $countpic+1$i++)
                {
                    echo 
    "<tr>";
                        echo 
    "<td>";
                            if (
    $i == $countpic+1){
                                echo 
    "<img src='../img/img_liste_vierge.png' width='234' height='176'/>";
                            }else{
                                echo 
    "<img src='"."../img/annonce/".$pathImg."/".substr($_GET['id'], 01)."/".$_GET['id']."-".$i.".jpg' width='234' height='176'/>";
                            }
                        echo 
    "</td>";
                        echo 
    "<td style='vertical-align:middle; text-align:center'>";
                            echo 
    "  <div id='photo_upload".$i."' style='float:left; height:190px; width:296px;background-size:190px; background:none'></div>";
                        echo 
    "</td>";
                        echo 
    "<td style='vertical-align:middle; text-align:center'>";
                            echo 
    "  <form id='uploadform".$i."' class='".$i."' method='post'
                                    enctype='multipart/form-data'
                                    action='fonction/image-upload.php'>
                                    <input type='hidden' value='"
    .$i."' name='i'/>
                                    <div class='fileinputs'>
                                        <input name='file' class='file' id='file' size='27' type='file' />
                                    </div>    
                                    <div id='uploadbtn"
    .$i."' class='div_bouton texte_bg_bleu' style='width:70px;margin-bottom:5px'>Charger</div>
                                    <iframe id='target_iframe"
    .$i."' name='target_iframe' src='' style='width:0;height:0;border:0px'></iframe>
                                    </form>
                                    <input type='hidden' id='src1"
    .$i."' value='img/annonce/".$pathImg."/".substr($_GET['id'], 01)."/".$_GET['id']."-".$i.".jpg' />
                                    <input type='hidden' id='src2"
    .$i."' value='' />
                                    <div id='valider"
    .$i."' alt='".$i."' class='div_bouton texte_bg_violet' style='width:70px;display:none'>Valider</div>";
                        echo 
    "</td>";
                        echo 
    "<td align='center'>";
                            if (
    $i==1){
                                echo 
    "La photo principale ne peut être que modifiée.";
                            }elseif (
    $i==$countpic){
                                echo 
    "<a href='annonce_modifier2.php?image=suppression&id=".$_GET['id']."&t=".$_GET['t']."&pic=".$i."'><img src='../img/poubelle.png' width='30' height='30'/></a>";
                            }else{
                                echo 
    "<a href='annonce_modifier2.php?image=suppression&id=".$_GET['id']."&t=".$_GET['t']."&pic=".$i."'><img src='../img/poubelle.png' width='30' height='30'/></a>";
                            }
                        echo 
    "</td>";
                    echo 
    "</tr>";    
                }
                
    closedir($dirpic);
            
    ?>
    link to file in rar www.particimmo.com/annonce_modifier2.rar

    line 179 to 196 and

    line 221 to 261

  2. #2
    Join Date
    May 2014
    Posts
    74
    Add the following to the start of the script to make sure all the data input is correct. It's a good debugging method.

    PHP Code:
    <pre>
    $_GET:
    <?php print_r($_GET);
    </
    pre>
    I'd also start echoing out all your strings to make sure it's what you expect, for example:

    PHP Code:
    $dirpicsupp=opendir("../img/annonce/".$pathImg."/".substr($_GET['id'], 01)."/"); 
    Could become something like this:

    PHP Code:
    $dir_to_open "../img/annonce/".$pathImg."/".substr($_GET['id'], 01)."/";
    $dirpicsupp=opendir();
    echo 
    "<b style="colorred;">Opening Dir:</b> $dir_to_open<br>"
    and....

    PHP Code:
    unlink($_SERVER['DOCUMENT_ROOT'].'../img/annonce/'.$pathImg.'/'.substr($_GET['id'], 01).'/'.$picsupp); 
    Could become something like this:

    PHP Code:
    $file_to_unlink"$_SERVER['DOCUMENT_ROOT'].'../img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$picsupp;
    echo "
    <b style="color: red;">Unlinking:</b$dir_to_open<br>";
    unlink(
    $file_to_unlink); 
    You should also do some error checking time to time:

    PHP Code:
    $dir_to_open "../img/annonce/".$pathImg."/".substr($_GET['id'], 01)."/";
    $dirpicsupp=opendir();
    echo 
    "<b style="colorred;">Opening Dir:</b> $dir_to_open<br>";
    if (!
    $dirpicsupp) {
      echo 
    "<b style="colorred;">dirpicsupp unopenable.</b>";
    exit;

    PHP Code:
    $file_to_unlink"$_SERVER['DOCUMENT_ROOT'].'../img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$picsupp;
    echo "
    <b style="color: red;">Unlinking:</b$dir_to_open<br>";
    $delete_success = unlink($file_to_unlink); 
    if(!
    $delete_success) {
      echo "
    <b style="color: red;">file not deleted $file_to_unlink</b>";

    I didn't test this code, but this can be useful in debugging.

  3. #3
    Join Date
    Jun 2014
    Posts
    6
    Thank you Gravy for your quick reply,
    ill test this code later today and let you know how it works out.

  4. #4
    Join Date
    Jun 2014
    Posts
    6
    Hi Gravy,

    i have almost finished it removes the file BUT it cant seem to find the picture number.
    Like the example below 159-.jpg it should be 159-2.jpg

    /var/www/particimmo.com/www/img/annonce/vente/1/159-.jpg, /var/www/particimmo.com/www/img/annonce/vente/1/159--fiche.jpg, /var/www/particimmo.com/www/img/annonce/vente/1/159--liste.jpg, /var/www/particimmo.com/www/img/annonce/vente/1/159--vignette.jpg


    when btn is clicked

    <a href='annonce_modifier2.php?image=suppression&id=".$_GET['id']."&t=".$_GET['t']."&pic=".$i."'>

    i created the link below but he doesnt recognize the .$i.

    $image1 = ($_SERVER['DOCUMENT_ROOT'].'/img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$_GET['id']."-".$i.".jpg");

    Wheres my mistake?


    thank you

  5. #5
    Join Date
    Jun 2014
    Posts
    6
    when btn is clicked

    <a href='annonce_modifier2.php?image=suppression&id=".$_GET['id']."&t=".$_GET['t']."&pic=".$i."'>

    i created the link below but he doesnt recognize the .$i.

    $image1 = ($_SERVER['DOCUMENT_ROOT'].'/img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$_GET['id']."-".$i.".jpg");
    $image2 = ($_SERVER['DOCUMENT_ROOT'].'/img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$_GET['id']."-".$i."-fiche.jpg");
    $image3 = ($_SERVER['DOCUMENT_ROOT'].'/img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$_GET['id']."-".$i."-liste.jpg");
    $image4 = ($_SERVER['DOCUMENT_ROOT'].'/img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$_GET['id']."-".$i."-vignette.jpg");
    echo "<b>Unlinking:</b> $image1, $image2, $image3, $image4<br>";
    unlink($image1);
    unlink($image2);
    unlink($image3);
    unlink($image4);

  6. #6
    Join Date
    Jun 2014
    Posts
    6
    Found the problem

    $image1 = ($_SERVER['DOCUMENT_ROOT'].'/img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$_GET['id']."-".$_GET['pic'].".jpg");

    but the new problem is that my images dont have an unique id so if you have

    image-1
    image-2
    image-3

    and remove image-2

    image-3 is movedup as image-2 but the filename remains image-3

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