uda*_*aya 110 php file-upload
我想上传多个文件并将它们存储在一个文件夹中并获取路径并将其存储在数据库中......你寻找多个文件上传的好例子......
注意:文件可以是任何类型的......
And*_*ham 235
我知道这是一篇旧帖子,但对于试图上传多个文件的人来说,一些进一步的解释可能会有用......这就是你需要做的:
name="inputName[]"
multiple="multiple"
或只是multiple
"$_FILES['inputName']['param'][index]"
array_filter()
在计数之前使用.这是一个肮脏的例子(仅显示相关代码)
HTML:
<input name="upload[]" type="file" multiple="multiple" />
Run Code Online (Sandbox Code Playgroud)
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 file path
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助!
rjv*_*rjv 37
可以选择多个文件,然后使用执行上传
<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>
Run Code Online (Sandbox Code Playgroud)
所选文件作为数组接收
$_FILES['file']['name'][0]
存储第一个文件的名称.
$_FILES['file']['name'][1]
存储第二个文件的名称.
等等.
小智 6
HTML
用id='dvFile'
; 创建div ;
创造一个button
;
onclick
该按钮调用功能 add_more()
JavaScript的
function add_more() {
var txt = "<br><input type=\"file\" name=\"item_file[]\">";
document.getElementById("dvFile").innerHTML += txt;
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以根据需要添加文件/图像,并通过php脚本处理它们.
这是我编写的一个函数,它返回一个更容易理解的$_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;
}
Run Code Online (Sandbox Code Playgroud)
这个简单的脚本对我有用。
<?php
foreach($_FILES as $file){
//echo $file['name'];
echo $file['tmp_name'].'</br>';
move_uploaded_file($file['tmp_name'], "./uploads/".$file["name"]);
}
?>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
317848 次 |
最近记录: |