...

View Full Version : Delete Image



simjay
01-13-2008, 08:01 PM
Hey Guys,

I have made a gallery but there is image in the gallery folder that are not in the sql database. I would like to delete these.

This is what i have done so far.



<?php
$dir = "upload/";
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {

$sql9 = "SELECT * FROM gallery";
$result9 = mysql_query($sql9, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result9)) {

$gallery_image = $newArray['gallery_image'];
}
if ($file != $gallery_image)
{
$myFile = "../upload/$gallery_image";
unlink($myFile);
}
else
{
echo "no images";
}
}
closedir($dh);
?>


I have never done this before so i havent got a clue. lol

shyam
01-14-2008, 08:15 AM
<?php
$dir = "upload/";
$dh = opendir($dir);

while (($file = readdir($dh)) !== false) {

$sql9 = sprintf("SELECT * FROM gallery where gallery_image='&#37;s'", mysql_real_escape_string($file));
$result9 = mysql_query($sql9, $conn) or die(mysql_error());
if ( $result9 ) {
// valid results
$newArray = mysql_fetch_array($result9)
$myFile = "../upload/" . $newArray['gallery_image'];
if ( !unlink($myFile) ) {
echo "failed to remove file : $myFile";
}
}
}
closedir($dh);
?>

simjay
01-14-2008, 02:25 PM
I get a server 500 error with that

oesxyl
01-14-2008, 02:35 PM
I get a server 500 error with that

syham code look ok from my point of view.

- you can use opendir or you are in safe mode?
- unlink don't work? check suid of the image files

EDIT: sorry, shyam, correct me please, if I'm wrong:
simjay say "not in database", so:


<?php
$dir = "upload/";
$dh = opendir($dir);

while (($file = readdir($dh)) !== false) {

$sql9 = sprintf("SELECT * FROM gallery where gallery_image='&#37;s'", mysql_real_escape_string($file));
$result9 = mysql_query($sql9, $conn) or die(mysql_error());
if ( $result9 && mysql_num_rows($result9) == 0) {
// valid results
// $newArray = mysql_fetch_array($result9)
// $myFile = "../upload/" . $newArray['gallery_image'];
$myFile = "../upload/" . $file;
if ( !unlink($myFile) ) {
echo "failed to remove file : $myFile";
}
}
}
closedir($dh);
?>


best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum