我在網上找到的關于檔案上傳部分,然后記錄下來,方便下次使用,
寫一個公共函式用于上傳檔案,例如寫在function.php檔案中,而不是直接修改common.php檔案
直接上代碼,首先是函式部分:
引數部分:用戶id,檔案,你也可以設定檔案大小和上傳路徑
function post_upload_one($uid,$file'){ $file = request()->file($file); if (empty($file)) { $data['code'] = '2'; $data['msg'] = '未選擇圖片'; return $data; } $size=11548484$path='/uploads/apifile'; $info = $file->move(Env::get('root_path') .'/public'.$path.DIRECTORY_SEPARATOR); if($info) { // 獲取附件資訊 $file_info = [ 'uid' => $uid, 'name' => $file->getInfo('name'), 'mime' => $file->getInfo('type'), 'path' => str_replace('\\', '/','uploads/apifile' . DIRECTORY_SEPARATOR . str_replace('\\', '/', $info->getSaveName()) ), 'ext' => $info->getExtension(), 'size' => $info->getSize(), 'md5' => $info->hash('md5'), 'sha1' => $info->hash('sha1'), 'module' => 'user', ]; if ($file_add = AttachmentModel::create($file_info)) { $data['code'] = '0'; $data['msg'] = $file_add->id; return $data; } } $data['code'] = '1'; $data['msg'] = $file->getError(); return $data; }
然后是呼叫我們寫好的公共函式
$data['image']=$_FILES; $cc = post_upload_one($uid,'image');
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103755.html
標籤:PHP
