Php beginners-php file Upload tutorial

Source: Internet
Author: User
Tags file upload html form php file upload php tutorial

Html form Upload code


<Form enctype = "multipart/form-data" action = "upload. php Tutorial" method = "post">
<Input type = "hidden" name = "max_file_size" value = "100000">
<Input name = "userfile" type = "file">
<Input type = "submit" value = "Upload file">
</Form>

The following is the php file upload code.

 

Function uploadfile ($ type, $ name, $ ext, $ size, $ error, $ tmp_name, $ targetname, $ upload_dir)
{
$ Max_size = 2000000;
$ File_mimes = array ('image/pjpeg ', 'image/jpeg', 'image/jpg ', 'image/GIF', 'image/png ');
$ File_exts = array('.jpg ', '.gif', '.png', '.jpg ', '.gif', '.png ');
$ File_path = $ upload_dir. $ targetname;
If (! Is_dir ($ upload_dir ))
{
If (! Mkdir ($ upload_dir ))
Die ("the file upload directory does not exist and the file upload directory cannot be created ");
If (! Chmod ($ upload_dir, 0755 ))
Die ("the file upload directory permission cannot be set to readable and writable ");
}
If ($ size> $ max_size)
Die ("The size of the uploaded file exceeds the specified size ");
If ($ size = 0)
Die ("Select the uploaded file ");
If (! In_array ($ type, $ file_mimes) |! In_array ($ ext, $ file_exts ))
Die ("please upload the file type that meets the requirements ");
If (! Move_uploaded_file ($ tmp_name, $ file_path ))
Die ("failed to copy the file, please upload it again ");
Switch ($ error)
{
Case 0:
Return;
Case 1:
Die ("the uploaded file exceeds the limit of the upload_max_filesize option in php. ini ");
Case 2:
Die ("The size of the uploaded file exceeds the value specified by the max_file_size option in the html form ");
Case 3:
Die ("only part of the file is uploaded ");
Case 4:
Die ("no files are uploaded ");
}
}

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.