我有一个已经运行的表单,它将数据插入到数据库中,我还有4x文件输入用于图像,然后表单添加所有数据,上传图像并重命名图像以匹配下一个ID,并为文件名添加随机结束
这一切都是有意的
但我现在需要将文件名添加到数据库中,有时表单会有1x想象有时4x图像但我不知道如何从下面的代码中将文件名存储在db中。有人可以帮忙吗?我假设数组需要分解为单个文件名,但不知道如何操作。
<?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]','$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/".$txtGalleryName."/".$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/".$txtGalleryName."/".$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/".$txtGalleryName."/".$newFileName);
}
}
else
{
array_push($error,"$file_name, ");
}
}
// end of image upload
echo '<p>This item was added successfully</p>';
}
?>
和我的表格;
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 />
感谢任何帮助:)
答案 0 :(得分:1)
你可以使用$ rename_var = rand('111111','999999');并在文件名之前添加前缀,如$ new_changed_name = $ rename_var。$ _ FILES ['files'] ['name'];然后使用此名称,同时将文件保存在文件夹中,如下所示.. move_uploaded_file('tmp_name','path /'.$ new_changed_name); 并将此$ new_changed_name新名称插入数据库。