[英]Insert the form values from an array into a database
I have created a form that allows a user to upload multiple files to a directory. 我创建了一个表单,该表单允许用户将多个文件上载到目录。 Now I am trying to grab the names of the files from the form input fields and store them into a database. 现在,我试图从表单输入字段中获取文件名并将其存储到数据库中。
I am trying to get the array values of the input fields ie; 我正在尝试获取输入字段的数组值,即; the ['file']['name'] and the ['file']['tmp_name'] and insert them into a database. ['file'] ['name']和['file'] ['tmp_name'],然后将它们插入数据库。 With the below code I have only managed to get the word "array" to be inserted into the database and not the actual names of the files themselves. 使用下面的代码,我仅设法将单词“ array”插入到数据库中,而不是文件本身的实际名称。
I think I need to loop through the array using a foreach loop but I am uncertain as to how to go about writing it and where to insert it into my code. 我想我需要使用foreach循环遍历数组,但是我不确定如何编写它以及将其插入到我的代码中。 Below is the code so far: 下面是到目前为止的代码:
#connect to the database
mysql_connect("localhost", "root", "");
mysql_select_db("masscic");
//Upload Handler to check image types
function is_image($file) {
$file_types = array('jpeg', 'gif', 'bmp'); //acceptable file types
if ($img = getimagesize($file)){
//echo '<pre>';
//print_r($_FILES); //will return an array of information for testing
//print_r($img); //will return an array of information for testing
if(in_array(str_replace('image/', '', $img['mime']), $file_types))
return $img;
}
return false;
}
//form submission handling
if(isset($_POST['submit'])) {
//file variables
$fname = $_FILES['files']['name'];
$ftype = $_FILES['files']['type'];
$fsize = $_FILES['files']['size'];
$tname = $_FILES['files']['tmp_name'];
$ferror = $_FILES['files']['error'];
$newDir = '../uploads/'; //relative to where this script file resides
for ($i = count($fname) -1; $i >=0; $i--) {
if ($ferror[$i] =='UPLOAD ERR OK' || $ferror[$i] ==0)
{
if(is_image($tname[$i]))
{
move_uploaded_file($tname[$i], ($newDir.time().$fname[$i]));
echo '<li><span class="success">'.$fname[$i].' -- image has been accepted<br></span></li>';
}else
echo '<li><span class="error">'.$fname[$i].' -- is not an accepted file type<br></span></li>';
}
}
}
$sqlInsert = mysql_query("INSERT INTO files (file_names) VALUES('$fname')") or die (mysql_error());
Thanks for any help. 谢谢你的帮助。
When uploading multiple files (with the same field name) another dimension is added to the array. 当上传多个文件(具有相同的字段名)时,另一个维度将添加到数组中。 Say I upload a.html and b.html, I'll get: 假设我上传a.html和b.html,我将得到:
$_FILES['files']['name'][0]; // a.html
$_FILES['files']['name'][1]; // b.html
$_FILES['files']['size'][0]; // size of a.html
$_FILES['files']['size'][1]; // size of b.html
To iterate through: 要遍历:
for(var $i = 0; $i < count($_FILES['files']['name']); $i++) {
echo 'File name ' . $_FILES['files']['name'][$i] . ' has size ' . $_FILES['files']['size'][$i];
}
This is explained at http://www.php.net/manual/en/features.file-upload.multiple.php 在http://www.php.net/manual/en/features.file-upload.multiple.php中对此进行了解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.