将Pdf文件上传到数据库

时间:2017-09-11 07:03:39

标签: javascript php html css mysql

我必须将所有文件上传到我的数据库中。我的数据库在服务器上可用。目前我正在尝试上传pdf文件,但我无法上传 它。我尝试了谷歌的许多代码,但到目前为止还没有为我工作的代码。如何将pdf文件上传到我的数据库?

<?php

if(isset($_POST["submit1"]))
 {

echo $filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;

$FileType = pathinfo($newname,PATHINFO_EXTENSION);

if($FileType == "pdf")
{
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
{

$filesql = "INSERT INTO user(pdf) VALUES('$filename') WHERE id='$id'";
$query = mysqli_query($conn, $filesql);
if (isset($query))
{
echo 'File Uploaded';
} else
{
 echo 'Something went Wrong';
 }
 }
 else
 {

 echo "<p>Upload Failed.</p>";
 }


}
else
{
echo "<p>Class notes must be uploaded in PDF format.</p>";
}

}
?>

1 个答案:

答案 0 :(得分:0)

我认为如果定义了$ id,你应该进行UPDATE而不是INSERT INTO:

    <?php

if(isset($_POST["submit1"]))
 {

echo $filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;

$FileType = pathinfo($newname,PATHINFO_EXTENSION);

if($FileType == "pdf")
{
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
{

$filesql = "UPDATE user SET pdf ='$filename' WHERE id='$id'";
$query = mysqli_query($conn, $filesql);
if (isset($query))
{
echo 'File Uploaded';
} else
{
 echo 'Something went Wrong';
 }
 }
 else
 {

 echo "<p>Upload Failed.</p>";
 }


}
else
{
echo "<p>Class notes must be uploaded in PDF format.</p>";
}

}
?>