php single file upload and multi file upload

PHP file upload configuration php.ini Files and predefined variables$_ FILES

1: Configuration php.ini file

To achieve the upload function smoothly, the first thing to do is to php.ini Open file upload and make reasonable settings for some of the parameters. Find the File Upioads item, and you can see that there are three attributes as follows, meaning as follows:

  • file_uploads: if the value is on, the server supports file upload; if off, it does not.

  • upload_tem_dir: upload file temporary directory. Before the file is uploaded successfully, the file is first stored in the temporary directory on the server side. If you want to specify a location, you can set it here. Otherwise, you can use the system default directory.

  • upload_ max_ File size: the maximum number of files allowed to be uploaded by the server, in MB. The default value of the system is 2MB, which can be set by the user.

In addition to the file uplads item, there are several properties that also affect the function of uploading files.

  • max_execution_time:PHP the longest time an instruction can be executed in seconds;

  • memory_limit: the amount of memory allocated by an instruction in PHP, in MB;

explain:

1. If you use the integrated installation package to configure the PHP development environment, these configuration information described above has been configured by default!

2. If you want to upload a large file, you need to php.ini Some parameters of are modified, including upload_ max_ The maximum value of the file size server allows to upload, max_execution_time the maximum time and memory that an instruction can execute_ Limit the memory space allocated by an instruction!

 

2: Predefined variables$_ FLIES

$5 The FLIES variable stores information about the uploaded file, which plays a great role in the upload function. The variable is a two-dimensional array. Predefined variables$_ The FILES element is described in the following table:

 

 

In the following example, create an upload file domain, which can be accessed through the$_ The FILES variable outputs the uploaded file data. The specific example code is as follows

<html>

<body>

<table width="500" border="1" cellpadding="0" cellspacing="0">

    <!--Of uploaded files form Form, must have enctype attribute-->

    <form method="post" about="" enctype="multipart/form-data">

    <tr>

        <td width="150" height="30" align="right" valign="middle">Please select upload file:</td>

        <!--Upload file fields, type Type is file-->

        <td width="250"><input type="file" name="upfile"></td>

        <!-- Submit button-->

        <td width="100"><input type="submit" name="submit" value="upload"></td>

</tr>

</form>

</table>

</body>

</html>

<?php

header("Content-Type:text/html; charset=utf-8");

if(!empty($_FILES)){                                //Judgment variable $_FILES Empty or not

    foreach ($_FILES['upfile'] as $name => $value){ //use foreach Loop out the name and value of the uploaded file information

        echo $name,"=".$value."<br>";

    }

}

?>

The output result is:

 

 

PHP file upload move_ uploaded_ A detailed explanation of the use of file() function

The following example creates an upload form that allows you to upload a file with a file size of less than 1MB. The specific example code is as follows:

<html>

<body>

<table width="500" border="1" cellpadding="0" cellspacing="0">

    <!--Of uploaded files form Form, must have enctype attribute-->

    <form method="post" about="" enctype="multipart/form-data">

    <tr>

        <td width="150" height="30" align="right" valign="middle">Please select upload file:</td>

        <!--Upload file fields, type Type is file-->

        <td width="250"><input type="file" name="upfile"></td>

        <!-- Submit button-->

        <td width="100"><input type="submit" name="submit" value="upload"></td>

</tr>

</form>

</table>

</body>

</html>

<?php

header("Content-Type:text/html; charset=utf-8");

if(!empty($_FILES)){                                //Judgment variable $_FILES Empty or not

    $fileinfo = $_FILES['upfile'];
    if($fileinfo['size']>0 && $fileinfo['size']<1000000){
        move_uploaded_file($fileinfo['tmp_name'], $fileinfo['name']);
        echo 'Upload succeeded';
    }else{
        echo 'File too large or unknown';
    }

}

?>

explain:

Using move_ uploaded_ The file() function uploads a file. When creating a form form, you must set the enctype = "multipart / form data" of the form form.

 

PHP file upload - Multi file upload example code details

The following example has two file upload fields, whose name is u_file [], the uploaded file information will be saved to$_ FILES['u_ In file '], most of the generated data are arrays. Read the array information and upload the file. The specific example code is as follows:

<html>

<body>

<table border="1" cellpadding="0" cellspacing="0">

    <!--Of uploaded files form Form, must have enctype attribute-->

    <form method="post" about="" enctype="multipart/form-data">

    <tr>

        <td>Please select upload file:</td>

        <!--Upload file fields, type Type is file-->

        <td><input type="file" name="u_file[]"></td>
        <td><input type="file" name="u_file[]"></td>

        <!-- Submit button-->

        <td><input type="submit" name="submit" value="upload"></td>

</tr>

</form>

</table>

</body>

</html>

<?php

header("Content-Type:text/html; charset=utf-8");

if(!empty($_FILES['u_file']['name'])){  
    $file_name = $_FILES['u_file']['name'];
    $file_tmp_name = $_FILES['u_file']['tmp_name'];
    for($i=0;$i<count($file_name);$i++){
        if($file_name[$i] != ''){
            move_uploaded_file($file_tmp_name[$i], $i.$file_name[$i]);
            echo 'file'.$file_name[$i].'Upload succeeded<br>';
        }
    }

}

?>

The output results are as follows:

 

Welcome to QQ communication and discussion: 965794175

Tags: PHP Attribute less

Posted on Tue, 26 May 2020 23:57:49 -0700 by psunshine