PDA

View Full Version : Uploading image to folder - how to delete it when deleting id


nanny
01-30-2009, 03:00 AM
Hi I have a form for the user to upload the images and resize them to a folder, I can also edit or delete that link via deleting the photo table id.
I don't know how you can connect the actual image and the resized image to be deleted at the same time. Otherwise it stays on the server forever and large images should be removed.
Thanks - know of any tuts for this? I googled but didn't get the result I was after - probably using the wrong search term.

nanny
02-03-2009, 06:27 AM
Hey I thought there would be quite a few people who would know this one. There must be a way to grab those images and delete them.

domedia
02-03-2009, 07:00 AM
Can't you just resize the image and overwrite? I thought this was defualt unless you specified another filename..

On the other hand, I always thought it was a good idea to keep the original in case of a screw up or a redesign etc.

nanny
02-05-2009, 12:54 AM
OK what I mean is in my mysql table it uploads the original to a folder named pics and the thumbnail it makes from the original to a folder named thumbs. In the mysql table it is the actual link to the source not the actual image itself otherwise I would be using blob not text or varchar.
So it deletes the link but not the image itself in the folders.
You can't search for these images unless I make a script to do so - I simply wanted to attach all in one big hit instead of leaving drips and drops behind.

davidj
02-05-2009, 06:47 AM
@unlink("path/image.jpg")

nanny
02-09-2009, 11:01 PM
will give this a go if I have trouble I will ask what I am doing wrong.
Thanks

nanny
02-20-2009, 02:54 AM
Well I had a go and it deletes still from database but not the actual images. If I am directing the path should it be say for localhost C:ect. or just the path that is close to the page that is deleting.
Show what I have:


$sql = "DELETE FROM images WHERE image_id = {$_POST['image_id']}";
$deleted = mysql_query($sql) or die(mysql_error());
$image_path = '../images/gallery/';
$image_path2 = '../images/gallery/thumbnail/';// if you had thumbnails
unlink($image_path);
unlink($image_path2);




So the path I have at the moment is the path to the images as it would be from that page.

Thanks.....

davidj
02-20-2009, 05:49 AM
make sure you have full permissions to the directory your deleting from

nanny
05-26-2010, 02:24 AM
Hi David I am still unable to delete images from the folder - if you have permissions to upload to that folder wouldn't you have permission to delete?

thanks I know this is old but now the image folders are getting ridiculously big. I have looked in many tutorials and the code seems the same but it isn't deleting the images - the directory is correct.

edbr
05-26-2010, 03:19 AM
wouldn't image path be
$_POST['image_id'].'../images/gallery/'; ?

davidj
05-26-2010, 06:20 PM
just do a...

$path = 'path/image.jpg';

if(file_exists($path)){

echo "file exists";

}to check the paths

once you id the path is correct then replace with

@unlink($path);If your app created the directory/files then the owner of the files will be apache

you can chmod the files after uploading or creating them by using

chmod("/path/files", 0644);

That will set the correct rights

BrainControl
03-12-2012, 07:34 PM
You should first getting the image in order to see if this the image you want to delete.

In the page you are making the deletion,in the case confrming deletion,use as example:

$path=$img_path;
$img=ReturnPics($img_path,$ID);

if($mode=="1")
{
deleteimage($img,$path);
RedirectPage("$indexpage?mode=1&ID=$ID");
exit;
}
for($i=0;$i<count($logo);$i++)
{
<img src="<?$img_path.$logo[$i]?>">
<a href="<?php echo $indexpage;?>?mode=di&delete=1&ID1=<?php echo $ID;?>&cid=<?php echo $cid;?>&catid=<?php echo $catid;?>&img=<?php echo $logo[$i];?>&path=<?php echo $machimg_path;?>">Delete Image</a>
}
for($i=count($logo);$i<1;$i++)
{
<tr>
<td class="lightbg"><b>Machinery Image</b><span class="err" >(jpg)</span></td>
<td class="lightbg" align="left"><span class="err">210 x 160</span> <input name="userfile" type="file" ></td>
</tr>

}
$userfile=$_FILES["userfile"];
if ($userfile)
{
Upload_file($img_path,null,$ID,840, 640, $th_w, $th_h,"",$userfile);
}
switch ($Mode)
{
case "d":
$img=ReturnPics($img_path,$ID);
if($img[0])
{
deleteimage($img[0],$img_path);
}
RedirectPage("$indexpage?....");
break;
}


In another file you write the Two functions That Are:Return Pics And Delete Image As Example:

First Line:$userfile=$_FILES["userfile"];

Then:
/*Returning Image*/
function ReturnPics($folder,$id)
{

$d = dir($folder);
while ($entry = $d->read())

{
if($entry!="." && $entry!="..")
{
$file=basename($entry);
if(!is_dir($folder."/".$file))
{
$ar_spec=explode("_",$file);
if ($ar_spec[0]==$id)
{
$result[] = $file;
}
}
}
}
$d->close();
return $result;

}
/*End Of Function*/

/*Delete Image*/
function deleteimage($imagename,$imgpath)
{
$path=$imgpath.$imagename[0];
/*To Remove Unwanted Spaces And-Or Newlines*/
$path = trim($path);
/*End */

if (unlink($path))
{
return true;
}

else
{
return false;
}
}

/*End Of Function*/


Hope This Help