/    Sign up×
Community /Pin to ProfileBookmark

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”);
}[/CODE]

[code=php]if((isset($_GET[‘image’]))&&($_GET[‘image’]==’suppression’)&&(isset($_GET[‘pic’]))&&(!empty($_GET[‘pic’]))){

$dirpicsupp=opendir(“../img/annonce/”.$pathImg.”/”.substr($_GET[‘id’], 0, 1).”/”);
while($picsupp = readdir($dirpicsupp))
{
if(ereg(“^”.$_GET[‘id’].”-“.$_GET[‘pic’], $picsupp)==true)
{
unlink($_SERVER[‘DOCUMENT_ROOT’].’../img/annonce/’.$pathImg.’/’.substr($_GET[‘id’], 0, 1).’/’.$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’].'”>’;
}[/code]

Upload is working fine

[code=php]<?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’], 0, 1).”/”.$_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’], 0, 1).”/”.$_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);
?>[/code]

link to file in rar www.particimmo.com/annonce_modifier2.rar

line 179 to 196 and

line 221 to 261

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@GravyJun 28.2014 — Add the following to the start of the script to make sure all the data input is correct. It's a good debugging method.

[code=php]
<pre>
$_GET:
<?php print_r($_GET);
</pre>
[/code]


I'd also start echoing out all your strings to make sure it's what you expect, for example:

[code=php]
$dirpicsupp=opendir("../img/annonce/".$pathImg."/".substr($_GET['id'], 0, 1)."/");
[/code]


Could become something like this:

[code=php]
$dir_to_open = "../img/annonce/".$pathImg."/".substr($_GET['id'], 0, 1)."/";
$dirpicsupp=opendir();
echo "<b style="color: red;">Opening Dir:</b> $dir_to_open<br>";
[/code]


and....

[code=php]
unlink($_SERVER['DOCUMENT_ROOT'].'../img/annonce/'.$pathImg.'/'.substr($_GET['id'], 0, 1).'/'.$picsupp);
[/code]


Could become something like this:

[code=php]
$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);

[/code]


You should also do some error checking time to time:

[code=php]
$dir_to_open = "../img/annonce/".$pathImg."/".substr($_GET['id'], 0, 1)."/";
$dirpicsupp=opendir();
echo "<b style="color: red;">Opening Dir:</b> $dir_to_open<br>";
if (!$dirpicsupp) {
echo "<b style="color: red;">dirpicsupp unopenable.</b>";
exit;
}
[/code]


[code=php]
$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>";
}
[/code]


I didn't test this code, but this can be useful in debugging.
Copy linkTweet thisAlerts:
@theplanetriderauthorJun 28.2014 — Thank you Gravy for your quick reply,

ill test this code later today and let you know how it works out.
Copy linkTweet thisAlerts:
@theplanetriderauthorJul 01.2014 — Hi Gravy,

i have almost finished it removes the file [B]BUT[/B] it cant seem to find the picture number.

Like the example below 159-.jpg it should be 159-[B]2[/B].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
Copy linkTweet thisAlerts:
@theplanetriderauthorJul 01.2014 — 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);
Copy linkTweet thisAlerts:
@theplanetriderauthorJul 01.2014 — 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
×

Success!

Help @theplanetrider spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...