多个文件上传,如何包含更多字段以及添加数据库的完整路径

时间:2016-08-09 18:40:19

标签: php mysql

我找到了一些代码,它将文件上传到我服务器上的文件夹,然后将路径保存到数据库表列。表格的代码:

 <form method="post" action="add_member.php" enctype="multipart/form-
 data">
<p>Please Upload a Photo of the Member in gif or jpeg format. The file  
name should be named after the Members name. If the same file name is  
uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
        <p>
          Photo:
        </p>
        <input type="hidden" name="size" value="350000">
        <input type="file" name="cert_1">
           <br/>
            <br/>
          <input TYPE="submit" name="upload" title="Add data to the 
Database" value="Add Member"/>
</form>

然后使用算法的脚本将文件移动到上传文件夹,然后将完整路径添加到数据库列

//This is the directory where images will be saved
 $target = "upload";
 $target = $target . basename( $_FILES['cert_1']['name']);

 //This gets all the other information from the form

   $pic=($_FILES['cert_1']['name']);



// Connects to your Database
mysql_connect("host", "db_user", "_db_pass") or         

die(mysql_error()) ;
mysql_select_db("your_db") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO student_biodata_master 
           (cert_1)
           VALUES ('$pic')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['cert_1']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['cert_1']['name']). " has been  
uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}

现在,我需要将文件上载字段的数量至少增加到4,然后分别将文件的完整路径写入数据库列。该脚本运行良好,但只是没有写入数据库的完整路径,所以我可以稍后调用它并显示在我的应用程序中。可以&#34; anygoodbody&#34;帮忙吗?它实际上是我的头脑。

提前感谢您提出的好建议。

Stack Overflow摇滚!

1 个答案:

答案 0 :(得分:0)

您需要支持html5的浏览器才能进行多次上传或使用不同的技术:java applet,flash,silverlight。 jquery有一个用于多次上传的插件:http://blueimp.github.io/jQuery-File-Upload/