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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考