123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2017/12/6 0006
- * Time: 17:30
- */
- header('Access-Control-Allow-Origin:*');//允许跨域
- $lang = $_REQUEST['lang'];
- $imgfile = 'imgfile_'.$lang;
- $file = $_FILES[$imgfile];
- //$fileName = $_REQUEST['fileName'];
- $langList = empty($langStr) ? [] : explode(',', $langStr);
- $imgDir = $_POST['imgDir'] ? $_POST['imgDir'] : 'images/activity/'.$lang;
- $imgDir = '../'.$imgDir;
- //如果语言不为空,则分别把图片上传到每个语言文件夹
- if(!empty($lang)) {
- die(upload($file, $imgDir));
- }else {
- die(response(4, '参数错误'));
- }
- /**
- * 移动上传文件到指定文件夹
- * @param $fileName
- * @param $srcFile
- * @param $desImgDir
- * @return json
- */
- function move_upload($fileName, $srcFile, $desImgDir)
- {
- $uploadPath = getFolder($desImgDir);
- if(!$uploadPath) {
- return response(1, '存放上传图片文件夹创建失败');
- }
- if(empty($srcFile['name'])) {
- return response(2, '上传文件为空');
- }
- $newFile = $uploadPath . '/' . $fileName;
- if(!move_uploaded_file($srcFile[ "tmp_name" ], $newFile)) {
- return response(3, '文件移动出错');
- }
- return response(0, '文件上传成功', ['imgPath' => $fileName]);
- }
- /**
- * 进行文件上传
- * @param $imgDir
- * @return json
- */
- function upload($file, $imgDir) {
- $uploadPath = getFolder($imgDir);
- if(!$uploadPath) {
- return response(1, '存放上传图片文件夹创建失败');
- }
- if(empty($file['name'])) {
- return response(2, '上传文件为空');
- }
- $fileName = getName($file['name']);
- $newFile = $uploadPath . '/' . $fileName;
- if(!move_uploaded_file($file[ "tmp_name" ], $newFile)) {
- return response(3, '文件移动出错');
- }
- return response(0, '文件上传成功', ['imgPath' => $fileName]);
- }
- /**
- * 进行文件上传 多语言文件夹图片上传
- * @param $imgDir
- * @return json
- */
- function uploadLangList($file, $imgDir, $langList) {
- if(empty($langList)) {
- return response(4, '上传路径错误');
- }
- $fileName = getName($file['name']);
- $count = 0;
- $tmpFilePath = '';
- foreach ($langList as $lang) {
- $tmpDir = $imgDir . $lang;
- $uploadPath = getFolder($tmpDir);
- if(!$uploadPath) {
- return response(1, '存放上传图片文件夹创建失败');
- }
- if(empty($file['name'])) {
- return response(2, '上传文件为空');
- }
- $newFile = $uploadPath . '/' . $fileName;
- //由于move_uploaded_file会删除之前的文件,所以后面的几张图片改为复制
- if($count == 0 ) {
- if(!move_uploaded_file($file[ "tmp_name" ], $newFile)){
- return response(3, '文件移动出错');
- }
- $tmpFilePath = $newFile;
- }
- if($count > 0) {
- if(!copy($tmpFilePath, $newFile)) {
- return response(5, '文件复制出错');
- }
- }
- $count++;
- }
- return response(0, '文件上传成功', ['imgPath' => $fileName]);
- }
- //---------end----------
- /**
- * 自动创建存储文件夹
- * @return string
- */
- function getFolder($pathStr)
- {
- if ( !file_exists( $pathStr ) ) {
- if ( !mkdir( $pathStr , 0777 , true ) ) {
- return false;
- }
- }
- return $pathStr;
- }
- /**
- * 重命名文件
- * @return string
- */
- function getName($oriName)
- {
- $ext = strtolower(strrchr($oriName, '.'));
- if (!$ext) {
- $ext = '.jpg';
- }
- return $fileName =uniqid().'_'.rand(1, 999).$ext;
- }
- /**
- * 统一输出返回json内容
- *
- * @access public
- * @date 2016-08-16
- * @param int $ret 状态码,0成功
- * @param string $msg 错误信息
- * @param array $data 返回数据内容
- * @return json;
- */
- function response($ret = 0, $msg = '', array $data = array())
- {
- $result = [
- 'ret' => $ret,
- 'msg' => $msg,
- 'data' => $data,
- ];
- return json_encode($result);
- }
|