php 中的图片上传和重命名错误
Error in image uploading and renaming in php
表格
<form action="product.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="input3" multiple>
<input type="submit" value="Add Product" name="submit" class="button2">
</form>
product.php
$image = $_FILES['image'];
$i = 1 ;
foreach ($image as $new_image)
{
print_r($new_image);
echo '<br>';
$dir_path_up = 'assets/images/product_images/'.$model."/";
$target_file = $dir_path_up . basename($new_image);
$new_name= $dir_path_up .$i.".jpg";
move_uploaded_file($_FILES["image"]["tmp_name"], $new_name);
$i++;
}
我使用上面的代码重命名图像并将图像上传到特定的($model)目录。上传单张图片时,一切看起来都很完美。但是当我上传多张单张图片时,最后一张图片只会重命名和上传。 (示例:如果我上传 3 张图片,最后一张图片只会上传,并且会重命名为 1.jpg
。之前的其他图片不会上传。)
这里面有什么问题吗??我对此感到震惊。
当您上传图片时,最后一张图片将被上传,只是因为您没有正确处理它。在此之前尝试 print_r($_FILES)
以查看实际结构。
您需要使用
<input type="file" name="image[]" />
你应该拥有的样本结构:
数组
(
[image] => Array
(
[name] => Array
(
[0] => architectural-245a.jpg
[1] => BeaverMeadow_EN-US12190942812_1920x1200.jpg
[2] => cool-chelsea-wallpaper-25403-26085-hd-wallpapers.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => D:\xampp\tmp\phpE4FF.tmp
[1] => D:\xampp\tmp\phpE52F.tmp
[2] => D:\xampp\tmp\phpE54F.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 689711
[1] => 642453
[2] => 396336
)
)
)
那你就好好处理吧
试试这个代码:
第一个问题是您用于上传文件的 input(type= file)
字段的名称。您的表格实际上只上传了最后选择的一个文件。这就是你面临这个问题的原因。
所以解决这个重命名为image[]
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image[]" class="input3" multiple> <!-- As you are uploading multiple file so u have to define name as array like =image[] -->
<input type="submit" value="Add Product" name="submit" class="button2">
</form>
<?php
if(isset($_POST['submit'])){
$image = $_FILES['image'];
$image = reArrayFiles($image); /* use this function to create a proper $_FILES['image'] array format*/
$i = 1 ;
foreach ($image as $new_image)
{
print_r($new_image);
echo '<br>';
$dir_path_up = "upload";
$target_file = $dir_path_up . basename($new_image['name']);
$new_name= $dir_path_up .$i.".jpg"; /*if you use .jpg here then all the files will be converted into .jpg even if user uploads a txt file or other*/
move_uploaded_file($new_image["tmp_name"], $new_name);
$i++;
}
}
function reArrayFiles(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
?>
表格
<form action="product.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="input3" multiple>
<input type="submit" value="Add Product" name="submit" class="button2">
</form>
product.php
$image = $_FILES['image'];
$i = 1 ;
foreach ($image as $new_image)
{
print_r($new_image);
echo '<br>';
$dir_path_up = 'assets/images/product_images/'.$model."/";
$target_file = $dir_path_up . basename($new_image);
$new_name= $dir_path_up .$i.".jpg";
move_uploaded_file($_FILES["image"]["tmp_name"], $new_name);
$i++;
}
我使用上面的代码重命名图像并将图像上传到特定的($model)目录。上传单张图片时,一切看起来都很完美。但是当我上传多张单张图片时,最后一张图片只会重命名和上传。 (示例:如果我上传 3 张图片,最后一张图片只会上传,并且会重命名为 1.jpg
。之前的其他图片不会上传。)
这里面有什么问题吗??我对此感到震惊。
当您上传图片时,最后一张图片将被上传,只是因为您没有正确处理它。在此之前尝试 print_r($_FILES)
以查看实际结构。
您需要使用
<input type="file" name="image[]" />
你应该拥有的样本结构:
数组 (
[image] => Array
(
[name] => Array
(
[0] => architectural-245a.jpg
[1] => BeaverMeadow_EN-US12190942812_1920x1200.jpg
[2] => cool-chelsea-wallpaper-25403-26085-hd-wallpapers.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => D:\xampp\tmp\phpE4FF.tmp
[1] => D:\xampp\tmp\phpE52F.tmp
[2] => D:\xampp\tmp\phpE54F.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 689711
[1] => 642453
[2] => 396336
)
)
)
那你就好好处理吧
试试这个代码:
第一个问题是您用于上传文件的 input(type= file)
字段的名称。您的表格实际上只上传了最后选择的一个文件。这就是你面临这个问题的原因。
所以解决这个重命名为image[]
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image[]" class="input3" multiple> <!-- As you are uploading multiple file so u have to define name as array like =image[] -->
<input type="submit" value="Add Product" name="submit" class="button2">
</form>
<?php
if(isset($_POST['submit'])){
$image = $_FILES['image'];
$image = reArrayFiles($image); /* use this function to create a proper $_FILES['image'] array format*/
$i = 1 ;
foreach ($image as $new_image)
{
print_r($new_image);
echo '<br>';
$dir_path_up = "upload";
$target_file = $dir_path_up . basename($new_image['name']);
$new_name= $dir_path_up .$i.".jpg"; /*if you use .jpg here then all the files will be converted into .jpg even if user uploads a txt file or other*/
move_uploaded_file($new_image["tmp_name"], $new_name);
$i++;
}
}
function reArrayFiles(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
?>