2011-09-01 117 views
1

我得到了一个非常令人沮丧的问题,我的PHP脚本下面。一切正常使用XAMPP我的本地机器上如预期,但是当我把它放在网上,我得到:Php意外的T_String上php4虚拟主机,但不是在php5本地主机

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/38/d220091779/htdocs/roughgiraffed/RG2/portfolio_item_vars.php on line 20 

我将不胜感激,如果有人会快速浏览一下我,或提供一些建议。

错误位于函数底部附近的array_push($projectImages, $filePath);正下方。

编辑:即使错误被列为第20行(在getImages())我删除了下面的类和错误消失。当然,我仍然希望课程能够工作......但这是否意味着它终究是我的PHP版本的问题?

<?php 

$pageTitle = "Portfolio"; 

//return an array of the (non-featured, non-thumb) images within a given project 
function getImages($category, $project){ 
    $projectImages = Array(); 
    if ($dir = opendir("portfolio/" . $category . "/" . $project . "/")){ 
     //open each 'file' in the directory 
     while($file = readdir($dir)){ 
      $filePath = "portfolio/" . $category.'/'.$project.'/'.$file; 
      //check if it really is a file 
      if($file != "." && $file != ".." && $file[0] != '.' && is_file($filePath)) { 
       //check if it is an image 
       if(getimagesize($filePath)){ 
        //ignore featured_image and _thumbs 
        if(!strstr($file, "featured_image") && !strstr($file, "_thumb")){ 
         //Add the non-featured, non-thumb image to the array 
         array_push($projectImages, $filePath); 
        } 
       } 
      } 
     } 
     closedir($dir); 
    } 
    return $projectImages; 
} 

class ImgResizer { 
    private $originalFile = ''; 
    public function __construct($originalFile = '') { 
     $this -> originalFile = $originalFile; 
    } 
    public function resize($newWidth, $targetFile) { 
     if (empty($newWidth) || empty($targetFile)) { 
      return false; 
     } 
     $src = imagecreatefromjpeg($this -> originalFile); 
     list($width, $height) = getimagesize($this -> originalFile); 
     $newHeight = ($height/$width) * $newWidth; 
     $tmp = imagecreatetruecolor($newWidth, $newHeight); 
     imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
     if (file_exists($targetFile)) { 
      unlink($targetFile); 
     } 
     imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious 
    } 
} 

?> 

我的本地运行PHP版本5.3.1

的虚拟主机提供商运行PHP版本4.4.9

+0

本地和Web服务器都运行相同版本的PHP吗? – Quasdunk

+3

@Quasdunk - 对这个错误有影响吗? OP,请100000%确保您放置在网络上的文件与您在本地计算机上的文件完全相同。 – ajreal

+0

它可能是围绕回报的parans(我非常怀疑它),试着做'return $ projectImages;'看看会有什么结果。正如Quasdunk所说,我不认为他们运行相同的版本。如果上述不起作用,请发布服务器的PHP版本。但看看这个错误,就像上传的东西似乎有些东西可能会变得混乱起来,正如阿杰莱尔所说的那样。 –

回答

3

如果您使用PHP5特定的东西,例如PHP5中引入的public关键字,那么这种奇怪的解析错误可能发生在PHP4中。

计划PHP4和PHP5下运行的非常相同的源代码时,你或许应该看看官方的PHP5 backward incompatibilties list

摘录:

向后兼容的改变

尽管大多数现有的PHP 4代码应该不用修改工作,你 应注意以下向后不兼容的改变:

  • 有一些新的保留关键字。
  • strrpos()和strripos()现在使用整个字符串作为针。
  • 非法使用字符串偏移导致E_ERROR而不是E_WARNING。

1

感谢那些在评论的帮助下,我确定它是在事实上,与PHP版本的问题。不过,我不知道为什么它在第20行发出错误,比违规代码高出大约十行。

+1

赦免我的无知,上次使用php 4是2005年 – ajreal

+1

davidhaskins指出了这个问题。在php4 oop中,类构造函数与类具有相同的名称---没有__construct或任何其他魔术方法。至于为什么错误发生在哪里,这并不是不常见的,有一个错误没有出现在你期望的地方。即使调查问题也没有意义--PHP4在2007年终结了!没有人应该在生产服务器上运行它。 – gview

2

不能使用__construct()在PHP 4

你必须使用一个名为同一个类名功能。

使用ImgResizer()代替__construct(),或者获得更好的Web主机。 PHP 4很老。