Download and upload files

Any type of variable data stored, are loaded into memory when the program is running, but can not persist, if you need to save up long-term data, there are two ways to save a file or saved to ordinary database. Our PHP can generate on our server directory, create, edit, delete, modify file attributes and other operations!

Upload file
upload process these details we do not need to pipe files, are automatically uploaded files by default is placed in a temporary directory, we need to do it is to take these temporary directory
to move files to the inside where we need to OK!

 

Fifth, the file upload
         1. HTML tags need to do upload settings
            . 1 ) form attribute sets 
                 . 1> Method to post
                 to multipart / form- 2> enctype setting data
                 sets the hidden type input 3> form a form, which name value set the MAX_FILE_SIZE, of vALUE value to upload file size need to limit (bytes)
         2. the server via PHP file upload process, the following pieces of information related to
            1) the uploaded configuration options related php (php . the INI)
                Configuration item might Value Function Description
                file_uploads ON determine whether PHP script on the server can accept HTTP file uploads
                Memory_limit 8M maximum amount of memory that can be allocated to set script, to prevent runaway script exclusive server memory
                Upload_max_filesize 2M maximum limit PHP file upload process, this value must be less than post_max_size
                post_max_size 8M restrictions by the POST method can accept the maximum amount of information
                F upload_tmp_dir : / WAMP / temporary path tmp upload files stored, can be an absolute path. The directory for the user that owns the server process must be written.
           2) $ _FILES multidimensional array : for storing information relating to the various upload
                 $ _FILES [ 'File'] [ 'name' ] the original name of the client machine's file, including the extension
                 $ _FILES [ 'File'] [ 'size ' ] has uploaded the file size in bytes
                 $ _FILES ' [ 'file'] [ 'tmp_name after] file upload, the temporary file name stored at the server
                 $ _FILES [' file '] [' error ': Indicates the size of the uploaded file exceeds the value of re-PHP configuration file upload_max_filesize option limits of
                                                                    2 : Uploads a file size exceeds the value of the HTML form MAX_FILE_SIZE option specified   
                                                                    3 : indicates that the document was only partially uploaded
                                                                    4 : that no uploads
                 _FILES $ [ 'file'] [ 'of the type'] to obtain client MIME type of the uploaded file, MIME type specifies the type of various file formats. Each type is a MIME / main partition types and subtypes of the composition
            . 3 ) of the PHP file upload handler: uploaded files for subsequent processing
                 . 1> entries for is_uploaded_file () determines whether the specified file is uploaded via HTTP POST
                 2> move_uploaded_file ( ) after the file is uploaded, first of all stored in the temporary directory on the server, you can use this function to upload the file to a new location

 

 

    <?php

// isset ($ _ POST [ 'submit']) Check that submit this variable is set, not set that we did not click the Start upload,
// the above steps did not write this, there is no myname variable will complain, because we have not committed, you can bind an event late in the submit button 
IF ( isset ( $ _POST [ 'the Submit' ])) {
     var_dump ( $ _FILES );
     // . a file name suffix, pathinfo returns the file attribute information and the like 
        $ file_arr = the pathinfo ( $ _FILES [ 'myname'] [ 'name' ]);
         var_dump ( $ file_arr );
         // write below this alone sentences can also file uploads, but will save, not a beginning $ _FILES information

        // move uploaded file, the first parameter is the time to upload the temporary path in $ FILES array,
         // The second parameter is the target path, you need to move a name and file extension 
        $ fname = DATE ( 'Ymd'). RAND (1000,9999);    // file name + current date on the random number 
        if ( move_uploaded_file ( $ _FILES [ 'myname'] [ 'tmp_name'], "Upload / { $ fname .} { $ file_arr [ 'Extension']}" )) {
             echo 'uploaded successfully' ;
        } The else {
             echo 'fail' ;
        };
    
}


   ?>

 

Guess you like

Origin www.cnblogs.com/xiaowie/p/11876211.html