Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to get a php script to run in a directory using ssh terminal. When I try to run the script, I get the error:

(uiserver):USER:~/directory/folder > php zipper.php
X-Powered-By: PHP/4.4.9
Content-type: text/html

<br />
<b>Parse error</b>:  syntax error, unexpected ')', expecting '(' in <b>/BLA/htdocs/directory/folder/zipper.php</b> on line <b>7</b><br />

Oddly enough though, when I insert a little html and visit the page where this script is located, it works fine. That, however, is no help to me as I need to run this with cron.

Here is the script I'm trying to run:

<?php 
//date variable
$today = date("Ymd");
//create an instance of ZipArchive class
$zip = new ZipArchive();
//create the archive called images.zip and open it
$result = $zip->open('images.zip', ZipArchive::CREATE);
if ($result) {
    //open the directory with the files that need to be archived
    $d = dir("turbo/");
    //loop through the files in $d
    while (false !== ($entry = $d->read())) {
        //use a regular expression with preg_match to get just the jpgs
        if(preg_match("/\w*\.jpg/",$entry)) {
            //add the file to the archive
            $zip->addFile("turbo/".$entry,$entry);
        }
    }
    //close the directory and archive
    $d->close();
    $zip->close();
} else {
    //display the error
    echo "Failed: $result.\n";
}
//deletes all jpg files
//foreach(glob('/www/images/*.jpg') as $file){
//  if(is_file($file))
//  @unlink($file);
//}
?>

Here's the line where it's erroring out on:

$result = $zip->open('images.zip', ZipArchive::CREATE);

I'm using 1and1 (I know) as my host, I've tried creating an .htaccess file to force php5, but that doesn't work.

I'm wondering what, syntactically, is wrong with this script? All I want to do is zip all the jpg images in a directory.

Any, ANY, help would be greatly appreciated.

share|improve this question
add comment

closed as too localized by Gordon, hakre, DaveRandom, Jocelyn, Jasper Mar 6 at 1:09

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

up vote 3 down vote accepted

It seems this method won't work in PHP 4 (The manual mentions (PHP 5 >= 5.2.0)).

I tried running your code in a PHP 4 sandbox, I got the same output as you did. When you select PHP 5.2.15 and run the code, everything works fine (except that the site mentions having open() disabled for security reasons).

So it seems you're out of luck. I recommend having a look at the File_Archive package mentioned in this Stackoverflow post. According to their site, it will run on PHP 4.3.3

Dependencies for File_Archive

PHP 4.3.3
PEAR Installer 1.4.0b1
MIME_Type
pcre extension
zlib extension
Mail_Mime (Optional)
Mail (Optional)
Cache_Lite 1.5.0 (Optional)
bz2 extension (Optional)
share|improve this answer
 
Thanks Terry! How would you explain the fact that this works when I actually visit the page? –  Dan Sep 15 '12 at 20:50
 
Hmm, apparently both my links use PHP 5.4.7. If you select 'PHP 4.4.9' you should get the <b>Parse error</b>: syntax error, unexpected ')', expecting '(' in <b>[...][...]</b> on line message :} –  Terry Seidler Sep 15 '12 at 21:23
add comment

I just ran the code "as is" on PHP 5.4.6. It works pretty well (with the exception of capturing .JPG in the zip file).

As Terry Seidler and Dan mentioned, this might be a versioning issue.

share|improve this answer
add comment

I think the error could be ZipArchive::CREATE try with ZIPARCHIVE::CREATE instead.

share|improve this answer
 
PHP class names are case insensitive. –  lanzz Sep 15 '12 at 17:04
 
@Nelson I still get the same exact error. I'm wondering now if this is a php version issue. –  Dan Sep 15 '12 at 17:05
 
Well, php.net does say (PHP 5 >= 5.2.0 on the ZipArchive.open page: php.net/manual/en/ziparchive.open.php –  Terry Seidler Sep 15 '12 at 17:11
 
You could try the last post on this page: stackoverflow.com/questions/437952/… –  Terry Seidler Sep 15 '12 at 17:25
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.