在php中多file upload

我想上传多个文件,并将它们存储在一个文件夹中,并获取path并将其存储在数据库中…任何你想要做多个file upload的好例子…

注意:文件可以是任何types的…

我知道这是一个旧的post,但一些进一步的解释可能是有用的试图上传多个文件…这是你需要做的:

  • input名称必须被定义为一个数组,即name =“inputName []”
  • input元素必须有多个=“多个”多个
  • 在你的PHP文件中使用语法“$ _FILES ['inputName'] ['param'] [index]”
  • 确保查找空文件名和path ,数组可能包含空string 。 在count之前使用array_filter()。

这里是一个肮脏的例子(只显示相关的代码)

HTML:

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

PHP:

 //$files = array_filter($_FILES['upload']['name']); something like that to be used before processing files. // Count # of uploaded files in array $total = count($_FILES['upload']['name']); // Loop through each file for($i=0; $i<$total; $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 } } } 

希望这有助于!

可以select多个文件,然后使用
<input type='file' name='file[]' multiple>
上传的示例php脚本:

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

所选文件以数组forms接收

$_FILES['file']['name'][0]存储第一个文件的名字。
$_FILES['file']['name'][1]存储第二个文件的名字。
等等。

HTML

  1. 创buildid='dvFile' div;

  2. 创build一个button ;

  3. onclick该button调用函数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 } 

通过这种方式,您可以根据需要添加文件/图像,并通过php脚本处理它们。

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $max_no_img=4; // Maximum number of images value to be set here echo "<form method=post action='' enctype='multipart/form-data'>"; echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>"; for($i=1; $i<=$max_no_img; $i++){ echo "<tr><td>Images $i</td><td> <input type=file name='images[]' class='bginput'></td></tr>"; } echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>"; echo "</form> </table>"; while(list($key,$value) = each($_FILES['images']['name'])) { //echo $key; //echo "<br>"; //echo $value; //echo "<br>"; if(!empty($value)){ // this will check if any blank field is entered $filename =rand(1,100000).$value; // filename stores the value $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line $add = "upload/$filename"; // upload directory path is set //echo $_FILES['images']['type'][$key]; // uncomment this line if you want to display the file type //echo "<br>"; // Display a line break copy($_FILES['images']['tmp_name'][$key], $add); echo $add; // upload the file to the server chmod("$add",0777); // set permission to the file. } } ?> </body> </html> 

简单的是,只需先计算文件数组,然后在while循环中就可以轻松完成

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

现在你有正确的文件总数。

在while循环中这样做:

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

与上传一个文件没有什么不同 – $_FILES是一个包含任何和所有上传文件的数组。

PHP手册有一章: 上传多个文件

如果你想在用户端启用多个file upload(一次select多个文件,而不是填写上传字段),请看SWFUpload 。 它的工作方式与正常的file uploadforms不同,但要求Flash工作。 SWFUpload随Flash一起被废弃。 检查现在正确的方法,其他更新的答案。

这是我写的函数,它返回一个更容易理解的$_FILES数组。

 function getMultiple_FILES() { $_FILE = array(); foreach($_FILES as $name => $file) { foreach($file as $property => $keys) { foreach($keys as $key => $value) { $_FILE[$name][$key][$property] = $value; } } } return $_FILE; } 

我运行与错误元素的foreach循环,看起来像

  foreach($_FILES['userfile']['error'] as $k=>$v) { $uploadfile = 'uploads/'. basename($_FILES['userfile']['name'][$k]); if (move_uploaded_file($_FILES['userfile']['tmp_name'][$k], $uploadfile)) { echo "File : ", $_FILES['userfile']['name'][$k] ," is valid, and was successfully uploaded.\n"; } else { echo "Possible file : ", $_FILES['userfile']['name'][$k], " upload attack!\n"; } } 

刚刚遇到以下解决scheme:

http://www.mydailyhacks.org/2014/11/05/php-multifile-uploader-for-php-5-4-5-5/

它是一个现成的PHP多file upload脚本,可以添加多个input和一个AJAX进度条。 它应该在服务器上解包后直接工作…

我们可以使用下面的脚本轻松地上传多个文件。

下载完整的源代码和预览

 <?php if (isset($_POST['submit'])) { $j = 0; //Variable for indexing uploaded image $target_path = "uploads/"; //Declaring Path for uploaded images for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array $validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) $file_extension = end($ext); //store extensions in the variable $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image $j = $j + 1;//increment the number of uploaded images according to the files in array if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded. && in_array($file_extension, $validextensions)) { if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>'; } else {//if file was not moved. echo $j. ').<span id="error">please try again!.</span><br/><br/>'; } } else {//if file size and file type was incorrect. echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>'; } } } ?> 
 $property_images = $_FILES['property_images']['name']; if(!empty($property_images)) { for($up=0;$up<count($property_images);$up++) { move_uploaded_file($_FILES['property_images']['tmp_name'][$up],'..http://img.dovov.comproperty_images/'.$_FILES['property_images']['name'][$up]); } } 

尼斯链接:

PHP单个file upload不同的基本解释

PHPfile upload与validation

PHP多个file upload与validation点击这里下载源代码

PHP / jQuery多个file upload与ProgressBar和validation(点击这里下载源代码)

如何在PHP中上传文件并存储在MySql数据库中(点击此处下载源代码)

 extract($_POST); $error=array(); $extension=array("jpeg","jpg","png","gif"); foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) { $file_name=$_FILES["files"]["name"][$key]; $file_tmp=$_FILES["files"]["tmp_name"][$key]; $ext=pathinfo($file_name,PATHINFO_EXTENSION); if(in_array($ext,$extension)) { if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name)) { move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name); } else { $filename=basename($file_name,$ext); $newFileName=$filename.time().".".$ext; move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName); } } else { array_push($error,"$file_name, "); } } 

你必须检查你的HTML代码

 <form action="create_photo_gallery.php" method="post" enctype="multipart/form-data"> <table width="100%"> <tr> <td>Select Photo (one or multiple):</td> <td><input type="file" name="files[]" multiple/></td> </tr> <tr> <td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td> </tr> </table> </form> 

尼斯链接:

PHP单个file upload不同的基本解释

PHPfile upload与validation

PHP多个file upload与validation点击这里下载源代码

PHP / jQuery多个file upload与ProgressBar和validation(点击这里下载源代码)

如何在PHP中上传文件并存储在MySql数据库中(点击此处下载源代码)

这个简单的脚本为我工作。

 <?php foreach($_FILES as $file){ //echo $file['name']; echo $file['tmp_name'].'</br>'; move_uploaded_file($file['tmp_name'], "./uploads/".$file["name"]); } ?>