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 ");
}
}