Functions to be implemented:

The form submits a file to the server, which accepts the file and stores it in the appropriate directory

Directory structure:

-- -- -- -- -- - form. The HTML / / front desk form page -- -- -- -- -- - upload_file. PHP / / background processing file -- -- -- -- -- - the upload file uploads / / directoryCopy the code

form.html

<html>  
<form method="post" action="test1.php" enctype="multipart/form-data">  
    <input type="file" name="file">  
    <input type="submit" value="Upload">  
</form>  
</html>  
Copy the code

Note: The form form must set the encType attribute to mulitiPart /form-data, otherwise PHP will not be able to receive the file from the foreground properly.

upload_file.php

if ($_FILES['file'] ['error'] > 0) {  
    echo 'Upload error encountered,';  
    switch ($_FILES['file'] ['error'])  
    {  
        case 1:  
            echo 'Uploaded file exceeds the limit of the upload_max_filesize option in php.ini';  
            break;  
        case 2:  
            echo 'Uploaded file size exceeds the value specified by the MAX_FILE_SIZE option in the HTML form';  
            break;  
        case 3:  
            echo 'Only part of the file was uploaded';  
            break;  
        case 4:  
            echo 'No files uploaded';  
            break;  
        case 5:  
            echo 'Upload file size is 0';  
            break; }}else{  
    // Displays information about uploaded files
    echo 'File name:'.$_FILES['file'] ['name'].'<br/>';  
    echo 'File type:'.$_FILES['file'] ['type'].'<br/>';  
    echo 'File size:'.$_FILES['file'] ['size'].'bytes < br / >';  
  
    // Set the path for saving the file
    // If the file name is Chinese, you need to use iconv() to convert the file name to GBK encoding, otherwise garbled characters will appear
    $dir = 'upload/'.iconv('UTF-8'.'gbk',basename($_FILES['file'] ['name']));  
  
    // Save the files uploaded by the user to the upload directory
    if (move_uploaded_file($_FILES['file'] ['tmp_name'],$dir)){  
        echo 'File uploaded successfully';  
    }  
    else{  
        echo 'File upload failed'; }}Copy the code

The file will already be stored in the upload folder at the root of the site.

If a file is too large to upload, PHP may report an error and fail to upload it. The solution is as follows:

Configure the php.ini file (for example, upload files smaller than 500M) to find the following options and change file_uploads = On. Open the file upload option upload_max_filesize = 500M; Upper limit of uploaded file size

If you want to upload large files, the above two items are not enough. You must increase the server cache limit and the maximum script execution time

post_max_size = 500M; Max_execution_time = 1800; Maximum execution time of each script, in seconds Max_input_time = 1800; Maximum amount of time each script may spend parsing request data memory_limit = 128M ; Maximum amount of memory a script may consume (128MB

Reference: http://www.jb51.net/article/18975.htm http://www.jb51.net/article/42660.htm