PHP 意外 T_String 在 php4 webhost 上但不在 php5 localhost 上
我的 php 脚本遇到了一个非常令人沮丧的问题。使用 XAMPP 在我的本地计算机上一切正常,但当我将其放在网络上时,我得到:
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
WebHost 正在运行 PHP 版本 4.4.9
I am getting a really frustrating problem with my php script below. Everything works as expected on my local machine using XAMPP, but when I put it on the web I get:
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
I would greatly appreciate it if someone would take a quick look for me, or offer some advice.
The error is on the line directly below array_push($projectImages, $filePath);
near the bottom of the function.
EDIT: Even though the error is listed as being on line 20 (in getImages()) I removed the class below and the error went away. Of course, I still want the class to work... but does this mean it is a problem with my php version after all?
<?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
}
}
?>
My Localhost is running PHP Version 5.3.1
The WebHost is running PHP Version 4.4.9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 PHP5 特定的东西,比如 PHP5 中引入的
public
关键字,那么 PHP4 中可能会发生这种奇怪的解析错误。在计划运行 PHP5 时,您也许应该查看官方 PHP5 向后不兼容性列表 PHP4和PHP5下的源代码相同。
摘抄:
This weird kind of parse errors may happen in PHP4 if you use PHP5 specific things, like the
public
keyword, which was introduced with PHP5.You maybe should have a look at the official PHP5 backward incompatibilties list when planning to run the very same source code under PHP4 and PHP5.
Excerpt:
您不能在 PHP 4 中使用 __construct()。
您必须使用与类名相同的函数。
使用 ImgResizer() 代替 __construct(),或者获取更好的网络主机。 PHP 4 已经很老了。
You can't use __construct() in PHP 4.
You have to use a function named the same as the class name.
Use ImgResizer() in place of __construct(), or get a better web host. PHP 4 is very old.
感谢评论中的帮助,我确定这实际上是 php 版本的问题。不过,我不知道为什么它会在第 20 行(即有问题的代码上方大约 10 行)处给出错误。
Thanks to the help of those in the comments, I determined that it was, in fact, an issue with php versions. Still, I have no idea why it was giving the error at line 20, about ten lines above the offending code.