思路
1.验证上传的数据:文件是否存在file_exists ;文件大小;是否为真的图片;
2.验证文件格式 :$file_type = strtolower( end( $tmp_file_extend ) );in_array
3.检验 is_uploaded_file() 函数检查指定的文件是否是通过 HTTP POST 上传的。
4.没有对应文件夹的话 新建文件夹与复制权限
if ( ! file_exists( $upload_dir ) ){
mkdir( $upload_dir,0777);
chmod( $upload_dir,0777);
}
5.移动 move_uploaded_file() 函数将上传的文件移动到新位置。
例子1
1.代码:获取处理参数 保存至数据库与文件夹
<?php
namespace app\common\services;
use app\models\book\Images;
use Yii;
class UploadService extends BaseService
{
public static function uploadByFile ($file_name,$file_path,$bucket) {
if ( !$file_name ) {
return self::_err("参数文件名是必须的");
}
if ( !$file_path || !file_exists( $file_path )) {
return self::_err("参数文件名是必须的");
}
$upload_config = Yii::$app->params['upload'];
if (!isset ($upload_config[$bucket])) {
return self::_err("指定参数篮子错误");
}
$tmp_file_extend = explode(".",$file_name);
$file_type = strtolower( end( $tmp_file_extend ) );
$hash_key = md5 (file_get_contents($file_path));
$upload_dir_path = UtilService::getRootPath() . "/web" . $upload_config[ $bucket ].'/';
$folder_name = date ("Ymd");
$upload_dir = $upload_dir_path.$folder_name;
if ( ! file_exists( $upload_dir ) ){
mkdir( $upload_dir,0777);
chmod( $upload_dir,0777);
}
$upload_file_name = $folder_name."/".$hash_key.".{$file_type}";
if ( is_uploaded_file( $file_path ) ){
move_uploaded_file($file_path,$upload_dir_path.$upload_file_name);
}else{
file_put_contents( $upload_dir_path.$upload_file_name,file_get_contents( $file_path ) );
}
self::saveImage( $bucket,$upload_file_name );
return [
'code' => 200,
'path' => $upload_file_name,
'prefix' => $upload_config[ $bucket ] ."/"
];
}
public static function saveImage($bucket = '' ,$file_key = ''){
$images = new Images();
$images->bucket = $bucket;
$images->file_key = $file_key;
$images->created_time = date("Y-m-d H:i:s",time());
return $images->save();
}
}
2.调用:控制器就是在处理参数 在调用服务保存
public function actionPic()
{
$bucket = trim($this->post("bucket"));
$callback = "window.parent.upload";
if ( !$_FILES || !isset($_FILES['pic']) ){
return "<script>{$callback}.error('请选择文件之后在提交')</script>";
}
$file_name = $_FILES['pic']['name'];
$tmp_file_extend = explode(".",$file_name);
if ( !in_array( strtolower ( end ( $tmp_file_extend ) ),$this->allow_file_type ) ) {
return "<script>{$callback}.error('请上传指定类型的文件')</script>";
}
//todo upload function
$ret = UploadService::uploadByFile( $file_name,$_FILES['pic']['tmp_name'],$bucket);
if ( ! $ret ) {
return "<script>{$callback}.error('".UploadService::getLastErrorMsg()."')</script>";
} else {
return "<script>{$callback}.success('{$ret['path']}')</script>";
}
}
例子2
PHP上传类
各种检测后 进行把上传的文件移动到目标位置处
<?php
//$fileInfo=$_FILES['myFile'];
function uploadFile($fileInfo,$uploadPath = 'uploads',$flag=true,$allowExt=array('jpeg','jpg','gif','png'),$maxSize = 2097152){
// 判断错误号
if ($fileInfo ['error'] > 0) {
switch ($fileInfo ['error']) {
case 1 :
$mes = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2 :
$mes = '超过了表单MAX_FILE_SIZE限制的大小';
break;
case 3 :
$mes = '文件部分被上传';
break;
case 4 :
$mes = '没有选择上传文件';
break;
case 6 :
$mes = '没有找到临时目录';
break;
case 7 :
case 8 :
$mes = '系统错误';
break;
}
echo ( $mes );
return false;
}
$ext = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION );
// $allowExt = array (
// 'jpeg',
// 'jpg',
// 'png',
// 'gif'
// );
if(!is_array($allowExt)){
exit('系统错误');
}
// 检测上传文件的类型
if (! in_array ( $ext, $allowExt )) {
exit ( '非法文件类型' );
}
//$maxSize = 2097152; // 2M
// 检测上传文件大小是否符合规范
if ($fileInfo ['size'] > $maxSize) {
exit ( '上传文件过大' );
}
//检测图片是否为真实的图片类型
//$flag=true;
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
exit('不是真实图片类型');
}
}
// 检测文件是否是通过HTTP POST方式上传上来
if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {
exit ( '文件不是通过HTTP POST方式上传上来的' );
}
//$uploadPath = 'uploads';
if (! file_exists ( $uploadPath )) {
mkdir ( $uploadPath, 0777, true );
chmod ( $uploadPath, 0777 );
}
$uniName = md5 ( uniqid ( microtime ( true ), true ) ) . '.' . $ext;
$destination = $uploadPath . '/' . $uniName;
if (! @move_uploaded_file ( $fileInfo ['tmp_name'], $destination )) {
exit ( '文件移动失败' );
}
//echo '文件上传成功';
// return array(
// 'newName'=>$destination,
// 'size'=>$fileInfo['size'],
// 'type'=>$fileInfo['type']
// );
return $destination;
}
PHP调用类
<?php
header('content-type:text/html;charset=utf-8');
include_once 'upload.func.php';
$fileInfo=$_FILES['myFile'];
// $newName=uploadFile($fileInfo);
// echo $newName;
// $newName=uploadFile($fileInfo,'imooc');
// echo $newName;
//$allowExt='txt';
$allowExt=array('jpeg','jpg','png','gif','html','txt');
$newName=uploadFile($fileInfo,'imooc',false,$allowExt);
echo $newName;