本站所搜集的资源均来源于网络,仅供学习研究代码使用,请勿商用
本站所有资源均免费下载无需付费。
登录账号后访问“个人中心”点击“我的余额”在右上角签到后即可免费下载
其资源在源站什么样下载就是什么样,并非本站制作发布,代码是否完整、是否有教程及使用说明需自行判断,本站不保证其资源可用。
本站所有资源均免费下载无需付费。
登录账号后访问“个人中心”点击“我的余额”在右上角签到后即可免费下载
其资源在源站什么样下载就是什么样,并非本站制作发布,代码是否完整、是否有教程及使用说明需自行判断,本站不保证其资源可用。
由于新的 multiple 属性,HTML 5 使得使用单个input上传多个文件成为可能。与 Javascript 或 Flash 相比,使用 HTML 5 的好处是本机支持,它可以跨 Internet Explorer 10、Firefox、Opera、Chrome 和 Safari 浏览器工作。
在这种情况下,创建表单标签指定操作我使用的是同一个文件,所以我将它留空,需要 enctype=”multipart/form-data” 来告诉浏览器期望接收二进制数据,最后告诉表单使用 post。
<form action="" enctype="multipart/form-data" method="post">
文件的输入将具有 multiple=”multiple” 的属性,这可以上传多个文件,给输入一个带有 [] 的名称,如上传 [] 这将在处理表单时形成一个数组。
<div>
    <label for='upload'>Add Attachments:</label>
    <input id='upload' name="upload[]" type="file" multiple="multiple" />
</div>
<p><input type="submit" name="submit" value="Submit"></p>
要处理表单,请检查表单是否已提交。
if(isset($_POST['submit'])){
接下来检查以确保它们是 $_FILES[‘upload’] 数组中的项目
if(count($_FILES['upload']['name']) > 0){
遍历文件,设置一个 tmp 文件路径来保存临时文件
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 != ""){
将文件名保存到 $shortname 变量以用于查看文件名,$filePath 将保存文件的完整 url,以上传到名为 upload 的文件夹中。每个文件都会在文件名的开头添加日期和时间,这会阻止任何现有文件被覆盖。
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = "/uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
    //insert into db
    //use $shortname for the filename
    //use $filePath for the relative url to the file
}
完整代码
<?php
if(isset($_POST['submit'])){
    if(count($_FILES['upload']['name']) > 0){
        //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 != ""){
                //save the filename
                $shortname = $_FILES['upload']['name'][$i];
                //save the url and the file
                $filePath = "uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
                //Upload the file into the temp dir
                if(move_uploaded_file($tmpFilePath, $filePath)) {
                    $files[] = $shortname;
                    //insert into db
                    //use $shortname for the filename
                    //use $filePath for the relative url to the file
                }
              }
        }
    }
    //show success message
    echo "<h1>Uploaded:</h1>";
    if(is_array($files)){
        echo "<ul>";
        foreach($files as $file){
            echo "<li>$file</li>";
        }
        echo "</ul>";
    }
}
?>
<form action="" enctype="multipart/form-data" method="post">
    <div>
        <label for='upload'>Add Attachments:</label>
        <input id='upload' name="upload[]" type="file" multiple="multiple" />
    </div>
    <p><input type="submit" name="submit" value="Submit"></p>
</form>
	声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
		
