简体   繁体   English

如何将带有路径的上传文件名存储到数据库中

[英]How to store uploaded file name with path into database

I'm using dropzone api to upload a image file(drag and drop). 我正在使用dropzone api上传图像文件(拖放)。 When I upload the file, the form automatically gets submitted. 当我上传文件时,表单会自动提交。 But I have two more text field also there in my form. 但是我的表格中还有两个文本字段。 And also I want to store the uploaded file path into database. 而且我还想将上传的文件路径存储到数据库中。 How can I do that. 我怎样才能做到这一点。 This is my code. 这是我的代码。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<!-- Add Dropzone -->
<link rel="stylesheet" type="text/css" href="css/dropzone.css" />
<script type="text/javascript" src="js/dropzone.js"></script>
</head>
<body>
<h1>Drag&amp;Drop Multiple Files Upload using DropzoneJS and PHP by CodexWorld</h1>
<div class="image_upload_div">
    <form action="upload.php" class="dropzone">
    </form>
        <input type="text" name="text1">
        <input type="text" name="text2">
</div>  
</body>
</html>

My upload.php file 我的upload.php文件

<?php
   if(!empty($_FILES)){

//database configuration
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbName = 'sample';
//connect with the database
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if($mysqli->connect_errno){
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$targetDir = "uploads/";
$fileName = $_FILES['file']['name'];
$targetFile = $targetDir.$fileName;
if(move_uploaded_file($_FILES['file']['tmp_name'],$targetFile)){
    //insert file information into db table
    $conn->query("INSERT INTO files (file_name, uploaded) VALUES('".$fileName."','".date("Y-m-d H:i:s")."')");
  }

}
?>

use this way 用这种方式

 <input type="file" name="file" id="file" /> <input type="submit" value="Upload Image" name="submit"> $file=$_FILES['file']['name']; $dest="uploads/$file"; $src=$_FILES['file']['tmp_name']; move_uploaded_file($src,$dest); $result=mysql_query("insert into tablename(dbfieldname) values('$dest')"); 

First you have to create a div and remove the dropzone class from the form to stop it attaching to the entire form. 首先,您必须创建一个div并从表单中删除dropzone类以阻止它附加到整个表单。

 <div class="dropzone dropzone-previews dzUpload" id="my-awesome-dropzone" name = "files"></div>

Then you must change the javascript to attach the dropzone programatically. 然后你必须改变javascript以编程方式附加dropzone。

 var dz = new Dropzone('#my-awesome-dropzone',{
         url: "fileUploader.php",
         ...

When the form submits, you must prevent the default action (in this case the form submitting.) 表单提交时,您必须阻止默认操作(在本例中为表单提交。)

 $('#form').on('submit', function(event) {
      if(dz.getQueuedFiles().length > 0) {
           event.preventDefault(); 
           dz.processQueue(); 
      }
       });

Now, instead you are stopping the form from submitting and you are processing the queue, this causes the 'fileUploader.php' script to run which you can use to validate the files. 现在,您要停止提交表单并处理队列,这会导致运行“fileUploader.php”脚本,您可以使用该脚本验证文件。

If you are happy with the files you can move them to your desired location and then return the filenames by storing them as an array and echoing json_encode($files) 如果您对文件感到满意,可以将它们移动到所需位置,然后通过将它们存储为数组并回显json_encode($ files)来返回文件名

This will give you a response in your javascript which you then use the JSON.parse() function to convert it into an object. 这将在您的javascript中给出响应,然后您使用JSON.parse()函数将其转换为对象。

Now you have the response so to add the file to your database, you can create an extra input in your form with the hidden attribute so the user cant see it. 现在您有了响应以便将文件添加到数据库中,您可以使用隐藏属性在表单中创建额外的输入,以便用户无法看到它。 You can then send the filenames from your javascript to the hidden input box in your form by using jQuery to do this. 然后,您可以使用jQuery将javascript中的文件名发送到表单中的隐藏输入框。

 $("#hiddenInput").val(files);

If you do this for each file, you could append the hidden input each time. 如果对每个文件执行此操作,则可以每次附加隐藏的输入。 You can submit the form once the queue is complete by using this function. 您可以使用此功能在队列完成后提交表单。

queuecomplete: function(file) {  

 $('#form).submit(); 
 $('#submitButton').click();

},

Now this is just like submitting a normal form, and you can explode the filenames into an array and loop round adding each one to your database 现在这就像提交一个普通的表单一样,你可以将文件名分解为一个数组并循环将每个文件名添加到你的数据库中

If you need any extra help feel free to contact me as my explanation is not great :-) 如果您需要任何额外的帮助,请随时与我联系,因为我的解释不是很好:-)

 <input type="file" name="file" id="file" /> <input type="submit" value="Upload Image" name="submit"> <?php $file=$_FILES['file']['name']; $dest="uploads/$file"; $src=$_FILES['file']['tmp_name']; move_uploaded_file($src,$dest); $result=mysql_query("insert into tablename(dbfieldname) values('$dest')"); ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM