dsdfd2322 2017-12-06 07:29
浏览 68
已采纳

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

I'm trying to figure out from a long time to print my file name in the MySQL database, file is uploading successfully into the path folder but unable to insert the file name into the database. below is my code

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条回答 默认 最新

  • drmet46444 2017-12-06 07:32
    关注

    Just Add Following Code

    $filename= $_FILES["fileToUpload"]["name"];
    $query = INSERT INTO `table`(`image`) VALUES ("$filename");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料