PHP上传多个文件 [英] php upload multiple files
本文介绍了PHP上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ pre $ c $<?php
session_start();
$ allowedExts = array(jpg,jpeg,gif,png);
$ extension = end(explode(。,$ _FILES [file] [name])); ($($ _FILES [file] [type] ==image / gif)
$($ _FILES [file] [type ] ==image / jpeg)
||($ _FILES [file] [type] ==image / png)
||($ _FILES [file] [$] $$$$$$$$$$$$&$; ($ _FILES [file] [error]> 0)
{
echo返回代码:。 $ _FILES [file] [error]。 < br />;
}
else
{
echoUpload:。 $ _FILES [file] [name]。 < br />;
回显类型:。 $ _FILES [file] [type]。 < br />;
回声大小:。 ($ _FILES [file] [size] / 1024)。 Kb< br />;
回显临时文件:。 $ _FILES [file] [tmp_name]。 < br />;
if(file_exists($ _ SESSION ['user']。/。$ _FILES [file] [name]))
{
echo $ _FILES [文件名] 。 已经存在。 ;
$ b $ move_uploaded_file($ _ FILES [file] [tmp_name] [$ i],
$ _SESSION ['user']。 /。$ _FILES [file] [name]);
回声存储在:。 $ _SESSION ['user']。/。 $ _FILES [ 文件] [ 名称];
else
{
echo无效文件;
$ / code $ / pre
$ b $ p
< input type = 'file'name ='file []'multiple>
上传的示例php脚本:
< HTML>
< title>上传< / 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>
所选文件是以
存储第一个文件的名称。 存储第一个文件的名称。 $ _ FILES ['file'] ['name'] [0]
$ _ FILES ['file'] ['name'] [1]
存储第二个文件的名称。
等等。
Im not super familiar with PHP, I have the following code which allows me to upload a file to the server. how can I make this upload multiple files, in my html I have already added the multiple property. the php code is this:
<?php
session_start();
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists($_SESSION['user']."/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"][$i],
$_SESSION['user']."/" . $_FILES["file"]["name"]);
echo "Stored in: " . $_SESSION['user']."/" . $_FILES["file"]["name"];
}
}
else
{
echo "Invalid file";
}
?>
解决方案
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>
The selected files are received as an array with
$_FILES['file']['name'][0]
storing the name of first file.
$_FILES['file']['name'][1]
storing the name of second file.
and so on.
这篇关于PHP上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文