0

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 :)

Earunder
  • 177
  • 1
  • 11
  • 2
    What do you mean by "it failed"? What where the specific errors? – Kyle Jul 27 '15 at 13:49
  • 3
    Syntax: `foreach ($groupFilesas $file) {` fix to `foreach ($groupFiles as $file) {` space between `$groupFiles` and the word `as` In both of your attempts – RiggsFolly Jul 27 '15 at 13:52
  • 1
    Then modify this parameter in `php.ini` to `display_errors = On` while you are developing. Or at least add this as the first line of all your code if you are foolish enough to develop on the live server, `ini_set('display_errors', '1')` – RiggsFolly Jul 27 '15 at 13:54
  • @Kyle - Thanks for the quick replies. Nothing shows up in the error logs or error reporting and the zip file loads in the browser as it should. The zip file though is corrupt. – Earunder Jul 27 '15 at 13:59
  • Better still use these 2 lines in code you are developing `error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); ini_set('display_errors', '1')` – RiggsFolly Jul 27 '15 at 13:59
  • @RiggsFolly - Thank you for the replies. It's being developed on a beta site ;) Error checking is enabled, but no error codes show up – Earunder Jul 27 '15 at 14:03

1 Answers1

2

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_ or PDO

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149