我对创建的上传文件的类有问题。这个类有一个父类,它是数据库访问层。上传的视频看起来是这样的:PDO错误 - 调用一个非对象的成员函数prepare()
<?php
class upload extends bao {
// general variables
private $basePath = '../../uploads/';
private $fullMaxWidth = 1000;
private $mediumMaxWidth = 500;
private $smallMaxWidth = 250;
private $errors = 0;
private $difference = 0;
private $debugging = false;
private $maxFileSize = 100000000;
// current file variables
private $currFileName = '';
private $currFileType = '';
private $currFileTmpName = '';
private $currFileError = 0;
private $currFileSize = 0;
private $currExt = '';
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
This will overwrite any default values with whats supplied
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
public function __construct($params = null) {
if($this -> debugging) {
echo '__construct method';
}
if($params) {
foreach($params as $key => $val) {
$this -> $key = $val;
}
}
$this -> initiate();
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Initiates the upload process
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
private function initiate() {
if($this -> debugging) {
echo 'initiate method';
}
for($i = 0; $i < count($_FILES['files']['name']); $i++) {
$fileCheck = $this -> checkFileSize($_FILES['files']['size'][$i]);
if(!$fileCheck) {
$this -> errors++;
}
$this -> currFileType = $_FILES['files']['type'][$i];
$this -> currFileTmpName = $_FILES['files']['tmp_name'][$i];
$fileExtPath = pathinfo($_FILES['files']['name'][$i]);
$this -> currExt = strtolower($fileExtPath['extension']);
if($this -> errors < 1) {
$this -> switcher();
}
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Runs a check to see what the file size is
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
private function checkFileSize($fileSize) {
if($this -> debugging) {
echo 'checkFileSize method';
}
if($fileSize <= $this -> maxFileSize) {
return true;
} else {
return false;
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Determines which type of file it is
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
private function switcher() {
if($this -> debugging) {
echo 'switcher method';
}
if($this -> currFileType == 'image/gif' || $this -> currFileType == 'image/png' || $this -> currFileType == 'image/jpeg') {
$this -> image();
} else {
$this -> other();
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Uploads all image files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
private function image() {
if($this -> debugging) {
echo 'image method';
}
$rand = rand(1000, 10000);
$newFullName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-full.'. $this -> currExt;
$newMediumName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-medium.'. $this -> currExt;
$newSmallName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-small.'. $this -> currExt;
$fullPath = $this -> basePath . $newFileName;
$info = getimagesize($this -> currFileTmpName);
$fullWidth = $this -> getNewWidth($this -> fullMaxWidth, $info[0]);
$mediumWidth = $this -> getNewWidth($this -> mediumMaxWidth, $info[0]);
$smallWidth = $this -> getNewWidth($this -> smallMaxWidth, $info[0]);
$fullDif = $this -> setDifference($this -> fullMaxWidth, $info[0]);
$mediumDif = $this -> setDifference($this -> mediumMaxWidth, $info[0]);
$smallDif = $this -> setDifference($this -> smallMaxWidth, $info[0]);
$fullHeight = $this -> getNewHeight($info[1], $fullDif);
$mediumHeight = $this -> getNewHeight($info[1], $mediumDif);
$smallHeight = $this -> getNewHeight($info[1], $smallDif);
// set debugging to try to output new image sizes
if($this -> debugging) {
echo $fullWidth .' - '. $fullHeight .'<br/>';
echo $mediumWidth .' - '. $mediumHeight .'<br/>';
echo $smallWidth .' - '. $smallHeight .'<br/>';
}
$smallImage = imagecreatetruecolor($smallWidth, $smallHeight);
$mediumImage = imagecreatetruecolor($mediumWidth, $mediumHeight);
$fullImage = imagecreatetruecolor($fullWidth, $fullHeight);
switch($info['mime']) {
case 'image/gif':
$smallImg = imagecreatefromgif($this -> currFileTmpName);
$mediumImg = imagecreatefromgif($this -> currFileTmpName);
$fullImg = imagecreatefromgif($this -> currFileTmpName);
break;
case 'image/png':
$smallImg = imagecreatefrompng($this -> currFileTmpName);
$mediumImg = imagecreatefrompng($this -> currFileTmpName);
$fullImg = imagecreatefrompng($this -> currFileTmpName);
break;
default:
$smallImg = imagecreatefromjpeg($this -> currFileTmpName);
$mediumImg = imagecreatefromjpeg($this -> currFileTmpName);
$fullImg = imagecreatefromjpeg($this -> currFileTmpName);
break;
}
imagecopyresampled($smallImage, $smallImg, 0, 0, 0, 0, $smallWidth, $smallHeight, $info[0], $info[1]);
imagecopyresampled($mediumImage, $mediumImg, 0, 0, 0, 0, $mediumWidth, $mediumHeight, $info[0], $info[1]);
imagecopyresampled($fullImage, $fullImg, 0, 0, 0, 0, $fullWidth, $fullHeight, $info[0], $info[1]);
if($info['mime'] == 'image/gif') {
imagegif($smallImage, $this -> basePath . $newSmallName);
imagegif($mediumImage, $this -> basePath . $newMediumName);
imagegif($fullImage, $this -> basePath . $newFullName);
} elseif($info['mime'] == 'image/png') {
imagepng($smallImage, $this -> basePath . $newSmallName, 8);
imagepng($mediumImage, $this -> basePath . $newMediumName, 8);
imagepng($fullImage, $this -> basePath . $newFullName, 8);
} else {
imagejpeg($smallImage, $this -> basePath . $newSmallName, 80);
imagejpeg($mediumImage, $this -> basePath . $newMediumName, 80);
imagejpeg($fullImage, $this -> basePath . $newFullName, 80);
}
// insert into database
$stringQuery = "INSERT INTO uploads (uid,smallFilename,mediumFilename,fullFilename,typeFile,status,createdOn,modifiedOn)VALUES(:uid, :smallFilename, :mediumFilename, :fullFilename, :typeFile, :status, :createdOn, :modifiedOn)";
$binding = array(":uid" => $_COOKIE['userId'], ":smallFilename" => $newSmallName, ":mediumFilename" => $newMediumName, ":fullFilename" => $newFullName, ":typeFile" => $this -> currExt, ":status" => "new", ":createdOn" => currentTime, ":modifiedOn" => currentTime);
$insertDB = parent::query($stringQuery, $binding);
if(!$insertDB) {
$this -> errors++;
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Uploads all files minus images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
private function other() {
if($this -> debugging) {
echo 'other method';
}
$rand = rand(1000, 10000);
$newFullName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'.'. $this -> currExt;
$fullPath = $this -> basePath . $newFullName;
if(move_uploaded_file($this -> currFileTmpName, $fullPath)){
$stringQuery = "INSERT INTO uploads (uid,fullFilename,title,description,keywords,typeFile,uploadedType,uploadedId,status,createdOn,modifiedOn)VALUES(:uid,:fullFilename,:typeFile,:uploadedType,:uploadedId,:status,:createdOn,:modifiedOn)";
$binding = array(":uid" => $_COOKIE['userId'], ":fullFilename" => $newFullName, ":typeFile" => $this -> currExt, ":uploadedType" => "", ":uploadedId" => "", ":status" => "new", ":createdOn" => currentTime, ":modifiedOn" => currentTime);
$insertDB = parent::query($stringQuery, $binding);
if(!$insertDB) {
$this -> errors++;
}
} else {
echo 'There was an error';
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Utility methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
// if image does not match size set the difference
private function setDifference($fullwidth, $width) {
if($this -> debugging) {
echo 'setDifference method';
}
return $fullwidth/$width;
}
// calculate the new width
private function getNewWidth($fullwidth, $width) {
if($this -> debugging) {
echo 'getNewWidth method';
}
$this -> difference = $fullwidth/$width;
return round($width * $this -> difference);
}
// calculate the new height
private function getNewHeight($height, $diff) {
if($this -> debugging) {
echo 'getNewHeight method';
}
$newHeight = round($height * $diff);
return $newHeight;
}
// creates new image for 3 sizes
private function createImageTrueFull($width, $height) {
if($this -> debugging) {
echo 'createImageTrueFull method';
}
return imagecreatetruecolor($width, $height);
}
}
?>
基础数据库访问层看起来是这样的:
<?php
class bao {
private $environments = array(
'development' => array(
),
'test' => array(
),
'review' => array(
),
'production' => array(
),
);
private $url = '',
$host = '',
$dbname = '',
$user = '',
$pass = '';
/*
Variable for connection
*/
private $connection;
/*
Init method
*/
public function __construct($params = null) {
$this -> setConnectionData();
$this -> overwriteSettings($params);
$connection = $this -> connect();
}
public function setConnectionData() {
if(strpos($_SERVER['HTTP_HOST'], 'dev')) {
$envVariable = 'development';
} elseif(strpos($_SERVER['HTTP_HOST'], 'test')) {
$envVariable = 'test';
} elseif(strpos($_SERVER['HTTP_HOST'], 'review')) {
$envVariable = 'review';
} else {
$envVariable = 'production';
}
$this -> url = $this -> environments[$envVariable]['url'];
$this -> host = $this -> environments[$envVariable]['host'];
$this -> dbname = $this -> environments[$envVariable]['dbname'];
$this -> user = $this -> environments[$envVariable]['user'];
$this -> pass = $this -> environments[$envVariable]['pass'];
}
/*
Overwrite any required values from default
*/
private function overwriteSettings($params) {
if($params) {
foreach($params as $key => $val) {
$this -> $key = $val;
}
}
}
/*
Connect to database and then assign to variable
*/
private function connect() {
$this -> connection = new PDO(sprintf('mysql:host=%s;dbname=%s', $this -> host, $this -> dbname), $this -> user, $this -> pass);
}
public function openQuery($query, $params = null) {
if($params) {
return $this -> query($query, $params);
} else {
return $this -> query($query);
}
}
public function openFetchall($sth) {
return $this -> fetchAll($sth);
}
/*
Runs actual query
*/
protected function query($query, $params = null) {
$sth = $this -> connection -> prepare($query);
if($params) {
$sth -> execute($params);
} else {
$sth -> execute();
}
return $sth;
}
public function openNumRows($sth) {
return $this -> numRows($sth);
}
protected function numRows($sth) {
return $sth -> rowCount();
}
public function openFetch($sth) {
return $this -> fetch($sth);
}
protected function fetch($sth) {
return $sth -> fetch(PDO::FETCH_ASSOC);
}
protected function fetchAll($sth) {
return $sth -> fetchAll(PDO::FETCH_ASSOC);
}
protected function lastId($sth) {
return $this -> connection -> lastInsertId();
}
}
?>
现在,当我运行该脚本,我得到这个错误:
Fatal error: Call to a member function prepare() on a non-object in /home/content/51/8932751/html/sites/clients/OwnerBusinessLoans/www/dev/lib/helpers/bao.php on line 118
哪个我不不明白为什么,因为我只是像我所有其他查询一样运行我的upload
类中准备好的查询,并且他们运行得很好,没有显示此错误。关于语法和我做事的方式,这是我为其他10多个类做的方式,我从来没有收到任何错误。
在我的bao
类中,我删除了连接的详细信息,但他们在那里测试应用程序。
起初我以为这是查询语法或什么,所以我改变,检查和重新检查我所有的语法,看起来不错。如果这是我的PDO实例未定义,我不明白为什么,因为它的定义方式相同,并在我所有其他类中使用相同的方法。
任何帮助将不胜感激,我一直在我的大脑上,我觉得它必须是小事!
感谢
太感谢你了,我在另一个类中遇到过,但在稍微不同的方式。一旦我打电话给结构,就完美了! –