2013-10-08 5 views
0

하나가 아닌 여러 파일을 업로드하고 싶습니다. 단일 파일 (이미지)을 업로드하는 방법을 알고 있습니다. 여러 파일을 업로드하는 데 도움이 필요합니다. 아래에서 하나의 파일을 업로드하는 방법을 볼 수 있습니다.서버에 여러 파일 업로드

는 여기에 추가 내 HTML이다 : 나는 어떤 사람들은 이름 = "파일"로 변경 보았다

<form action="upload_file.php" method="post" enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file" multiple><br> 
    <input type="submit" name="submit" value="Submit"> 
    </form> 

이름 에 = "[] 파일". 하지만 PHP로 업로드하는 방법을 모르겠습니다. 당신이 사용하는 경우 여기

는 PHP 코드

<?php 
    $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $temp = explode(".", $_FILES["file"]["name"]); 
    $extension = end($temp); 

    $random_digit=rand(0000,9999); 

    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/jpg") 
    || ($_FILES["file"]["type"] == "image/pjpeg") 
    || ($_FILES["file"]["type"] == "image/x-png") 
    || ($_FILES["file"]["type"] == "image/png")) 
    && ($_FILES["file"]["size"] < 20000) 
    && in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } 
    else 
    { 

    if (file_exists("upload/" . $_FILES["file"]["name"])) 
    { 
     echo $_FILES["file"]["name"] . " already exists. "; 
    } 
    else 
    { 
    $file_name = $_FILES["file"]["name"]; 
    $ext = pathinfo($file_name, PATHINFO_EXTENSION); // Get the extension of a file 
    $new_file_name = $random_digit . '.' . $ext; 
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_file_name); 
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
    } 
    } 
} 
else 
{ 
echo "Invalid file"; 
} 
?> 
+1

아, 네, 좋은 오래된 표준은 [w3fools] (http://w3fools.com) 완전히 불안하고는 w3fools 사람들이 얼마나 바보 같은 표현을 시도하는 방법에서 완전히 바보 둘 다 스크립트를 업로드 할 수 있습니다. –

+1

[PHP 다중 파일 배열] 가능한 중복 (http://stackoverflow.com/questions/15941592/php-multiple-file-array) –

+0

지금 여러 개의 파일을 추가 할 수 있습니다. 그러나 어쨌든 서버에 추가하기 전에 표시 할 수 있습니까? – EM10

답변

1

업로드 양식에 이름이 같은 입력란이 여러 개 있어야합니다.

는의 호출하자이

는 다음 수신 스크립트에서, 각 하나를 얻기 위해 $ _FILES [] 배열을 반복 할 필요 '[] 업로드'.

$response = ""; 
$i = 0; 
while ((isset($_FILES['upload']['name'][$i])) && ($_FILES['upload']['name'][$i]<>"")){ 
    $error = $_FILES['upload']['error'][$i]; 

    if ($error == 0){ 

     $upfilename = $_FILES['upload']['name'][$i]; 
     //should probably validate your filename here 
      //$upfilename = validate_fname($upfilename) 

     $tmpname = $_FILES['upload']['tmp_name'][$i]; 
     $dirname = "uploads" // your files to be stored in this directory 
     if (move_uploaded_file($tmpname, $dirname."/".$filename)){ 
      $response .= "<br><div class=\"message\">File : $filename : uploaded successfully. Thank you.</div><br>"; 
     }else { 
      $response .= "<br> <div class=\"message\"> There was an error uploading file: $filename "; 
     } 

    } 
    else $response = "<br><div class=\"errormsg\">there was an error - error code: $error</div><br>"; 
    $i++; 
} 
3

입니다 이름 = 당신이 다음과 같이 표시됩니다 print_r($_FILES["file"]) 또는 var_dump($_FILES["file"]) 어레이와 함께 그것을 테스트 할 수 있습니다) ($_FILES["file"]["name"]이 될 것입니다 배열을 "[] 파일" http://staticfloat.com/php-programmieren/multiupload-mit-html5-und-php/는 독일어이지만, 단지 신용 :

Array 
(
[uploads] => Array 
    (
     [name] => Array 
      (
       [0] => hack-attempt.sh 
       [1] => picture.gif 
       [2] => text.txt 
       [3] => virus.exe 
      ) 

     [type] => Array 
      (
       [0] => application/sh 
       [1] => image/jpeg 
       [2] => text/plain 
       [3] => applicatio/octet-stream 
      ) 

     [tmp_name] => Array 
      (
       [0] => /tmp/hack-attempt.sh 
       [1] => /tmp/picture.gif 
       [2] => /tmp/text.txt 
       [3] => /tmp/virus.exe 
      ) 

     [error] => Array 
      (
       [0] => 0 
       [1] => 0 
       [2] => 0 
       [3] => 0 
      ) 

     [size] => Array 
      (
       [0] => 217297 
       [1] => 53600 
       [2] => 6511924 
       [3] => 127662 
      ) 

    ) 

) 

예에서 가져옵니다.