Reputation: 189
I've been playing with the following script for the last hour now and can not figure out what the hell is going wrong.
What's the idea:- Basically I have a database that holds data on images and I am calling that data (image filenames), trying to put it into an array to then pass to a zip creation script.
Here's what I have:-
$query_ImageCollect = "SELECT * FROM image_data WHERE image_dealerid='$dealer_id' AND imagename LIKE '$imageName'";
$ImageCollect = mysql_query($query_ImageCollect, $vwconn) or die(mysql_error());
$row_ImageCollect = mysql_fetch_assoc($ImageCollect);
$totalRows_ImageCollect = mysql_num_rows($ImageCollect);
$counter=0;
do{
$counter=$counter+1;
$ImageUrl=$row_ImageCollect['image_url'];
$getFileNames="../../images/$ImageUrl";
if($counter==1){
$getFiles="'$getFileNames'";
}else{
$getFiles="$getFiles, '$getFileNames'";
}
} while ($row_ImageCollect = mysql_fetch_assoc($ImageCollect));
$groupFiles=array("$getFiles");
$zipName="Pictures.zip";
$zip = new ZipArchive;
$zip->open($zipName, ZipArchive::CREATE);
foreach ($groupFilesas $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
I also tried parsing the results directly into an array but it also failed:-
$query_ImageCollect = ("SELECT image_url FROM image_data WHERE image_dealerid='$dealer_id' AND imagename LIKE '$imageName'") or die(mysql_error());
while( $row = mysql_fetch_assoc( $query_ImageCollect )){
$groupFiles[] = $row;
}
$zipName="Pictures.zip";
$zip = new ZipArchive;
$zip->open($zipName, ZipArchive::CREATE);
foreach ($groupFilesas $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
Any help / suggestions would be much appreciated. Thanks in advance :)
Upvotes: 0
Views: 152
Reputation: 94662
You are not actually adding anything to the zip as you are not processing the query result correctly. You are trying to pass a $row array as the filename to $zip->addFile()
You also dont need to create a temporary array from the query result and then process that you can do it all as part of the first query result processing loop.
Also in your second attempt you are not issuing the query for execution, so there will be no results to process.
Try this instead :-
$query_ImageCollect = "SELECT image_url
FROM image_data
WHERE image_dealerid='$dealer_id'
AND imagename LIKE '$imageName'";
$ImageCollect = mysql_query($query_ImageCollect, $vwconn) or die(mysql_error());
$zipName="Pictures.zip";
$zip = new ZipArchive;
$zip->open($zipName, ZipArchive::CREATE);
while( $row = mysql_fetch_assoc( $ImageCollect)) {
// make sure you dont need to add anything to the
// $row['image_url'] to make a correctly pathed filename
$zip->addFile($row['image_url']);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
If you truely got no errors reported anywhere, then something is badly wrong with your config. The php error file should be HUGH.
Also please dont use the mysql_ database extension it has been deprecated for years now, especially if this is a new development. See this for help moving to
mysqli_
orPDO
Upvotes: 2