试图从数组中提取文件名并插入数据库

时间:2016-04-24 21:58:58

标签: php mysql forms upload

我编写了一些文件上传php代码,用于上传多个文件,在循环中重命名并在表单上提交数据。

我现在正试图拉上传的文件名并将它们插入到数据库中,但是在表单完成时,它们甚至会努力回应它们。

任何人都可以帮我获取新文件名并帮助我将数据存入我的数据库吗?

这是我的一些代码;

<?php
if(isset($_POST['submit'])){ 

// Variables for date&Time logs
$dateLog = date("y-m-d"); // DATE OF  ADDITION
$timeLog = date("H:i:s", time() - 3600);   // TIME OF ADDITION 

// INSERT QUERY
$sql="INSERT INTO $table1 (firstname, lastname, companyname, phone, email, name, make, serial, catagory, price, location, description, sold, operational, year, clear, rip, version, service, dock, loading, available, extras, dateadded, featured)
VALUES
('$_POST[firstname]',
'$_POST[lastname]',
'$_POST[companyname]',
'$_POST[phone]',
'$_POST[email]',
'$_POST[name]',
'$_POST[make]',
'$_POST[serial]',
'$_POST[catagory]',
'$_POST[price]',
'$_POST[location]',
'$_POST[description]',
'No',
'$_POST[operational]',
'$_POST[year]',
'$_POST[clear]',
'$_POST[rip]',
'$_POST[version]',
'$_POST[service]',
'$_POST[dock]',
'$_POST[loading]',
'$_POST[available]',
'$_POST[extras]',
'$dateLog',
'No')";


$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());

// start of image upload
$insert_id = mysql_insert_id() or die("Unable to get insert id for image name.<br>" . mysql_error());

extract($_POST);
    $error=array();
    $extension=array("jpeg","jpg","png","gif");
    foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
            {
                $file_name=$_FILES["files"]["name"][$key];
                $file_tmp=$_FILES["files"]["tmp_name"][$key];
                $ext=pathinfo($file_name,PATHINFO_EXTENSION);
                if(in_array($ext,$extension))
                {
                    if(!file_exists("../images/listings/".$file_name))
                    {
                        $filename=basename($file_name,$ext);
                        $newFileName=$insert_id."_".mt_rand(1, 99999).".".$ext;
                        move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"../images/listings/".$newFileName);
                    }
                    else
                    {
                        $filename=basename($file_name,$ext);
                        $newFileName=$filename.mt_rand(1, 99999).".".$ext;
                        move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"../images/listings/".$newFileName);
                    }
                }
                else
                {
                    array_push($error,"$file_name, ");
                }
            }
// end of image upload
//echo '<p>This item was added successfully</p>';
  print_r($newFileName);

}
?>

和我的表格项目;

1: Upload : <input type="file" name="files[]"/><br />
2: Upload : <input type="file" name="files[]"/><br />
3: Upload : <input type="file" name="files[]"/><br />
4: Upload : <input type="file" name="files[]"/><br />

干杯

1 个答案:

答案 0 :(得分:1)

在php字符串和数组中,使用{$ _POST [&#39; field&#39;]} ..

尝试

$sql="INSERT INTO $table1 (firstname, lastname, companyname, phone, email, name, make, serial, catagory, price, location, description, sold, operational, year, clear, rip, version, service, dock, loading, available, extras, dateadded, featured)
VALUES
('{$_POST['firstname']}',
'{$_POST['lastname']}'
.... continue

并检查是否isset($ _ POST [&#39; field&#39;])是一个好主意。