Php çoklu dosya yükleme

5 Cevap php

Ben birden fazla dosya upload ve bir klasörde saklayın ve yol almak ve veritabanında saklamak istiyorum ... Herhangi bir iyi örnek birden fazla dosya yükleme yapmak için baktım ...

Note: Dosyalar herhangi bir tür olabilir ...

5 Cevap

Ben bu eski bir yazı olduğunu biliyorum ama biraz daha açıklama birden fazla dosya yüklemek için çalışan biri için yararlı olabilir ... İşte yapmanız gerekenler:

  • Input name must be be defined as an array i.e. name="inputName[]"
  • Giriş elemanı var multiple="multiple" ya da sadece multiple gerekir
  • PHP dosyasında sözdizimi kullanın "$_FILES['inputElemName']['param'][index]"
  • Aramak için emin olun empty file names and paths , the array might contain empty strings

Burada bir aşağı ve kirli örneği (sadece ilgili kod gösteren) olduğunu

HTML:

<input name="upload[]" type="file" multiple="multiple" />

PHP:

//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

Bu dışarı yardımcı olur umarım!

Multiple files can be selected and then uploaded using the
<input type='file' name='file[]' multiple>
The sample php script that does the uploading:

<html>
<title>Upload</title>
<?php
    session_start();
    $target=$_POST['directory'];
        if($target[strlen($target)-1]!='/')
                $target=$target.'/';
            $count=0;
            foreach ($_FILES['file']['name'] as $filename) 
            {
                $temp=$target;
                $tmp=$_FILES['file']['tmp_name'][$count];
                $count=$count + 1;
                $temp=$temp.basename($filename);
                move_uploaded_file($tmp,$temp);
                $temp='';
                $tmp='';
            }
    header("location:../../views/upload.php");
?>
</html>

Seçilen dosyalar ile bir dizi olarak alınır

$_FILES['file']['name'][0] storing the name of first file.
$_FILES['file']['name'][1] storing the name of second file.
and so on.

Bu bir dosya yükleme çok farklı değil - $_FILES her türlü yüklenen dosyaları içeren bir dizidir.

PHP kılavuzda bir bölüm var: Uploading multiple files

(Yerine yükleme alanları doldurarak bir seferde birden fazla dosya seçerek) kullanıcının ucunda kolay seçimi ile birden fazla dosya yükleme etkinleştirmek istiyorsanız SWFUpload bakabilirsiniz. Normal bir dosya yükleme formundan farklı çalışır ve ancak, çalışmak için Flash gerektirir.

Basit, sadece kolayca böyle yapabilirsiniz süre döngü sonra, ilk dosyalar dizi saymak, yani

$count = count($_FILES{'item_file']['name']);

şimdi sağ dosyalarının toplam sayısı var.

While döngüsüne böyle yapın:

$i = 0;
while($i<$count)
{
    Upload one by one like we do normally
    $i++;
}

HTML

  1. id='dvFile' ile div oluşturmak;

  2. Bir button oluşturmak;

  3. onclick bu düğme arama fonksiyonu add_more()

JavaScript

function  add_more() {
  var txt = "<br><input type=\"file\" name=\"item_file[]\">";
  document.getElementById("dvFile").innerHTML += txt;
}

PHP

if(count($_FILES["item_file"]['name'])>0)
 { 
//check if any file uploaded
 $GLOBALS['msg'] = ""; //initiate the global message
  for($j=0; $j < count($_FILES["item_file"]['name']); $j++)
 { //loop the uploaded file array
   $filen = $_FILES["item_file"]['name']["$j"]; //file name
   $path = 'uploads/'.$filen; //generate the destination path
   if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) 
{
   //upload the file
    $GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>";
    //Success message
   }
  }
 }
 else {
  $GLOBALS['msg'] = "No files found to upload"; //No file upload message 
}

Bu şekilde dosya / resim, gerektiği kadar ekleyebilir ve php komut dosyası aracılığıyla bunları işlemek.