Php uploads images to the server

Source: Internet
Author: User
Tags php file upload
Php will be used to upload images to php files on the server $ _ FILES system functions

1. $ _ FILES system functions

Common $ _ FILES system functions in PHP programming are used as follows:
$ _ FILES ['myfile'] ['name'] displays the 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'] indicates the temporary file name, which is generally the default file name.

II. common functions

Common $ _ FILES system functions in PHP programming are used as follows:

$ _ FILES ['myfile'] ['name'] displays the 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'] indicates the temporary file name, which is generally the default file name.

$ _ FILES ['myfile'] ['error'] error code related to file upload. The meanings of different codes are as follows:

0. the file is uploaded successfully.

1; exceeds the file size set by the system in php. ini.

2. the file size exceeds the value specified by the MAX_FILE_SIZE option.

3. only part of the file is uploaded.

4. no files are uploaded.

5. the size of the uploaded file is 0.

III. php file Upload

To upload a file using form, you must first mark
The php code is as follows:

 $ Cover_pic, 'Store _ name' => $ store_name, 'Tel _ number' => $ tel_number, 'ad _ word' => $ ad_words, 'Store _ introduce '=> $ store_introduce, 'qq _ number' => $ qq_number); updatetable (tname ('open _ member_weixin_ap'), $ setarr, array ('apid' =>$ _ COOKIE ['apid'], 'id' =>> _ COOKIE ['id']); $ smarty-> assign ('loginuser', $ _ COOKIE ['loginuser']); $ smarty-> display ('setting. dwt '); exit ();} gourl ('binding. php '); function upload_store_pic (){ // File storage path $ save_path = './uploads/store_pic/'; if (! File_exists ($ save_path) {mkdir ($ save_path);} // defines the file extension $ ext_arr = array ('image' => array ('GIF ', 'jpg ', 'jpeg', 'PNG ', 'bmp'); // maximum file size $ max_size = 1000000; // if (! Empty ($ _ FILES ['IMG _ file'] ['error']) {switch ($ _ FILES ['IMG _ file'] ['error']) {case '1': $ error = 'exceeds php. ini size. '; Break; case '2': $ error =' exceeds the size allowed by the form. '; Break; case '3': $ error =' only part of the image is uploaded. '; Break; case '4': $ error =' select an image. '; Break; case '6': $ error =' The temporary directory cannot be found. '; Break; case '7': $ error =' an error occurred while writing a file to the hard disk. '; Break; default: $ error =' unknown error. ';} Showmessage ($ error); return '';} // if (empty ($ _ FILES) = false) when a file is uploaded) {// original file name $ file_name = $ _ FILES ['IMG _ file'] ['name']; // temporary file name on the server $ tmp_name =$ _ FILES ['IMG _ file'] ['tmp _ name']; // file size $ file_size = $ _ FILES ['IMG _ file'] ['size']; // check the file name if (! $ File_name) {showmessage ("Select a file. "); Return'';} // Check the directory if (@ is_dir ($ save_path) ===false) {showmessage ("the upload directory does not exist. "); Return'';} // Check the directory write permission if (@ is_writable ($ save_path) ===false) {showmessage ("the upload Directory has no write permission. "); Return'';} // check whether the image has been uploaded. if (@ is_uploaded_file ($ tmp_name) === false) {showmessage ("Upload failed. "); Return'';} // check the file size if ($ file_size> $ max_size) {showmessage ("The size of the uploaded file exceeds the limit. "); Return'';} // Check the directory name $ dir_name = empty ($ _ GET ['dir'])? 'Image': trim ($ _ GET ['dir']); if (empty ($ ext_arr [$ dir_name]) {showmessage ("The directory name is incorrect. "); Return'';} // get the file extension $ temp_arr = explode (". ", $ file_name); $ file_ext = array_pop ($ temp_arr); $ file_ext = trim ($ file_ext); $ file_ext = strtolower ($ file_ext ); // Check the extension if (in_array ($ file_ext, $ ext_arr [$ dir_name]) ===false) {showmessage ("the Upload file extension is not allowed. "); Return'';} // new file name $ new_file_name = date ("YmdHis "). '_'. rand (10000,999 99 ). '_ apid _'. $ _ COOKIE ['apid']. '. '. $ file_ext; // move the file $ file_path = $ save_path. $ new_file_name; if (move_uploaded_file ($ tmp_name, $ file_path) ===false) {showmessage ("An error occurred while uploading the file. "); Return'';} @ chmod ($ file_path, 0777); return $ file_path;} else {return '';}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.