English 中文(简体)
如何上载多个档案
原标题:how to upload multiple files

我愿在我的网站上张贴一些图像。

在我只使用一个图像时,它为单一员额工作。 因为它试图使用多种图像,它会更动地工作,而我也想理解为什么这样做。 a. 显示我的法典:

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 ; 
                }

            }

还有一种办法可以缩短这一法典? 非常感谢。

最佳回答

You are doing it so wrong. To much redundancy in your code

$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);
   //...


}
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签