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.