我正在尝试从HTML表单中获取Array中的FILES,并将它们存储在数据库中。我写了以下代码,它给了我很多错误信息。看起来主要问题是它没有将Array转换为字符串。
请指导我。
Line 27 : $image_name= $_FILES["files"]["name"]; LINE 29: $random_name= rand().$_FILES["files"]["name"];
$ _文件输出
数组([files] =>数组([名称] =>数组([0] => Bracelet_Gold.jpg [1] => Necklaces_Silver.png [2] => Brooches_Gold.png)[type] =>排列 ([0] => image / jpeg [1] => image / png [2] => image / png)[tmp_name] => 数组([0] => F:\ xampp \ tmp \ php599C.tmp [1] => F:\ xampp \ tmp \ php599D.tmp [2] => F:\ xampp \ tmp \ php599E.tmp)[错误] => 数组([0] => 0 [1] => 0 [2] => 0)[size] =>数组([0] => 7150 [1] => 37867 [2] => 314296)))
<body>
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="files[]" />
<input type="file" name="files[]" />
<input type="file" name="files[]" />
<input type="submit" value="Send" />
</p>
</form>
</body>
</html>
<?php
include 'connect.php';
if (isset($_FILES['files'])
|| ($_FILES["files"]["type"] == "image/jpeg"))
{
foreach($_FILES['files']['tmp_name'] as $key=> $tmp_name)
{
//echo $tmp_name."<br>";
echo $image_name= $_FILES["files"]["name"];
$random_name= rand().$_FILES["files"]["name"];
$folder="upload/products/" .$random_name;
move_uploaded_file($_FILES["files"]["tmp_name"],
"upload/products/" . $random_name);
$sql = "Insert into product_images (product_id,name,images)
VALUES ($current_id,'$image_name', '$folder')";
if (mysql_query($sql))
{
echo 'Done';
}
else
{
echo mysql_error();
}
}
}
?>
注意:数组转换为字符串 第27行的F:\ xampp \ htdocs \ CMS \ array_upload.php数组注意:数组到 第29行的F:\ xampp \ htdocs \ CMS \ array_upload.php中的字符串转换
警告:move_uploaded_file()期望参数1为string,array 在第34行的F:\ xampp \ htdocs \ CMS \ array_upload.php中给出
答案 0 :(得分:3)
$ _ FILES [“files”] [“tmp_name”]是您收到的文件的数组 所以你需要这样做: -
move_uploaded_file($_FILES["files"]["tmp_name"][$key],
"upload/products/" . $random_name);
或者您在$ tmp_name中收到当前临时名称,因此您可以使用: -
move_uploaded_file($tmp_name,
"upload/products/" . $random_name);
答案 1 :(得分:1)
第27行&amp; 29试试
$_FILES['files']['name'][$key]
还将move_uploaded_file的第一个参数替换为$ tmp_name
move_uploaded_file($tmp_name,