我正在尝试将pdf上传到数据库。我一直收到“上传文件时出错”错误。非数据也会插入到数据库中。
请问那些对PHP和SQL不那么陌生的人看看并告诉我哪里出错了。
稍后我将尝试将几个pfds合并在一起,然后将新的合并pdf保存到数据库中。
表格的SQL
CREATE TABLE `tbl_request` (
`InvoiceNum` int(11) NOT NULL,
`userEmail` varchar(25) NOT NULL,
`Progress` enum('Uploaded','Verified and Awaiting Payment','Submitted to Department','Closed') NOT NULL DEFAULT 'Uploaded',
`UploadDate` datetime NOT NULL,
`FileName` varchar(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
PHP代码
<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
if($_SESSION['userRole'] !== ('user'))
{
$user_home->redirect('index.php');
}
$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html class="no-js">
<body>
<div id="body">
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<br /><br />
<?php
if(isset($_GET['success']))
{
?>
<label>File Uploaded Successfully... <a href="view.php">click here to view file.</a></label>
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Problem While File Uploading !</label>
<?php
}
else
{
?>
<label>Please only upload PDF files</label>
<?php
}
?>
</div>
<?php
if(isset($_POST['btn-upload']))
{
if ($_FILES['file']['type'] == "application/pdf") {
$update=date("Ymdhis");
$mail =$row['userEmail'];
$sql="INSERT INTO tbl_request(userEmail,UploadDate) VALUES('$mail','$update)";
mysql_query($sql);
$file = $update."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$folder="uploads/";
$new_file_name = strtolower($file);
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="UPDATE tbl_uploads SET(FileName) VALUES('$final_file') WHERE UploadDate = $update";
mysql_query($sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
else{
?>
<script>
alert('Only pdf files may be uploaded');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
</body>
</html>
答案 0 :(得分:0)
所以我最终得到了我的代码,这里适用于任何可能需要在将来做同样事情的人。
<?php
if(isset($_POST['btn-upload']))
{
if ($_FILES['file']['type'] == "application/pdf") {
$link = mysqli_connect("localhost", "root", "", "test");
$update=date("Ymdhis");
$mail =$row['userEmail'];
$sql="INSERT INTO tbl_request(userEmail,uploadDate) VALUES('$mail','$update')";
mysqli_query($link,$sql);
$file = $update."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$folder="upload/";
$final_file = strtolower($file);
//$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file)) {
$sql2="UPDATE tbl_request SET FileName ='".$final_file."' WHERE uploadDate = '".$update."'";
mysqli_query($link,$sql2);
?>
<script>
alert('successfully uploaded');
//window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
//window.location.href='index.php?fail';
</script>
<?php
}
}
else{
?>
<script>
alert('Only pdf files may be uploaded');
//window.location.href='index.php?fail';
</script>
<?php
}
}
?>
我想改变的两件事:
$link = mysqli_connect("localhost", "root", "", "test");
mysqli_query($link,$sql);
我确信有办法从dbconfig.php获取此信息(我只是不确定如何)
我希望人们能够上传3个pdf,然后使用php代码将它们合并为一个然后像上面一样保存的代码。
答案 1 :(得分:-1)
从技术上讲,您不是将pdf存储在数据库中,而是仅存储对文件存储位置的引用。因此,为了得到您所获得的错误,您的if(move_uploaded_file($file_loc,$folder.$final_file))
必定会失败,可能是路径问题,看起来您正在指定相对路径,
try an absolute (full path) ie windows C:\folder\ or linux /home/user/public_html/uploads/