Play site source code PHP background development, file upload and download implementation

In the source code development of the accompanying website, in addition to the client, we have to develop a server. The server generally adopts the PHP development framework and language. In the server, we will upload some files that need to be used. How do we implement them? Today, let's learn about the PHP background development of the source code of the accompanying website, and the implementation of file upload and download.

1, html and PHP considerations for file upload

1. To set the enctype attribute of the form, set the method to post. When enctype is set to multipart / form data, the upload information of the source image of the accompany website will be listed F I L E S exceed whole game number group , and wrong _ FILES super global array, not F # ILES super global array, not_ POST, so as to achieve the real purpose of uploading
2. Set hidden input: indicates the maximum upload space
3.PHP configuration file settings:

2, Single file upload

1. Copy or move the uploaded file: move_uploaded_file()
bool move_uploaded_file ( string $filename , string $destination )
2. Wrong judgment

3. Judge the type through the extension array
4. Judge the size
5. Set the random file name (Security) of the file uploaded from the source code of the accompanying website

<!DOCTYPE >
<html>
    <head>
        <meta charset="utf-8" />
        <style>
            input{
                display: block;
            }
        </style>
    </head>
    <body>
        <form action="receive.php" method="post" enctype="multipart/form-data">
            <label for="uploadname">name: </label><input type="text" value="" name="uploadname" id="uploadname" />
            <input type="hidden" name="MAX_VALUE_SIZE" value="1000000" />
            <input type="file" name="pic" value="" />
            <input type="submit" value="upload" />
        </form>
    </body>
</html>
<?php
    /*echo "<pre>";
    print_r($_POST);
    print_r($_FILES);    
    echo "</pre>";*/
    if($_FILES['pic']['error']>0){
        switch($_FILES['pic']['error']){
            case 1:
                echo"Picture size exceeds upload_max_filesize<br>";
                break;
            case 2:
                echo"The picture size exceeds the size of the hidden form MAX_FILE_SIZE<br>";
                break;
            case 3:
                echo"Files are only partially uploaded<br>";
                break;
            case 4:
                echo"No files uploaded<br>";        
                break;    
            case 6:
                echo"The temporary folder could not be found<br>";    
                break;        
            case 7:
                echo"fail to write to file<br>";    
                break;    
            default:
                echo "An unknown error occurred<br>";                
        }
    }
    $extArr=explode('.',$_FILES['pic']['name']);
    $ext=array_pop($extArr);
    $arr=array('jpg','jpeg','png');
    if(!in_array($ext,$arr)){
        echo "Upload type does not match<br>";
        exit;
    }
    $maxSize=100000;
    if($_FILES['pic']['size']>$maxSize){
        echo "The file exceeds the specified size<br>";
        exit;
    }
    Date_default_timezone_set('PRC');
    $proPath=$_FILES['pic']['tmp_name'];
    $newPath='./'.date('ymdhis').rand(100,999).'.'.$ext;
    if(move_uploaded_file($proPath,$newPath)){
        echo "Upload succeeded<br>";
    }else{
        echo "Upload failed<br>";
    }
?>

2, Multiple file uploads

1. Change the name attribute of the upload button into an index array to facilitate traversal
2. Set a cycle on the outer layer of the above single file instance

<?php
    echo "<pre>";
    print_r($_POST);
    print_r($_FILES);    
    echo "</pre>";
    $num=count($_FILES['pic']['name']);
    for($i=0;$i<$num;$i++){
        //Wrong judgment
        if($_FILES['pic']['error'][$i]>0){
            switch($_FILES['pic']['error']){
                case 1:
                    echo"Picture size exceeds upload_max_filesize<br>";
                    break;
                case 2:
                    echo"The picture size exceeds the size of the hidden form MAX_FILE_SIZE<br>";
                    break;
                case 3:
                    echo"Files are only partially uploaded<br>";
                    break;
                case 4:
                    echo"No files uploaded<br>";        
                    break;    
                case 6:
                    echo"The temporary folder could not be found<br>";    
                    break;        
                case 7:
                    echo"fail to write to file<br>";    
                    break;    
                default:
                    echo "An unknown error occurred<br>";                
            }
            continue;
        }
        //Judgment type
        $extArr=explode('.',$_FILES['pic']['name'][$i]);
        $ext=array_pop($extArr);
        $arr=array('jpg','jpeg','png');
        if(!in_array($ext,$arr)){
            echo "Upload type does not match<br>";
            continue;
        }
        //Judge size
        $maxSize=1000000;
        if($_FILES['pic']['size'][$i]>$maxSize){
            echo "The file exceeds the specified size<br>";
            continue;
        }
        //Set the random file name to upload
        Date_default_timezone_set('PRC');
        $proPath=$_FILES['pic']['tmp_name'][$i];
        $newPath='./'.date('ymdhis').rand(100,999).'.'.$ext;
        if(move_uploaded_file($proPath,$newPath)){
            echo "upload{$_FILES['pic']['name'][$i]}success<br>";
        }else{
            echo "Upload failed<br>";
        }
    }
?>

3, File upload class

1. Refer to the PHP file upload class for details
4, File download

1.a set the tag href attribute to the download address
2. The HTTP header information is changed to an attachment

<?php
    $filename='view.html';
    header("Content-Type:text/html");
    header("Content-Disposition:attachment;filename:{$filename}");
    header("Content-Length:".filesize($filename));
    readfile($filename);
?>

Unexpectedly, there are such complicated processes and requirements for uploading and downloading files on the server of the source code of the accompanying website. The above is all the contents of "PHP background development of the source code of the accompanying website, and the realization of file uploading and downloading". I hope it will be helpful to you.

Added by herghost on Sat, 01 Jan 2022 06:16:29 +0200