php中的多文件上传
我想上传多个文件并将其存储在文件夹中,然后获取路径并将其存储在数据库中。。。任何一个好的例子,你想做多个文件上传php中的多文件上传,php,file-upload,Php,File Upload,我想上传多个文件并将其存储在文件夹中,然后获取路径并将其存储在数据库中。。。任何一个好的例子,你想做多个文件上传 注意:文件可以是任何类型的…它与上载一个文件没有什么不同-$\u文件是一个包含所有上载文件的数组 PHP手册中有一章: 如果您想在用户端轻松选择(一次选择多个文件而不是填写上载字段)启用多个文件上载,请查看。不过,它的工作原理与普通的文件上传表单不同,需要Flash才能工作。SWFUpload与Flash一起被淘汰。检查其他较新的答案,了解现在正确的方法。 <!DOCTYPE
注意:文件可以是任何类型的…它与上载一个文件没有什么不同-
$\u文件是一个包含所有上载文件的数组
PHP手册中有一章:
如果您想在用户端轻松选择(一次选择多个文件而不是填写上载字段)启用多个文件上载,请查看。不过,它的工作原理与普通的文件上传表单不同,需要Flash才能工作。SWFUpload与Flash一起被淘汰。检查其他较新的答案,了解现在正确的方法。
<!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>
无标题文件
可以选择多个文件,然后使用
执行上载操作的示例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>
上传
所选文件以数组形式接收,其中包含
$\u FILES['file']['name'][0]
存储第一个文件的名称。
$\u FILES['file']['name'][1]
存储第二个文件的名称。
等等。我知道这是一篇老文章,但对于试图上传多个文件的人来说,一些进一步的解释可能会有用。。。以下是您需要做的:
- 输入名称必须定义为数组,即。
name=“inputName[]”
- 输入元素必须具有
multiple=“multiple”
或只是multiple
- 在PHP文件中使用语法
“$\u FILES['inputName']['param'][index]”
- 确保查找空文件名和路径,数组可能包含空字符串。计数前使用
array\u filter()
下面是一个糟糕的示例(仅显示相关代码)
HTML:
PHP:
/$files=array_filter($_files['upload']['name'])//在处理文件之前要使用类似的东西。
//阵列中上载文件的计数#
$total=计数($_文件['upload']['name']);
//循环浏览每个文件
对于($i=0;$i<$total;$i++){
//获取临时文件路径
$tmpFilePath=$\u文件['upload']['tmp\u name'][$i];
//确保我们有一个文件路径
如果($tmpFilePath!=“”){
//设置我们的新文件路径
$newFilePath=“./uploadFiles/”$_FILES['upload']['name'][$i];
//将文件上载到临时目录
if(移动上传的文件($tmpFilePath,$newFilePath)){
//在这里处理其他代码
}
}
}
希望这有帮助 HTML
使用id='dvFile'
创建div
创建一个按钮
onclick该按钮调用函数的add\u more()
JavaScript
function add_more() {
var txt = "<br><input type=\"file\" name=\"item_file[]\">";
document.getElementById("dvFile").innerHTML += txt;
}
函数add_more(){
var txt=“
”;
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
}
if(计数($\u文件[“项目\u文件”][“名称])>0)
{
//检查是否上传了任何文件
$GLOBALS['msg']=“”;//启动全局消息
对于($j=0;$j<计数($_文件[“项_文件”][“名称]);$j++)
{//循环上载的文件数组
$filen=$\u FILES[“item\u file”]['name'][“$j”];//文件名
$path='uploads/'。$filen;//生成目标路径
如果(移动上传的文件($文件[“项目文件”][“tmp文件名”][“$j”],$path))
{
//上传文件
$GLOBALS['msg'].=“文件#”。($j+1)。”($filen)已成功上载
”;
//成功信息
}
}
}
否则{
$GLOBALS['msg']=“找不到要上载的文件”;//没有文件上载消息
}
通过这种方式,您可以根据需要添加任意数量的文件/图像,并通过php脚本处理它们。简单的是,只需先计算文件数组,然后在while循环中,您就可以轻松地执行类似的操作
$count = count($_FILES{'item_file']['name']);
现在,您获得了正确的文件总数
在while循环中,执行以下操作:
$i = 0;
while($i<$count)
{
Upload one by one like we do normally
$i++;
}
$i=0;
当($i我使用error元素运行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";
}
}
下面是我编写的一个函数,它返回一个更容易理解的$\u文件
数组
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;
}
刚刚遇到以下解决方案:
这是一个现成的PHP多文件上传脚本,带有一个表单,您可以在其中添加多个输入和一个AJAX进度条。它应该在服务器上解包后直接工作…我们可以使用下面的脚本使用PHP轻松上传多个文件
$property\u images=$\u文件['property\u images']['name'];
如果(!empty($property_images))
{
对于($up=0;$upNice链接,请访问:
你必须检查你的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>
选择照片(一张或多张):
注意:支持的图像格式:.jpeg、.jpg、.png、.gif
链接如下:
这个简单的脚本适合我
<?php
foreach($_FILES as $file){
//echo $file['name'];
echo $file['tmp_name'].'</br>';
move_uploaded_file($file['tmp_name'], "./uploads/".$file["name"]);
}
?>
这对我来说很有效。我必须上传文件,存储文件名,我还需要从输入字段存储额外的inof,并且每个文件名有一条记录。
我使用serialize()然后将其添加到主sql查询中
class addReminder extends dbconn {
public function addNewReminder(){
$this->exdate = $_POST['exdate'];
$this->name = $_POST['name'];
$this->category = $_POST['category'];
$this->location = $_POST['location'];
$this->notes = $_POST['notes'];
try {
if(isset($_POST['submit'])){
$total = count($_FILES['fileUpload']['tmp_name']);
for($i=0;$i<$total;$i++){
$fileName = $_FILES['fileUpload']['name'][$i];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$newFileName = md5(uniqid());
$fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
$justFileName = $newFileName.'.'.$ext;
if($ext === 'pdf' || 'jpeg' || 'JPG'){
move_uploaded_file($_FILES['fileUpload']['tmp_name'][$i], $fileDest);
$this->fileName = array($justFileName);
$this->encodedFileNames = serialize($this->fileName);
var_dump($this->encodedFileNames);
}else{
echo $fileName . ' Could not be uploaded. Pdfs and jpegs only please';
}
}
$sql = "INSERT INTO reminders (exdate, name, category, location, fileUpload, notes) VALUES (:exdate,:name,:category,:location,:fileName,:notes)";
$stmt = $this->connect()->prepare($sql);
$stmt->bindParam(':exdate', $this->exdate);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':category', $this->category);
$stmt->bindParam(':location', $this->location);
$stmt->bindParam(':fileName', $this->encodedFileNames);
$stmt->bindParam(':notes', $this->notes);
$stmt->execute();
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
class AddReconn扩展了dbconn{
公共函数addnewrementer(){
$this->exdate=$\u POST['exdate'];
$this->name=$\u POST['name'];
$this->category=$_POST['category'];
$this->location=$\u POST['location'];
$this->notes=$_POST['notes'];
试一试{
如果(isset($_POST['submit'])){
$total=计数($_文件['fileUpload']['tmp_名称']);
对于($i=0;$ifileName=array($justFileName);
$this->encodedFileNames=序列化($this->fileName);
变量转储($this->encodedFileNames);
}否则{
echo$fileName。'n
$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],'../images/property_images/'.$_FILES['property_images']['name'][$up]);
}
}
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, ");
}
}
<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
foreach($_FILES as $file){
//echo $file['name'];
echo $file['tmp_name'].'</br>';
move_uploaded_file($file['tmp_name'], "./uploads/".$file["name"]);
}
?>
class addReminder extends dbconn {
public function addNewReminder(){
$this->exdate = $_POST['exdate'];
$this->name = $_POST['name'];
$this->category = $_POST['category'];
$this->location = $_POST['location'];
$this->notes = $_POST['notes'];
try {
if(isset($_POST['submit'])){
$total = count($_FILES['fileUpload']['tmp_name']);
for($i=0;$i<$total;$i++){
$fileName = $_FILES['fileUpload']['name'][$i];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$newFileName = md5(uniqid());
$fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
$justFileName = $newFileName.'.'.$ext;
if($ext === 'pdf' || 'jpeg' || 'JPG'){
move_uploaded_file($_FILES['fileUpload']['tmp_name'][$i], $fileDest);
$this->fileName = array($justFileName);
$this->encodedFileNames = serialize($this->fileName);
var_dump($this->encodedFileNames);
}else{
echo $fileName . ' Could not be uploaded. Pdfs and jpegs only please';
}
}
$sql = "INSERT INTO reminders (exdate, name, category, location, fileUpload, notes) VALUES (:exdate,:name,:category,:location,:fileName,:notes)";
$stmt = $this->connect()->prepare($sql);
$stmt->bindParam(':exdate', $this->exdate);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':category', $this->category);
$stmt->bindParam(':location', $this->location);
$stmt->bindParam(':fileName', $this->encodedFileNames);
$stmt->bindParam(':notes', $this->notes);
$stmt->execute();
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
}