duanjian9148 2014-06-21 11:31
浏览 47

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

Okay, so i am doing this project which is a file repository and right now i am trying to make the functionality of uploading a file for example a .pdf and i have followed the following guide http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx

But i am still having the following problems: The page keeps loading for eternity.When i go check my database the title and stuff is stored but the format, size and content is just stay as 0 as the values.

So i am useing the MVC file system.

My table consist of the following

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 ;

And my View that allows the user to upload a new file:

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

And my controller is the following :

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";
        }
    }

The setInfo method :

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;
        }

So the problem again is when i try to upload a file it just keeps loading for ever and after i go to phpmyadmin to check my data the title and description is inserted but Filze size and content and format stay 0.

  • 写回答

3条回答 默认 最新

  • duanlipeng4136 2014-06-21 12:01
    关注

    I PHP hangs and your using fread, it's most probably the cause of the problem. If I'd have to guess there is something wrong with the file or it's data (not corrupted, but maybe just not there).

    Try to make another script without the whole MVC and try to read it from the server directory instead of from FILES.

    If that works extend it with your form and a page request.

    If that works do a partial rebuild of your script.

    It might be the long way around, but it always works for debugging. Cause if you want to find the problem eliminate the rest.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化