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("文件上传目录不存在并且无法创建文件上传目录"); if(!chmod($upload_dir,0755)) die("文件上传目录的权限无法设定为可读可写"); } if($size>$MAX_SIZE) die("上传的文件大小超过了规定大小"); if($size == 0) die("请选择上传的文件"); if(!in_array($type,$FILE_MIMES) || !in_array($ext,$FILE_EXTS)) die("请上传符合要求的文件类型"); if(!move_uploaded_file($tmp_name, $file_path)) die("复制文件失败,请重新上传"); switch($error) { case 0: return ; case 1: die("上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值"); case 2: die("上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值"); case 3: die("文件只有部分被上传"); case 4: die("没有文件被上传"); } }
Parameter Description $type, $name, $size, $error, $tmp_name correspond to the relevant variables in the global variable $_FILES, that is: $_FILES['userfile']['type']: The MIME type of the file requires the browser to provide support for this information, such as the image type "image/gif". $_FILES['userfile']['name']: The original name of the client file. $_FILES['userfile']['size']: The size of the uploaded file, in bytes. $_FILES['userfile']['tmp_name']: The temporary file name stored on the server after the file is uploaded. $_FILES['userfile']['error']: The error code related to the file upload, that is, Value: 0: No error occurred, the file upload was successful. Value: 1: The uploaded file exceeds the limit of the upload_max_filesize option in php.ini. Value: 2: The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. Value: 3: Only part of the file was uploaded. Value: 4: No files were uploaded. $ext: Upload file extension $targetname: The final file name after uploading $upload_dir: Which directory to upload to, using a relative path Comments: No. 3 Line ~ Line 6: Set the size of the image file to upload, as well as the MIME type and extension of the file. Since this code is an image file upload program, all image types are listed in the two arrays, such as PNG, GIF, and JEPG. wait. Line 17~Line 24: If the file is empty, size is equal to 0; if the extension or type of the image file does not match, it will jump out. Line 26: The function of the move_uploaded_file function is to move the file in the server temporary directory set by upload_tmp_dir to the file specified by $file_path. Note that if the target file already exists, the target file will be overwritten How to upload multiple document? For example, to upload 3 files at the same time just copy the code
as follows:
changed to
Copy the code The code is as follows:
Correspondingly, when calling this function, $_FILES['userfile']['name'][0] represents the related files of the first file information, and so on, and so on.
Summary This function is the simplest core code in PHP file upload. Image upload is just one of them. You only need to modify or expand the relevant information of the $FILE_MIMES and $FILE_EXTS arrays to achieve other types of file uploads. Function. On the periphery of the function, you can write other related codes according to your own needs to achieve other functions, such as association with the database, etc.
http://www.bkjia.com/PHPjc/322934.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322934.htmlTechArticleDrupal file upload form example copy code The code is as follows: function upload_form() { $form = array(); / / If this #attribute is not present, upload will fail on submit $form['#attri...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn