Php 上载多个图像时出错
我正在尝试一次上传多张图片,目前我得到的是:Php 上载多个图像时出错,php,image,file-upload,image-uploading,Php,Image,File Upload,Image Uploading,我正在尝试一次上传多张图片,目前我得到的是: if(isset($_POST['submit'])) { $file_name=$_FILES["image"]["name"]; foreach($file_name as $files) { $target_path = "Sub_uploads/".$files; if(move_uploaded_file($
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(isset($\u POST['submit']))
{
$file\u name=$\u FILES[“image”][“name”];
foreach($file\u名称为$files)
{
$target_path=“Sub_uploads/”$文件;
如果(移动上传的文件($files[“image”][“tmp\u name”],$target\u path))
{
$target_path=“Sub_uploads/”$文件;
$sql=“插入产品图像(图像)值(“$target\u path”)”;
$query=mysql\u查询($sql);
}
}
echo“警报(‘插入数据’);document.location='Sub_CateGory_image.php';
}
?>
似乎错误发生在这一行:
如果(移动上传的文件($files[“image”][“tmp\u name”],$target\u 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>";
}
if(isset($\u POST['submit']))
{
$file\u name=$\u文件;
foreach($file\u名称为$files)
{
$target_path=“Sub_uploads/”$files[“image”][“name”];
如果(移动上传的文件($files[“image”][“tmp\u name”],$target\u path))
{
$target_path=“Sub_uploads/”$files[“image”][“name”];
$sql=“插入产品图像(图像)值(“$target\u path”)”;
$query=mysql\u查询($sql);
}
}
echo“警报(‘插入数据’);document.location='Sub_CateGory_image.php';
}
当您迭代名称的数组时,连接的tmp\u name
属性将具有与当前迭代的名称相同的键。因此,将键添加到您的foreach
并在该键下获得tmp\u名称
:
$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);
}
}
请指定报告的错误。只需将$\u文件[“image”][“tmp\u name”]替换为$FILES[“image”][“tmp\u name”]我尝试了此操作,但无效。$FILES
是一个字符串。如果您不知道
数组的结构,请不要回答。您编辑的答案仍然不正确。