2014-12-30 11 views
-6

我想不出什麼導致這個錯誤:PHP T_Public語法錯誤?

Parse error: syntax error, unexpected T_PUBLIC

它說的錯誤是在此代碼(public function upload($uid = 'public')的第二行存在的。

//--> Uploads the file 
public function upload($uid = 'public') 
{ 

//Get file details 
if(isset($_GET['ajax'])) 
{ 
if(!$_SERVER['HTTP_X_FILE_SIZE'] > 0) return false; 

$filename = strip_tags($_SERVER['HTTP_X_FILE_NAME']); 
$file_type = $_SERVER['HTTP_X_FILE_TYPE']; 
$size = round($_SERVER['HTTP_X_FILE_SIZE']/1000, 3); 
} 
else 
{ 
if($_FILES["files"]["error"] > 0 or empty($_FILES)) return false; 

$filename = strip_tags($_FILES['files']['name']); 
$file_type = $_FILES['files']['type']; 
$size = round($_FILES['files']['size']/1000, 3); 
} 

//FF null type fix 
if($file_type == null || strlen($file_type) == 0) $file_type = "unknown"; 

//Reduce filename to < 255 char 
if(strlen($filename) > 255) 
{ 
$filename = urldecode($filename); 
$filename = substr($filename, 0, 36) . '--.' . pathinfo($filename, PATHINFO_EXTENSION); 
$filename = urlencode($filename); 
} 

//Check file size 
if($size > (maxFileSize/1000)) 
{ 
$this->errorMessage = "file too large"; 
return false; 
} 

//Check file type 
if(limitFileTypes and !in_array($file_type, explode(',', allowedFileTypes))) 
{ 
$this->errorMessage = "file type not allowed"; 
return false; 
} 

$db = $this->connectDB(); 

$share = isset($_GET['ajax'])?$_SERVER['HTTP_BITDROPSHARE']:$_POST['bitdrop_share']; 
$share = ($share == 'true' || $share == 'share')? 1 : 0; 

$password = isset($_GET['ajax'])?$_SERVER['HTTP_BITDROPPASS']:$_POST['bitdrop_password']; 

//Find an available shortURL 
$res = $db->prepare("select count(shortURL) from share where binary shortURL = ?;"); 
do 
{ 
$shortURL = $this->shortURL(); 
$res->execute(array($shortURL)); 
$data = $res->fetchAll(PDO::FETCH_COLUMN, 0); 
} 
while($data[0] != 0); 

$password = (is_null($password) or strlen($password)==0) ? 0 : sha1($password . $shortURL) ; 

$q = "insert into details (date, name, size, type, public, password) values (now(), ?, ?, ?, ?, ?)"; 
$data = array($filename, $size, $file_type, $share, $password); 
$res = $db->prepare($q); 

//exec command or print error 
if(!$res->execute($data)) print_r($db->errorInfo()); 

$fid = $db->lastInsertId(); 

$q = "insert into share (shortURL, file_id) values (?, ?)"; 
$data = array($shortURL, $fid); 
$res = $db->prepare($q); 
$res->execute($data); 

//Connect unique user to file 
$tid = $this->addTag("_$uid"); 

$q = "insert into `fid-tid` (file_id, tag_id) values ('$fid', '$tid');"; 
$res = $db->prepare($q); 
$res->execute(); 

$file = $_SERVER[DOCUMENT_ROOT] . "/uploads/$fid.temp"; 

$this->log('upload', '{ "uid" : "'.$uid.'", "fid" : "'.$fid.'", "shortURL" : "'.$shortURL.'" }'); 

$db = null; 

//Upload file 
if(isset($_GET['ajax'])) 
{ 
if(!isset($_SERVER['HTTP_X_FILE_NAME']) && !isset($_SERVER['CONTENT_LENGTH'])) 
{ 
$this->errorMessage = "no headers found"; 
return false; 
} 

$fileReader = fopen('php://input', "r"); 
$fileWriter = fopen($file, "w+"); 

while(true) 
{ 
$buffer = fgets($fileReader, 4096); 
if(strlen($buffer) == 0) 
{ 
fclose($fileReader); 
fclose($fileWriter); 
$this->createThumb($fid, $shortURL); 
$this->shortURL = $shortURL; 
return true; 
} 
fwrite($fileWriter, $buffer); 
} 
} 
else 
{ 
move_uploaded_file($_FILES["files"]["tmp_name"], $file); 
$this->createThumb($fid, $shortURL); 
$this->shortURL = $shortURL; 
return true; 
} 
} 
+0

哎呀!你的縮進發生了什麼? ':-)' – halfer

+2

下一次,看看你是否可以做一個簡單的例子。在你的情況下,你可以做一個這樣的例子:'public function test(){echo'test';}'..你仍然會得到錯誤,問題會變得更加清晰。答案不會更有趣,因爲'公共'不允許在那裏,但它會提出更好的問題:) – Nanne

+0

似乎像經典的部分複製+粘貼(雖然可能是其他一些語法問題)。 Origin,btw:'bitdrop.class.php - v1.4'。 – mario

回答

2

您正在定義功能public

public是Object Orieneted Programming的關鍵字。

但是,根據您的代碼,沒有定義類,因此,沒有使用public關鍵字。

只需刪除public關鍵字。

更正代碼:

function upload($uid = 'public') 
+0

不僅「我認爲你正在定義公共功能」,他這樣做:D – Rizier123