上传多张图片出错
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
具有相同的键。因此,将 key
添加到您的 foreach
并在此键下获得 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);
}
}
我正在尝试一次上传多张图片,这是我目前所拥有的:
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
具有相同的键。因此,将 key
添加到您的 foreach
并在此键下获得 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);
}
}