Saturday 8 October 2011

Get image from mysql database and copy to file system.

Get image (or download image) from mysql database and copy to file system.




// Performing query on database
$querySelect = " select * from products where image is not null order by image limit 1, 5000";
$selectEposResult = mysql_query($querySelect, $link) or die("Query failed to get content.$querySelect");


// Looping through data of the query
while ($line = mysql_fetch_array($selectEposResult, MYSQL_ASSOC)) {

// Other way of loop thogur results, that worked, for colum had to use $line->NAME
//while ($row = mysql_fetch_object($selectEposResult)) {

echo "
Name: " . $line['NAME'];
$imgName = "img_".$line['CODE'];


if($line['IMAGE'] == "" || $line['IMAGE'] == NULL) {

echo $filename."
NULL data";

}
else {
$filename= "/Users/nidb/xxx/$imgName.gif";

$file=fopen($filename, 'w') or die("Can't open file");


if (fwrite($file, $line['IMAGE'])) {
echo $filename."
";
$counter++;
}

fclose($file);
}

}

?>

No comments: