如何将文件名插入mysql数据库

时间:2017-12-06 07:29:07

标签: php mysql html5 mysqli

我试图在很长一段时间内弄清楚在MySQL数据库中打印我的文件名,文件是否成功上传到路径文件夹但无法将文件名插入数据库。下面是我的代码

Choose file: <span class="text-danger">Max size 1mb, doc, pdf, jpg, jpeg, png & gif only</span>
            <input type="file" name="fileToUpload" id="fileToUpload" required> 
            <input type="hidden" value="Upload Image" name="submit">

     <?php
 $target_dir = "../online/upload_files/";
 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 $uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
    echo '<span style="color:#C0392B;text-align:center; size:45"> Sorry, 
 file already exists.</span>';
  $uploadOk = 0;
}

 // Check file size
 if ($_FILES["fileToUpload"]["size"] > 1048576) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
 }

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != 
"jpeg"
 && $imageFileType != "gif" && $imageFileType != "doc" && $imageFileType != 
"docx" 
&& $imageFileType != "pdf"&& $imageFileType != "xlsx") {
   echo  '<span style="color:#C0392B;text-align:center;">Sorry, only JPG, 
 JPEG, PNG, doc, pdf & GIF  files are allowed.</span>' ;
    $uploadOk = 0;
 }

 // Check if $uploadOk is set to 0 by an error
 if ($uploadOk == 0) {
     echo '<span style="color:#C0392B;text-align:center;">Sorry, your file 
 was not uploaded.</span>';

 // if everything is ok, try to upload file
  } else {
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
 $target_file)) {
         echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has 
 been uploaded. & <b>Proposal Submited Successfully.</b>";
     } else {
         echo '<span style="color:#C0392B;text-align:center;"> Sorry, there 
 was an error uploading your file.</span>';
     }
 }

 ?>

1 个答案:

答案 0 :(得分:1)

只需添加以下代码

$filename= $_FILES["fileToUpload"]["name"];
$query = INSERT INTO `table`(`image`) VALUES ("$filename");