Experience: sample PHP file upload code. Today I read a book about PHP and learned how to upload files in PHP. The two main functions are move_uploade_file (temporary file, target location and file name) I read a book about PHP today with is_uploaded_fi and learned how to upload files in PHP. The two main functions are move_uploade_file (temporary file, target location and file name) and is_uploaded_file (). The former is used to move the files stored in the cache area of the server to the target file, and the latter is used to determine whether the files are uploaded successfully. In addition to the above two functions, we also need to explain that the value of enctype in the form label should be as follows:
- <formenctypeformenctype="multipart/form-data"method="post"name="upform">
Only multipart/form-data can ensure that files are uploaded in the correct encoding mode. "File" in the type attribute of the input tag"
- <inputnameinputname="upfile"type="file">
Another system function is $ _ FILES, $ _ FILES ['myfile'] ['name'] original name of the client file, $ _ FILES ['myfile'] ['type'] MIME type of the file, for example, "image/gif", $ _ FILES ['myfile'] ['size'] size of the uploaded file, in bytes, $ _ FILES ['myfile'] ['tmp _ name'] stores temporary file names, this is generally the system default, $ _ FILES ['myfile'] ['error'] error code related to the file upload. This function divides the information of the uploaded file into arrays and stores them in different array elements. for example, the file name value is stored in $ _ FILES ['myfile'] ['name. The following is a simple PHP file upload code written by myself:
PHP file upload code class saveupload. php
- Php
- If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
- $Upfile= $ _ FILES ["upfile"]; // If you have selected the file to be uploaded, save its index in $ upfile
-
-
- // Upload the object names and types respectively.
- $Name= $ Upfile ["name"];
- $Type= $ Upfile ["type"];
- $Size= $ Upfile ["size"];
- $Tmp_name= $ Upfile ["tmp_name"];
- $Error= $ Upfile ["error"];
-
-
- // Set the Upload file type
- Switch ($ type ){
- Case 'image/pjpeg ':
- $OK=1;
- Break;
-
- Case 'image/jpeg ':
- $OK=1;
- Break;
-
- Case 'image/png ':
- $OK=1;
- Break;
-
- Case 'image/GIF ':
- $OK=1;
- Break;
- }
-
-
- // If the file type is valid and the value of $ error is 0, the upload is successful.
- If ($ OK & $Error= '0 '){
- Move_uploaded_file ($ tmp_name, 'up/'. $ name); // Move the files stored in the cache to the specified directory
- Echo "uploaded successfully ";
- }
- }
-
- ?>
PHP upload file code upload page upload. php
- >
- <HtmlxmlnsHtmlxmlns=Http://www.w3.org/1999/xhtml">
- <Head>
- <Metahttp-equivMetahttp-equiv= "Content-Type"Content= "Text/html;Charset=Utf-8"/>
- <Title>Upload Title>
- <StyletypeStyletype="Text/css">
-