我正在尝试使用ssh终端在目录中运行php脚本。当我尝试运行脚本时,我得到错误:
(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 />
奇怪的是,当我插入一个小html并访问这个脚本所在的页面时,它工作得很好。然而,这对我没有帮助,因为我需要用cron来运行它。
这是我试图运行的脚本:
<?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);
//}
?>
这是它出错的地方:
$result = $zip->open('images.zip', ZipArchive::CREATE);
我使用1和1(我知道)作为主机,我尝试创建一个.htaccess文件来强制php5,但这不起作用。
我想知道这个脚本在语法上有什么问题?我只想把所有jpg图片压缩到一个目录中。
任何帮助都将不胜感激。
这个方法似乎在PHP4中不起作用(手册中提到了(PHP 5 >= 5.2.0)
)。
我试着在PHP 4沙箱中运行您的代码,得到了与您相同的输出。当您选择PHP 5.2.15并运行代码时,一切都很好(除了网站提到出于安全原因禁用了open())。
看来你运气不好。我建议大家看看这篇文章中提到的File_Archive包。根据他们的网站,它将在PHP 4.3.3 上运行
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)
我刚刚在PHP 5.4.6上"按原样"运行了代码。它工作得很好(除了在zip文件中捕获.JPG)。
正如Terry Seidler和Dan提到的,这可能是一个版本控制问题。
我认为错误可能是ZipArchive::CREATE
,请改用ZIPARCHIVE::CREATE
。