验证中...
Languages: PHP
Categories: Web开发技术
Latest update 2019-06-25 11:02
gistfile1.txt
Raw Copy
public function completeApplication(Request $request)
{
$files = $request->file('file');
$filePath = []; // 定义空数组用来存放图片路径
if (!is_array($files)) {
if ($files->isValid()) {
$allowed_extensions = ['image/jpeg', 'image/png', 'image/gif'];
if ($files->getClientMimeType() && !in_array($files->getClientMimeType(), $allowed_extensions)) {
return ('您只能上传PNG、JPG或GIF格式的图片!');
}
$real_path = $files->getRealPath();
$hash = [
'md5' => md5_file($real_path),
'sha1' => sha1_file($real_path),
];
//根据哈希获得文件
$uploaded = File::where($hash)->first();
$type = 'attachment';
if (!$uploaded) {
$path = $files->store('public/' . $type . '/' . date('Y-m'));
$data = array_merge($hash, [
'name' => $files->getClientOriginalName(),
'mime_type' => $files->getClientMimeType(),
'size' => $files->getSize(),
'url' => Storage::url($path),
'path' => $path,
]);
$uploaded = File::create($data);
$filePath[] = asset($uploaded->url);
}
} else {
return "上传图片出错,请重试!";
}
} else {
foreach ($files as $file) {
if ($file->isValid()) {
$allowed_extensions = ['image/jpeg', 'image/png', 'image/gif'];
if ($file->getClientMimeType() && !in_array($file->getClientMimeType(), $allowed_extensions)) {
return ('您只能上传PNG、JPG或GIF格式的图片!');
}
$real_path = $file->getRealPath();
$hash = [
'md5' => md5_file($real_path),
'sha1' => sha1_file($real_path),
];
//根据哈希获得文件
$uploaded = File::where($hash)->first();
$type = 'attachment';
if (!$uploaded) {
$path = $file->store('public/' . $type . '/' . date('Y-m'));
$data = array_merge($hash, [
'name' => $file->getClientOriginalName(),
'mime_type' => $file->getClientMimeType(),
'size' => $file->getSize(),
'url' => Storage::url($path),
'path' => $path,
]);
$uploaded = File::create($data);
$filePath[] = asset($uploaded->url);
}
} else {
return "上传图片出错,请重试!";
}
}
}
return $filePath;
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025