PHP和mySQL无法正常工作的文件上传(.pdf),

时间:2014-06-21 11:31:50

标签: php mysql pdf upload

好的,所以我正在做这个项目,这是一个文件存储库,现在我正在尝试上传文件的功能,例如.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 ;

我的View允许用户上传新文件:

<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。

3 个答案:

答案 0 :(得分:0)

我挂起了PHP并且使用了fread,这很可能是导致问题的原因。如果我不得不猜测文件或其数据有问题(没有损坏,但可能不存在)。

尝试制作另一个没有整个MVC的脚本,并尝试从服务器目录而不是FILES中读取它。

如果可行,请使用您的表单和页面请求进行扩展。

如果可以的话,可以部分重建你的脚本。

可能还有很长的路要走,但它始终适用于调试。因为如果你想找到问题则消除其余部分。

答案 1 :(得分:0)

您真的需要/想要将内容上传到数据库吗?我目前正在处理带有音频文件的上传功能,为了避免大文件的sql问题,我们将文件名存储在数据库中,并将实际文件存储在文件系统中,稍后通过php提供。

        if((isset($clean['title'])) && (isset($clean['description'])) && (isset($clean['tag'])))
    {
        if(move_uploaded_file($fileTmpName, $finalLocation)){
            //Handle success
        }else{
            //Handle failure
        }

如果必须上传到数据库。你有没有在sql查询之前设置一个断点来验证变量$ filesize,$ content和$ format是否包含你期望的内容?

答案 2 :(得分:0)

所以我想用一些错误的代码修复问题,但奇怪的是,当我上传pdf其他格式时,它仍然会崩溃!

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'])))
            {
                require_once('../Model/Folder.php');
                $folder = new Folder();

                if(isset($rootID))
                {
                    $rootID = $folder->getRoot($_SESSION['userID']);
                }

                if((isset($_FILES['uploadFile'])) && $_FILES['uploadFile']['size']  > 0)
                {
                    $tmpFileName  = $_FILES['uploadFile']['tmp_name'];
                    $fileSize = $_FILES['uploadFile']['size'];
                    $fileType = $_FILES['uploadFile']['type'];

                    $fp      = fopen($tmpFileName, 'r');
                    $content = fread($fp, filesize($tmpFileName));
                    $content = addslashes($content);
                    fclose($fp);
                }

                $folder->setInfo($_SESSION['userID'],$clean['title'],$clean['description'],$clean['tag'],$rootID,$fileSize,$fileType,$content);
                header('Location: ../View/folders.php');
            }'

我在使用supergoabal $ _FILES

的代码时遇到了问题