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.
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