XeStorage를 사용하여 이미지 업로드 이후 생성된 링크를 통해 이미지를 불러올 수 없음.
Laeng
- 2018.05.29
- 조회수 418
- 댓글수 6
public function Upload(Request $request)
{
$r = (object)[];
if(($uploadedFile = $request->file('file')) === null)
{
$r->error = "업로드 실패. 전송된 파일이 없습니다.";
return json_encode($r);
}
if($uploadedFile->getSize() > 10 * pow(1024, 2))
{
$r->error = "업로드 실패. 파일 크기가 10MB 이하여야 합니다.";
return json_encode($r);
}
$extensions = array(
'png', 'jpg', 'jpeg', 'jpe', 'svg', 'bmp', 'tif', 'tiff', 'ico',
'asf', 'asx', 'wmv', 'wmx', 'wm', 'avi', 'divx', 'flv', 'mov', 'qt', 'mpeg', 'mpg', 'mpe', 'mp4', 'm4v', 'ogv', 'webm', 'mkv',
'mp3', 'm4a', 'm4b', 'ogg', 'oga', 'mid', 'midi', 'wma', 'wmx', 'mka',
'txt', 'pdf', 'zip', 'gz', 'gzip', 'rar', '7z', 'hwp',
'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx',
'obt', 'odp', 'ods', 'odg', 'odc', 'odb', 'odf',
'key', 'numbers', 'pages'
);
if(!in_array(strtolower($uploadedFile->getClientOriginalExtension()), $extensions))
{
$r->error = "업로드 실패. 허용되지 않는 파일 확장자입니다.";
return json_encode($r);
}
$file = XeStorage::upload($uploadedFile, 'public/editor');
$r->url = sprintf("%s/%s", $file->path, $file->filename);
return json_encode($r);
}
$.upload = function (type, file, editor) {
let name = file.name.split(".")[0];
let out = new FormData();
out.append('type', type);
out.append('file', file, file.name);
$.ajax({
method: 'POST',
url: '/post/upload',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
contentType: false,
cache: false,
processData: false,
dataType: 'JSON',
data: out,
success: function (r) {
if(typeof(r.error) !== "undefined") XE.toast("danger", r.error);
else editor.summernote('insertImage', r.url);
//else $('#editor').summernote('editor.insertImage', r.url);
},
error: function (jqXHR, textStatus, errorThrown) {
console.error(textStatus + " " + errorThrown);
console.log(jqXHR);
}
});
};
jQuery 의 r.url 의 값은 "public/editor/56/42/201805291211140c35376d3ac1fafbfc6d904accb235945cb2bf47.jpg" 가 나옵니다.
위와 같이 PHP 와 jQuery 가 있을때, ajax 를 사용하여 PHP upload 메소드를 실행합니다.
이미지 업로드는 정상적으로 되고, 리턴도 잘 됩니다.
위와 같이 r.url 에 리턴된 값을 가져와 아래와 같이 만들어진 링크를 브라우져에 입력하니,
http://xe.vm/public/editor/56/42/201805291211140c35376d3ac1fafbfc6d904accb235945cb2bf47.jpg
"잘못된 요청입니다. 입력한 주소를 확인하시기 바랍니다." 404를 뱉어버립니다....
음.... nginx 세팅을 핫링크를 방지하도록 해놨긴 한데, 세팅 당시 403 오류를 뱉도록 했었거든요....
어딜 어루어만져줘야 이미지가 잘 출력될 수 있을까요?
+ public/editor/56/42 폴더에 이미지가 잘 저장되어 있습니다.
0개 댓글