我想在我的网站上发布一些图片。
当我只使用一张图片时,它适用于单个帖子。因为我试图使用多个图像它不再工作,我不明白为什么这不起作用。显示我的代码:
if (isset($_POST['var1'];
...
... $Bild1 = $_FILES['image1'];
$Bild2 = $_FILES['image2'];
$Bild3 = $_FILES['image3'];
$Bild4 = $_FILES['image4'];
$Bild5 = $_FILES['image5'];
$Bild6 = $_FILES['image6'];
$Bild7 = $_FILES['image7'];
$Bild8 = $_FILES['image8'];
$Bild9 = $_FILES['image9'];
$Bild10 = $_FILES['image10'];
$errors = array();
$allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
$file_name_1 = $_FILES['image1']['name'];
$file_name_2 = $_FILES['image2']['name'];
$file_name_3 = $_FILES['image3']['name'];
$file_name_4 = $_FILES['image4']['name'];
$file_name_5 = $_FILES['image5']['name'];
$file_name_6 = $_FILES['image6']['name'];
$file_name_7 = $_FILES['image7']['name'];
$file_name_8 = $_FILES['image8']['name'];
$file_name_9 = $_FILES['image9']['name'];
$file_name_10 = $_FILES['image10']['name'];
$split_1 = explode('.', $file_name_1);
$split_2 = explode('.', $file_name_2);
$split_3 = explode('.', $file_name_3);
$split_4 = explode('.', $file_name_4);
$split_5 = explode('.', $file_name_5);
$split_6 = explode('.', $file_name_6);
$split_7 = explode('.', $file_name_7);
$split_8 = explode('.', $file_name_8);
$split_9 = explode('.', $file_name_9);
$split_10 = explode('.', $file_name_10);
$split_1[0] = '1';
$split_2[0] = '2';
$split_3[0] = '3';
$split_4[0] = '4';
$split_5[0] = '5';
$split_6[0] = '6';
$split_7[0] = '7';
$split_8[0] = '8';
$split_9[0] = '9';
$split_10[0] = '10';
$file_basename_1 = $split_1[0];
$file_basename_2 = $split_2[0];
$file_basename_3 = $split_3[0];
$file_basename_4 = $split_4[0];
$file_basename_5 = $split_5[0];
$file_basename_6 = $split_6[0];
$file_basename_7 = $split_7[0];
$file_basename_8 = $split_8[0];
$file_basename_9 = $split_9[0];
$file_basename_10 = $split_10[0];
$file_extension_1 = strtolower(end(explode('.', $file_name_1)));
$file_extension_2 = strtolower(end(explode('.', $file_name_2)));
$file_extension_3 = strtolower(end(explode('.', $file_name_3)));
$file_extension_4 = strtolower(end(explode('.', $file_name_4)));
$file_extension_5 = strtolower(end(explode('.', $file_name_5)));
$file_extension_6 = strtolower(end(explode('.', $file_name_6)));
$file_extension_7 = strtolower(end(explode('.', $file_name_7)));
$file_extension_8 = strtolower(end(explode('.', $file_name_8)));
$file_extension_9 = strtolower(end(explode('.', $file_name_9)));
$file_extension_10 = strtolower(end(explode('.', $file_name_10)));
$file_size_1 = $_FILES['image1']['size'];
$file_size_2 = $_FILES['image2']['size'];
$file_size_3 = $_FILES['image3']['size'];
$file_size_4 = $_FILES['image4']['size'];
$file_size_5 = $_FILES['image5']['size'];
$file_size_6 = $_FILES['image6']['size'];
$file_size_7 = $_FILES['image7']['size'];
$file_size_8 = $_FILES['image8']['size'];
$file_size_9 = $_FILES['image9']['size'];
$file_size_10 = $_FILES['image10']['size'];
$file_tmp_1 = $_FILES['image1']['tmp_name'];
$file_tmp_2 = $_FILES['image2']['tmp_name'];
$file_tmp_3 = $_FILES['image3']['tmp_name'];
$file_tmp_4 = $_FILES['image4']['tmp_name'];
$file_tmp_5 = $_FILES['image5']['tmp_name'];
$file_tmp_6 = $_FILES['image6']['tmp_name'];
$file_tmp_7 = $_FILES['image7']['tmp_name'];
$file_tmp_8 = $_FILES['image8']['tmp_name'];
$file_tmp_9 = $_FILES['image9']['tmp_name'];
$file_tmp_10 = $_FILES['image10']['tmp_name'];
$file_newname_1 = $file_basename_1 .".". $file_extension_1;
$file_newname_2 = $file_basename_2 .".". $file_extension_2;
$file_newname_3 = $file_basename_3 .".". $file_extension_3;
$file_newname_4 = $file_basename_4 .".". $file_extension_4;
$file_newname_5 = $file_basename_5 .".". $file_extension_5;
$file_newname_6 = $file_basename_6 .".". $file_extension_6;
$file_newname_7 = $file_basename_7 .".". $file_extension_7;
$file_newname_8 = $file_basename_8 .".". $file_extension_8;
$file_newname_9 = $file_basename_9 .".". $file_extension_9;
$file_newname_10 = $file_basename_10 .".". $file_extension_10;
$path ='a/b/c/'.$id.'/'.$new_file_id.'/';
if (in_array($file_extension_1, $allowed_extension)=== false){
$errors[] = 'errormessage1';
}
if (in_array($file_extension_2, $allowed_extension)=== false){
$errors[] = 'errormessage2';
}
if (in_array($file_extension_3, $allowed_extension)=== false){
$errors[] = 'errormessage3';
}
if (in_array($file_extension_4, $allowed_extension)=== false){
$errors[] = 'errormessage4';
}
if (in_array($file_extension_5, $allowed_extension)=== false){
$errors[] = 'errormessage5';
}
if (in_array($file_extension_6, $allowed_extension)=== false){
$errors[] = 'errormessage6';
}
if (in_array($file_extension_7, $allowed_extension)=== false){
$errors[] = 'errormessage7';
}
if (in_array($file_extension_8, $allowed_extension)=== false){
$errors[] = 'errormessage8';
}
if (in_array($file_extension_9, $allowed_extension)=== false){
$errors[] = 'errormessage9';
}
if (in_array($file_extension_10, $allowed_extension)=== false){
$errors[] = 'errormessage10';
}
if ($file_size_1 > 2097152){
$errors[] = 'errormessage1';
}
if ($file_size_2 > 2097152){
$errors[] = 'errormessage2';
}
if ($file_size_3 > 2097152){
$errors[] = 'errormessage3';
}
if ($file_size_4 > 2097152){
$errors[] = 'errormessage4';
}
if ($file_size_5 > 2097152){
$errors[] = 'errormessage5';
}
if ($file_size_6 > 2097152){
$errors[] = 'errormessage6';
}
if ($file_size_7 > 2097152){
$errors[] = 'errormessage7';
}
if ($file_size_8 > 2097152){
$errors[] = 'errormessage8';
}
if ($file_size_9 > 2097152){
$errors[] = 'errormessage9';
}
if ($file_size_10 > 2097152){
$errors[] = 'errormessage10';
}
if (empty($errors)) {
if (move_uploaded_file($file_tmp_1, $path . $file_newname_1)) {
echo 'success1';
}
if (move_uploaded_file($file_tmp_2, $path . $file_newname_2)) {
echo 'success2';
}
if (move_uploaded_file($file_tmp_3, $path . $file_newname_3)) {
echo 'success3';
}
}
还有办法缩短这段代码吗?非常感谢。
答案 0 :(得分:5)
你这么做错了。为您的代码提供更多冗余
$allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
foreach($_FILES as $file) {
// Carry on like this
$file_name = $file['name'];
$size = $file['size'];
//However, you might want to extract the extension like this
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
//...
}