【问题标题】:PHP Parse Error Syntax error - unexpected ')', expecting '(' [closed]PHP Parse Error 语法错误 - 意外')',期待'(' [关闭]
【发布时间】:2012-09-15 16:54:45
【问题描述】:

我正在尝试使用 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);

我使用 1and1(我知道)作为我的主机,我尝试创建一个 .htaccess 文件来强制使用 php5,但这不起作用。

我想知道这个脚本在语法上有什么问题?我要做的就是将所有 jpg 图像压缩到一个目录中。

任何,任何,帮助将不胜感激。

【问题讨论】:

    标签: php ssh cron zip jpeg


    【解决方案1】:

    似乎这种方法在 PHP 4 中不起作用(The manual 提到了(PHP 5 &gt;= 5.2.0))。

    我尝试在 PHP 4 沙箱中运行您的代码,我得到了 the same output 和您一样。 当您select PHP 5.2.15 and run the code 时,一切正常(除了该网站提到出于安全原因禁用了 open())。

    看来你运气不好。我建议查看this Stackoverflow post 中提到的 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.7。如果您选择“PHP 4.4.9”,您应该会收到 &lt;b&gt;Parse error&lt;/b&gt;: syntax error, unexpected ')', expecting '(' in &lt;b&gt;[...][...]&lt;/b&gt; on line 消息:}
    【解决方案2】:

    我只是在 PHP 5.4.6 上“按原样”运行代码。它工作得很好(除了在 zip 文件中捕获 .JPG)。

    正如 Terry Seidler 和 Dan 提到的,这可能是一个版本问题。

    【讨论】:

      【解决方案3】:

      我认为错误可能是ZipArchive::CREATE 尝试使用ZIPARCHIVE::CREATE

      【讨论】:

      猜你喜欢
      • 2015-12-31
      • 1970-01-01
      • 2014-02-19
      • 2010-11-20
      • 2013-04-12
      • 2012-10-30
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多