【问题标题】:File upload (.pdf) with PHP and mySQL not working,使用 PHP 和 mySQL 的文件上传 (.pdf) 不起作用,
【发布时间】: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 遇到了困难。插入物很轻,应该不会给单个插入物带来压力。

标签: php mysql pdf upload


【解决方案1】:

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

尝试在没有整个 MVC 的情况下创建另一个脚本,并尝试从服务器目录而不是从 FILES 中读取它。

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

如果可行,请对您的脚本进行部分重建。

这可能是很长的路要走,但它总是适用于调试。因为如果您想找到问题,请消除其余部分。

【讨论】:

  • 好的,我会努力做到的!
  • 祝你好运,我的人,如果你再靠近一点,请告诉我们。如前所述,这可能是恐惧。对它进行谷歌搜索。在某些情况下它有点狡猾。
  • 感谢您的建议,现在开始破解 :) 可能需要我一段时间,因为我还在学习 php。
  • 如果您有具体问题,请告诉我们。我也许可以提供帮助
  • 好的,我想问你“从服务器目录而不是从文件中读取”是什么意思
【解决方案2】:

您真的需要/想要将内容上传到数据库吗?我目前正在研究音频文件的上传功能,为了避免大文件的 sql 问题,我们将文件名存储在 db 中,并将实际文件存储在文件系统上,稍后通过 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 是否包含您所期望的内容?

【讨论】:

  • mmm 我想这也可以,然后在数据库中我将​​存储文件夹的路径?然后我使用 fread 读取文件的内容?
  • 这就是一般的想法。将文件名存储在 db 中(例如:“user_id + unix_timestamp.pdf”)。提供 pdf 时,查询文件名,连接 > $filePath.$filename.,然后使用 fread 提供
  • 今天晚些时候我会试一试,因为我需要做一个文件存储库,它将读取 pdf txt 和 doc 之类的 sot 文件扩展名
【解决方案3】:

所以我设法解决了一些错误代码的问题,但奇怪的是,当我上传 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 代码有问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2015-02-11
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多