PHP100 2019-03-27
代码如下:
function upload_form() { $form = array(); // If this #attribute is not present, upload will fail on submit $form['#attributes']['enctype'] = 'multipart/form-data'; $form['file_upload'] = array( '#title' => t('Upload file'), '#type' => 'file', ); $form['submit_upload'] = array( '#type' => 'submit', '#value' => 'Submit' ); return $form; } function upload_submit($form, &$form_state) { $validators = array(); $dest = file_directory_path(); $file = file_save_upload('file_upload', $validators, $dest); //$file will be 0 if the upload doesn't exist, or the $dest directory //isn't writable if ($file != 0) { $file->filepath; // 文件相对路径 } else { form_set_error('myform', t("Failed to save the file.")); } }
代码如下:
<form enctype="multipart/form-data" action="upload.php" method="post"> <input type="hidden" name="max_file_size" value="100000"> <input name="userfile" type="file"> <input type="submit" value="上传文件"> </form>
代码如下:
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("没有文件被上传"); } }
代码如下:
<input name="userfile" type="file">
代码如下:
<input name="userfile[]" type="file"> <input name="userfile[]" type="file"> <input name="userfile[]" type="file">
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh