Jump to content

PHP runs on browser but not command line


shartpphp

Recommended Posts

Hi,

I have this frustrating issue, and my hosting company couldn't help so far.

 

I have a php code that runs perfectly on web. But when I want to run it on command line, it gives following syntax error!!

 

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in testfile.php on line 109

Errors parsing testfile.php

 

On that line, I have class member function. The function is public. If I remove the word "public", it does not show the error on that line any more, but  then gives error on other lines.

 

I am repeating this once again. This code works perfectly on Web, but running it by php testfile.php, it gives syntax error.

 

Please help

Thanks

S.

 

Hi,

I have this frustrating issue, and my hosting company couldn't help so far.

 

I have a php code that runs perfectly on web. But when I want to run it on command line, it gives following syntax error!!

 

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in testfile.php on line 109

Errors parsing testfile.php

 

On that line, I have class member function. The function is public. If I remove the word "public", it does not show the error on that line any more, but then gives error on other lines.

 

I am repeating this once again. This code works perfectly on Web, but running it by php testfile.php, it gives syntax error.

 

Please help

Thanks

S.

 

Here is my code:

 

<?php

//1 & 2
$zipFile = findLastZipFile();
echo $zipFile;

unzipFile($zipFile);

//#####################################################################################
function findLastZipFile()
{
    $dir = './xml/';
    $filesList = array();
    $i=-1;
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
                $fDate="";
                $fName="";
                $i++;
                //echo "filename: $file : filetype: " . filetype($dir . $file) . "<br>";
                $fDate =  date("Y-m-d", filemtime("./xml/".$file));
                $fName =  $file;
                //echo "file: $fName date: $fDate<BR><BR>";
                if($fName !=".." & $fName !="." & $fName !=".ftpquota"){
                    $filesList["$file"]= strtotime($fDate);  // converts the date to a number that represents date
                }
            }
            closedir($dh);
        }
    }
    else
    {
        echo "Dir not found";
    }

    asort($filesList);
    $newestFile = array_pop(array_flip($filesList));

    return($newestFile);
}

//#####################################################################################
function deleteXMLfiles(){
    $dir = '/www/localonlineshoppingbot/adm/zipTools/output/';
    $xmlFiles = array();
    if(is_dir($dir)){
        if ($dh = opendir($dir)) {
            while(($file = readdir($dh)) !== false){
                if(strpos(strtolower($file),"xml")){
                    echo $file;
                    echo "<BR>";
                    unlink("./output/".$file);
                }
            }
        }
    }
}

//#####################################################################################
function unzipFile($zFile){
    $ARCHIVE = new zip;
            //$ARCHIVE->makeZip('./','./test.zip'); // make an ZIP archive
            //var_export($ARCHIVE->infosZip('./test.zip'), false); // get infos of this ZIP archive (without files content)
            //var_export($ARCHIVE->infosZip('./test.zip')); // get infos of this ZIP archive (with files content)
            //$ARCHIVE->infosZip('./test.zip');
    $ARCHIVE->extractZip("./xml/$zFile", './out/'); //

} //unzipFile()

class zip
{
    public function infosZip ($src, $data=true)
    {
        if (($zip = zip_open(realpath($src))))
        {
            while (($zip_entry = zip_read($zip)))
            {
                $path = zip_entry_name($zip_entry);
                if (zip_entry_open($zip, $zip_entry, "r"))
                {
                    $content[$path] = array (
                        'Ratio' => zip_entry_filesize($zip_entry) ? round(100-zip_entry_compressedsize($zip_entry) / zip_entry_filesize($zip_entry)*100, 1) : false,
                        'Size' => zip_entry_compressedsize($zip_entry),
                        'NormalSize' => zip_entry_filesize($zip_entry));
                    if ($data)
                        $content[$path]['Data'] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                    zip_entry_close($zip_entry);
                }
                else
                    $content[$path] = false;
            }
            zip_close($zip);
            return $content;
        }
        return false;
    }
    public function extractZip ($src, $dest)
    {
        $zip = new ZipArchive;
        if ($zip->open($src)===true)
        {
            $zip->extractTo($dest);
            $zip->close();
            return true;
        }
        return false;
    }
    public function makeZip ($src, $dest)
    {
        $zip = new ZipArchive;
        $src = is_array($src) ? $src : array($src);
        if ($zip->open($dest, ZipArchive::CREATE) === true)
        {
            foreach ($src as $item)
                if (file_exists($item))
                    $this->addZipItem($zip, realpath(dirname($item)).'/', realpath($item).'/');
            $zip->close();
            return true;
        }
        return false;
    }
    private function addZipItem ($zip, $racine, $dir)
    {
        if (is_dir($dir))
        {
            $zip->addEmptyDir(str_replace($racine, '', $dir));
            $lst = scandir($dir);
                array_shift($lst);
                array_shift($lst);
            foreach ($lst as $item)
                $this->addZipItem($zip, $racine, $dir.$item.(is_dir($dir.$item)?'/':''));
        }
        elseif (is_file($dir))
            $zip->addFile($dir, str_replace($racine, '', $dir));
    }
}


?>

Thanks Ken. Well that is the issue. I also don't get error in Browser. That means code does not have any syntax error at all. But something in command line is missing that causes the issue for me.

it should be something with php.ini or so.

 

I hope someone could guess what it is, or at least tell me some troubleshooting way.

 

Sam

Ok, I am not sure kind of trick my hosting company is doing ,but here is what I see for each of these tests:

 

1- running phpinfo(); in a php file by browser, I get PHP Version 5.2.6

2- running phpinfo(); in a php file by command line, I get php version 4.4.8

 

3- running echo phpversion(); in a php file by browser, I get internal server error!!

 

4- running echo phpversion(); in a php file call it in command line, it prints 4.4.8

 

5- running php -v in command line, it prints following:

PHP 4.4.8 (cli) (built: Jul 11 2008 07:08:19)

Copyright © 1997-2008 The PHP Group

Zend Engine v1.3.0, Copyright © 1998-2004 Zend Technologies

    with Zend Extension Manager v1.2.2, Copyright © 2003-2007, by Zend Technologies

    with Zend Optimizer v3.3.3, Copyright © 1998-2007, by Zend Technologies

 

So I am confused, is it php 4 or 5?

My hosting company's control panel hast an option that you click and it enables the PHP5 !!! That is strange, what you think it is?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.