[英]Error in uploading multiple images
我正在尝试一次上传多张图片,这是到目前为止的内容:
if(isset($_POST['submit']))
{
$file_name=$_FILES["image"]["name"];
foreach($file_name as $files)
{
$target_path = "Sub_uploads/".$files;
if(move_uploaded_file($files["image"]["tmp_name"],$target_path))
{
$target_path="Sub_uploads/".$files;
$sql = "INSERT INTO product_images (image) VALUES ('$target_path')";
$query = mysql_query($sql);
}
}
echo "<script>alert('data inserted');document.location='Sub_CateGory_image.php'</script>";
}
?>
似乎该错误发生在此行: if(move_uploaded_file($files["image"]["tmp_name"],$target_path))
您需要使用ForEach
$files
变量
if(isset($_POST['submit']))
{
$file_name=$_FILES;
foreach($file_name as $files)
{
$target_path = "Sub_uploads/".$files["image"]["name"];
if(move_uploaded_file($files["image"]["tmp_name"],$target_path))
{
$target_path="Sub_uploads/".$files["image"]["name"];
$sql = "INSERT INTO product_images (image) VALUES ('$target_path')";
$query = mysql_query($sql);
}
}
echo "<script>alert('data inserted');document.location='Sub_CateGory_image.php'</script>";
}
当您迭代name
数组时,连接的tmp_name
属性将具有与当前迭代name
相同的键。 因此,向您的foreach
添加一个key
,并在此密钥下获得一个tmp_name
:
$file_name = $_FILES["image"]["name"];
foreach($file_name as $key => $files) // add `$key` here
{
$target_path = "Sub_uploads/".$files;
// use `$key` to get connected `tmp_name`
if(move_uploaded_file($_FILES["image"]["tmp_name"][$key], $target_path))
{
$target_path="Sub_uploads/".$files;
$sql = "INSERT INTO product_images (image) VALUES ('$target_path')";
$query = mysql_query($sql);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.