1

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

hakre
  • 193,403
  • 52
  • 435
  • 836
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • Do both the local and the web server run the same version of PHP? – Quasdunk Sep 01 '11 at 15:26
  • 3
    @Quasdunk - does it matter to this error ? OP, please 100000% ensure the file your put it on the web is exactly same as the one you have in your local machine. – ajreal Sep 01 '11 at 15:36
  • It could be the parans around the return (I highly doubt it), try just doing `return $projectImages;` See what comes of it. As Quasdunk says, I do not think they run the same versions. If the above does not work, post the server's php version please. But looking at the error, it seems that something could be messing up when you are uploading, as ajreal said. – Jim Sep 01 '11 at 15:37
  • I'll re-upload it again and let you know if anything changes... but I am 99.99% – Zach Lysobey Sep 01 '11 at 15:37
  • 4
    I agree with @ajreal. I have just syntax checked the above code against 4.3.10, 4.4.9, 5.2.9-2, 5.2.17, 5.3.5 and 5.3.6 and it came up clean on all of them. The code on the server is not the same as the code on the local machine. – DaveRandom Sep 01 '11 at 15:39
  • I just re-uploaded the file and checked the previous one as well. They are all identical as far as I can tell. I added in the php versions and removed the parens from the return. It is still throwing the error :-( – Zach Lysobey Sep 01 '11 at 15:53
  • Maybe it is worth saying that this file is an include. Would other php files being not the same somehow effect this? – Zach Lysobey Sep 01 '11 at 15:54
  • Is the file above definitely `portfolio_item_vars.php`? Seems like an odd name for a file with just this function in... Sounds more like a file with some configuration in to me... – DaveRandom Sep 01 '11 at 15:55
  • 1
    Check your php version of your server – Suresh Katawal Sep 01 '11 at 15:45
  • My Localhost is running PHP Version 5.3.1 The WebHost is running PHP Version 4.4.9 – Zach Lysobey Sep 01 '11 at 15:54
  • it is a funny name I agree, but it is what it is. I have a _vars page for each page on the site, most only have a variable or two, but this one happens to have a function – Zach Lysobey Sep 01 '11 at 16:03
  • How are you transferring the files to the server? If it is via FTP, make sure it is sent as a TYPE I, TYPE A will change the format of the line breaks in the file. Shouldn't make a difference but you never know. – DaveRandom Sep 01 '11 at 16:06
  • Hmm... it is FTP (filezilla). I'll look into this. – Zach Lysobey Sep 01 '11 at 16:07
  • 1
    Is this a literal copy/paste of the file? Cos if it is, line 20 in my editor is not the line immediately below the `array_push...`, it is the line immediately above `closedir()`. Not that this helps at the moment, just trying to ensure we have a complete picture of the file. – DaveRandom Sep 01 '11 at 16:11
  • no it isnt... I just changed some stuff around... removed some stuff way below where it was saying there was an error and it went away.. im editing the post with the full content of the file now. I am assuming this might be a php version problem after all after seeing this – Zach Lysobey Sep 01 '11 at 16:19
  • 1
    Hm, I'm not sure if I remember that correctly, but wasn't PHP4 sometimes giving that kind of parse errors when hitting PHP5 specific things, e.g. the `public` keyword or similiar? What does line 20 of `/homepages/38/d220091779/htdocs/roughgiraffed/RG2/portfolio_item_vars.php` contain? – Jürgen Thelen Sep 01 '11 at 16:20
  • ahhh... I do have a public keyword there.... but line 20 is still in the previous function... really weird – Zach Lysobey Sep 01 '11 at 16:22
  • 1
    Yeh you cannot use `public`, `private` etc in PHP4. That is the cause of the error, although why it says the error is on line 20 I don't know. In a class in PHP4, all members are effectively public. Methods must be defined as simply `function ( ... ) {...` and you can only declare properties with the `var` keyword. – DaveRandom Sep 01 '11 at 16:25
  • Anyone have a good resource or advice making my above class php4-ready? Also I am really sorry I omitted that stuff earlier.. I hate wasting people's time and would have left that in if I though there was ANY chance it would effect something. Thanks Everyone for their help! – Zach Lysobey Sep 01 '11 at 16:25
  • Also, if someone puts something relevant in an answer I would be more than happy to select it as correct and give you an upvote! – Zach Lysobey Sep 01 '11 at 16:32

3 Answers3

3

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:

Backward Incompatible Changes

Although most existing PHP 4 code should work without changes, you should pay attention to the following backward incompatible changes:

  • There are some new reserved keywords.
  • strrpos() and strripos() now use the entire string as a needle.
  • Illegal use of string offsets causes E_ERROR instead of E_WARNING.

:

Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
2

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.

davidhaskins
  • 280
  • 2
  • 12
1

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.

Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • 1
    pardon my ignorance,last use php 4 is year 2005 – ajreal Sep 01 '11 at 16:50
  • 1
    davidhaskins pinpointed the problem. In php4 oop the class constructor had the same name as the class --- there was no __construct, or any of the other magic methods. As to why the error occurs where it does, this is not unusual to have an error that doesn't show up where you expect it would. There is no point in even investigating the problem -- php4 went end of life in 2007! Noone should be running it on a production server. – gview Sep 01 '11 at 17:33