文件上传函数(有空会自己修改)
来源:互联网 发布:2017年10月份宏观数据 编辑:程序博客网 时间:2024/06/05 07:32
CODE
<?php/** * A function for easily uploading files. This function will automatically generate a new * file name so that files are not overwritten. * Taken From: http://www.bin-co.com/php/scripts/upload_function/ * Arguments: $file_id- The name of the input field contianing the file. * $folder - The folder to which the file should be uploaded to - it must be writable. OPTIONAL * $types - A list of comma(,) seperated extensions that can be uploaded. If it is empty, anything goes OPTIONAL * Returns : This is somewhat complicated - this function returns an array with two values... * The first element is randomly generated filename to which the file was uploaded to. * The second element is the status - if the upload failed, it will be 'Error : Cannot upload the file 'name.txt'.' or something like that */function upload($file_id, $folder="", $types="") { if(!$_FILES[$file_id]['name']) return array('','No file specified'); $file_title = $_FILES[$file_id]['name']; //Get file extension $ext_arr = split("\.",basename($file_title)); $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension //Not really uniqe - but for all practical reasons, it is $uniqer = substr(md5(uniqid(rand(),1)),0,5); $file_name = $uniqer . '_' . $file_title;//Get Unique Name $all_types = explode(",",strtolower($types)); if($types) { if(in_array($ext,$all_types)); else { $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any. return array('',$result); } } //Where the file must be uploaded to if($folder) $folder .= '/';//Add a '/' at the end of the folder $uploadfile = $folder . $file_name; $result = ''; //Move the file from the stored location to the new location if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) { $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any. if(!file_exists($folder)) { $result .= " : Folder don't exist."; } elseif(!is_writable($folder)) { $result .= " : Folder not writable."; } elseif(!is_writable($uploadfile)) { $result .= " : File not writable."; } $file_name = ''; } else { if(!$_FILES[$file_id]['size']) { //Check if the file is made @unlink($uploadfile);//Delete the Empty file $file_name = ''; $result = "Empty file found - please use a valid file."; //Show the error message } else { chmod($uploadfile,0777);//Make it universally writable. } } return array($file_name,$result);}
Sample
PHP Part
if($_FILES['image']['name']) { list($file,$error) = upload('image','uploads/','jpeg,gif,png'); if($error) print $error;}
HTML Part
<form action="" method="post" enctype="multipart/form-data"><input type="file" name="image" /><input type="submit" value="Upload" name="action" /></form>
0 0
- 文件上传函数(有空会自己修改)
- 上传文件样式修改
- 上传文件样式修改
- 修改上传文件按钮值
- 修改fck 文件上传文件夹
- 修改PHP文件上传限制
- 修改文件上传大小限制
- 修改上传文件的名字
- Testlink上传文件上限修改
- IIS 修改上传文件限制
- 修改文件上传默认样式
- 修改文件上传默认样式
- 修改文件上传大小限制
- 修改文件上传按钮样式
- 修改 文件上传 默认样式
- PHP文件上传修改配置文件
- 文件上传大小限制修改
- 修改默认上传文件样式
- jQuery学习实例:焦点图轮播特效
- C++11新特性之 nullptr
- leetcode:House Robber
- SSH远程登录的使用和介绍
- 对象
- 文件上传函数(有空会自己修改)
- fwrite和fread
- 高斯混合模型
- jvm HotSpot虚拟机主要参数表
- SQLServer 扩展事件(Extended Events)
- Groovy基础——接口的实现方式
- 解决 spring mvc 返回 json 时 在 ie 下提示下载问题
- LintCode- 链表倒数第n个节点
- hiho 9 状态压缩二