A simple php file upload example program

Source: Internet
Author: User
This article introduces a simple php file Upload sample code for beginners. if you need to learn more, refer to. the instance code is as follows :? Php definition prompt function functionaler... this article will introduce you to a simple php file Upload sample code for beginners. if you need to learn, visit the reference.

The instance code is as follows:

 Alert ("'. $ msg. '"); window. history. back (-1); script ';} // defines the allowed file types $ allowType = array ('image/jpeg', 'image/GIF ', 'image/jpg '); // defines the path, which can be an absolute path or a relative path $ filePath = '. /uploadFileDir/'; // The file value written in the received form information is the name value in the form of the static page $ file =$ _ FILES ['file']; // Step 1: Determine whether the uploaded file has an error if ($ file ['error']! = 0) {exit (alert ('file Upload error');} // Step 2: determine the file size. here, 102400 is the byte, converting to kb is 100 kbif ($ file ['size']> 102400) {exit (alert ('file too large ');} // Step 3, determine the file type if (! In_array (mime_content_type ($ file ['tmp _ name']), $ allowType) {exit (alert ('file type error');} // Step 4, determine whether the path exists. if not, create if (! File_exists ($ filePath )&&! Mkdir ($ filePath, 0777, true) {exit (alert ('directory creation error');} // Step 5, defines the name and path of the uploaded file $ filename = time (). '_'. $ file ['name']; // Step 6: Copy the object if (! Copy ($ file ['tmp _ name'], $ filePath. $ filename) {exit (alert ('file Upload error, please try again later ');} // Step 7, delete the temporary file unlink ($ file ['tmp _ name']); // the system prompts that the upload is successful echo alert ('Congratulations, upload file ['. $ filename. '] successful! ');?>


Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.