
Untitled
By: a guest on
May 11th, 2012 | syntax:
None | size: 0.95 KB | hits: 21 | expires: Never
Deleting Files In A Directory Based On A Table
//listing all the ".jpg" files
$arrayfiles=scandir("../images/Produk/");
//getting all the product list
$sql="select * from produk";
$produk=mysql_query($sql,$conn) or die("Error : ".mysql_error());
foreach($arrayfiles as $key=>$value)
{
while($row=mysql_fetch_array($produk,MYSQL_ASSOC))
{
///here is the part i've been confused of.
}
}
$sql = "SELECT * FROM Produk";
$result = mysql_query($sql);
$existing_products = array();
while ($row = mysql_fetch_array($result))
$existing_products[] = $row["id_produk"] . ".jpg";
$existing_images = array();
foreach(glob("../images/Produk/*.jpg") as $v)
$existing_images[] = str_replace("../images/Produk/", "", $v);
$images_to_delete = array_diff($existing_images, $existing_products);
dir /b > filelist.txt (windows)
ls -1 > filelist.txt (linux)
foreach(glob('../images/Produk/*.jpg') as $file) {
if(is_file($file))
@unlink($file);
}