Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

PHP 多文件上传功能实例讲解

 Share


HACK1949

Recommended Posts

PHP文件上传功能是非常普片的需求,而在网站开发过程中,我们可以需要同时上传多个文件,这个时候就需要开发出php多文件上传的功能。在本教程中,我将向您展示如何使用PHP实现多个文件上传。

 

HTML代码

首先, 您需要创建一个属性为 enctype ='multiple / form-data'的HTML表单。实际上, enctype属性指定将表单数据提交到服务器时应如何编码。使用具有文件上载控件的表单时,需要将enctype指定为multiple / form-data。

如果使用单个input file文本控件,则需要将input file设置为可以选择多个文件。因此,需要将input file命名为数组,例如: name="upload[]" 。

<form method='post' action='' enctype='multipart/form-data'>
 
 <input type="file" name="upload[]" id="upload" multiple>
 <input type='submit' name='submit' value='Upload'>

</form>

 

PHP代码

在处理表单之前,请先检查表单是否已提交。

if(isset($_POST['submit']))

下一步检查上传文件的个数,上传个数必须大于或等于1个,否则不作任何处理。

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,然后将其上传到名为Uploaded的文件夹中。每个文件的日期和时间都将添加到文件名的开头,这可以防止任何现有文件被覆盖。

//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多文件上传完整示例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
</head>
<body>
<?php

$allowtype = array("gif","png","jpg"); //允许上传的文件格式
$size = 1000000; //设置允许大小1M以内的文件
$path = "./uploads"; //设置上传后保存文件的路径


//判断文件是否可以上传到服务器 $_FILES['myfile'][error]为0表示成功
//循环
for( $i = 0;$i < count($_FILES['myfile']['error']);$i++ ){

    $upfile[$i] = $_FILES['myfile']['name'][$i];

    if($_FILES['myfile']['error'][$i]>0){

        echo "上传错误";
        switch($_FILES['myfile']['error'][$i]){
        
            case 1: die('第'.($i+1).'个文件上传文件大小超出了PHP配置文件中的约定值:upload_max_filesize');
            case 2: die('上传第'.($i+1).'个文件大小超出了表单中的约定值:MAX_FILE_SIZE');
            case 3: die('第'.($i+1).'个文件只被部分上传');
            case 4: die('第'.($i+1).'个文件没有上传');
            default: die('未知错误');
        }
    }



    //判断上传的文件是否为允许的文件类型,通过文件的后缀名
    //array_pop 弹出并返回数组中的最后一个元素,并将array的长度减1
    $hz[$i] = array_pop(explode(".",$_FILES['myfile']['name'][$i]));
    if(!in_array($hz[$i],$allowtype)){

        die("第".($i+1)."个文件后缀是<b>{$hz}</b>,不是允许的文件类型!");
    }

    /*也可通过获取上传文件的MIME类型中的主类型和子类型,来限制文件上传的类型
    list($maintype,$subtype) = explode("/",$_FILES['myfile']['type']);
    if($maintype == "text"){

        die("不能上传文本文件");
    }
    */

    //判断上传的文件是否允许大小
    if($_FILES['myfile']['size'][$i]>$size){

        die("第".($i+1)."个文件超过了允许的<b>{$size}</b>");
    }


    //为了系统安全,同时也为了同名文件不被覆盖,上传后将文件名使用系统定义
    $filename[$i] = date("YmdHis").rand(100,999).".".$hz[$i];


    //判断是否为上传文件
    if(is_uploaded_file($_FILES['myfile']['tmp_name'][$i])){

        if(!move_uploaded_file($_FILES['myfile']['tmp_name'][$i],$path.'/'.$filename[$i])){
        
            die("不能将文件移动到指定目录");
        }
    }else{

        die("上传文件{$_FILES['myfile']['name'][$i]}不是一个合法文件");
    }

    //如果文件上传成功
    $filesize[$i] = $_FILES['myfile']['size'][$i]/1024;
    echo "文件{$upfile[$i]}上传成功,保存在目录{$path}中,文件大小为{$filesize[$i]}KB<br>";
}
?>

    <form action="upload.php" method="post" enctype="multipart/form-data">
        
        <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
        选择文件1:<input type="file" name="myfile[]"><br>
        选择文件2:<input type="file" name="myfile[]"><br>
        选择文件3:<input type="file" name="myfile[]"><br>
        <input type="submit" value="上传文件">

    </form>

</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助。更多教程请访问码农之家
Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...