【发布时间】:2014-08-12 00:45:03
【问题描述】:
好的,所以我正在做这个项目,它是一个文件存储库,现在我正在尝试制作上传文件的功能,例如 .pdf,我遵循了以下指南 http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
但我仍然遇到以下问题:页面一直在加载。当我去检查我的数据库时,标题和内容已存储,但格式、大小和内容只是保持为 0 作为值。
所以我使用的是 MVC 文件系统。
我的表由以下组成
CREATE TABLE IF NOT EXISTS `FoldersFiles` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(50) NOT NULL,
`Description` varchar(255) NOT NULL,
`Tag` varchar(20) NOT NULL,
`FileSize` int(11) DEFAULT NULL COMMENT 'The size is calculated in MD',
`UploadedDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastEditDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`Format` int(6) DEFAULT NULL,
`UserID` int(11) NOT NULL,
`ParentID` int(11) DEFAULT NULL,
`Content` longblob,
PRIMARY KEY (`ID`),
KEY `UserID` (`UserID`,`ParentID`),
KEY `ParentID` (`ParentID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=83 ;
还有允许用户上传新文件的视图:
<form method="post" class="basic-frm" id="newFolder" action="../Controller/folders.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<label>
<span>Title:</span>
<input id="title" type="text" name="filetitle"/>
</label>
<label>
<span>Description:</span>
<input id="description" type="text" name="filedescription"/>
</label>
<label>
<span>Tag:</span>
<input id="tag" type="text" name="filetag" />
</label>
<label>
<input id="file" type="file" name="file" />
<input id="submit" type="submit" name="submit" class="button"/>
</label>
</form>
我的控制器如下:
if ((isset($_POST['filetitle'])) && (isset($_POST['filedescription'])) && (isset($_POST['filetag'])) && (isset($_FILES['file'])))
{
$title = $_POST['filetitle'];
$description = $_POST['filedescription'];
$tag = $_POST['filetag'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size'];
$format = $_FILES['file']['type'];
$clean = array();
#checking if the data is clean.
if((ctype_alnum(str_replace(array(' ', "'", '-'), '',$title))) && (ctype_alnum(str_replace(array(' ', "'", '-'), '',$description))) && (ctype_alnum(str_replace(array(' ', "'", "-"), '',$tag))))
{
$clean['title'] = $title;
$clean['description'] = $description;
$clean['tag'] = $tag;
}
if((isset($clean['title'])) && (isset($clean['description'])) && (isset($clean['tag'])))
{
$fp = fopen($fileTmpName, 'r');
$content = fread($fp, filesize($fileTmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
require_once('../Model/Folder.php');
$folder = new Folder();
if(!isset($_SESSION['parentID']))
{
$rootID = $folder->getRoot($_SESSION['userID']);
}
else
{
$rootID = $_SESSION['parentID'];
}
$folder->setInfo($_SESSION['userID'],$clean['title'],$clean['description'],$clean['tag'],$rootID,$size,$format,$content);
header('Location: ../View/folders.php');
var_dump($clean);
}
else
{
echo "Wrong data";
}
}
setInfo 方法:
public function setInfo($userID,$title,$description,$tags,$parentID,$fileSize,$format,$content)
{
$this->userID=$userID;
$this->title= $title;
$this->description = $description;
$this->tag = $tags;
$this->format = $format;
$this->fileSize = $fileSize;
$this->content = $content;
$dbh = new PDO($this->dsn,$this->dbUser,$this->dbpassword);
$insertQuery = <<<'insertQuery'
INSERT INTO `FoldersFiles` (`Title`,`Description`,`Tag`,`UserID`,`ParentID`,`LastEditDate`,`FileSize`,`Format`,`Content`)
VALUES(?,?,?,?,?,?,?,?,?)
insertQuery;
$dateFormat = "Y-m-d H:i:s";
$time = date($dateFormat);
$sth = $dbh->prepare($insertQuery);
$sth->execute(array($title,$description,$tags,$userID,$parentID,$time,$fileSize,$format,$content));
}
public function getInfo($userID, $parentId )
{
$this->userID = $userID;
$dbh = new PDO($this->dsn,$this->dbUser,$this->dbpassword);
$selectQuery = <<<'selectQuery'
SELECT * FROM `FoldersFiles` WHERE `userID` = ? AND `ParentID` = ?
selectQuery;
$sth = $dbh->prepare($selectQuery);
$sth->execute(array($userID,$parentId));
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
所以问题又是,当我尝试上传文件时,它只是一直在加载,在我去 phpmyadmin 检查我的数据后,标题和描述已插入,但 Filze 大小、内容和格式保持为 0。
【问题讨论】:
-
在每个 PDO 语句之后进行一些错误处理,尤其是存储 blob 的语句
-
我已经尝试过了,因为它没有返回错误,而且它是有道理的,因为它是正常插入其他值。
-
您是否检查过您的 MySQL 错误日志。你确定它挂在哪里了吗
-
我会尝试这样做,再给我一个需要弄清楚如何在 Ubuntu 上做到这一点 :)
-
别打扰了。 Mysql 大概不会落后。这是一个插入。在您的 ubuntu 上登录并在插入时运行“顶部”。您会看到 PHP 遇到了困难。插入物很轻,应该不会给单个插入物带来压力。