그누보드에 있는 스마트 에디터를 일반 사이트에 적용할때 이미지 업로드 문제
먼저 file_uploader.php 을 아래와 같이 수정하고
</p><p>// default redirection</p><p> </p><p>$url = $_REQUEST["callback"].'?callback_func='.$_REQUEST["callback_func"];</p><p> </p><p>$bSuccessUpload = is_uploaded_file($_FILES['Filedata']['tmp_name']);</p><p> </p><p> </p><p>$data_dir = "mydir";</p><p>$data_url = "myurl";</p><p> </p><p> </p><p>// SUCCESSFUL</p><p> </p><p>if(bSuccessUpload) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$tmp_name = $_FILES['Filedata']['tmp_name'];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$name = $_FILES['Filedata']['name'];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$filename_ext = strtolower(array_pop(explode('.',$name)));</p><p> </p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>if (!preg_match("/(jpe?g|gif|bmp|png)$/i", $filename_ext)) {</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$url .= '&errstr='.$name;</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>} else {</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p> $file_name = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR'])).'_'.time().".".$filename_ext;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$save_dir = sprintf('%s/%s', $data_dir, $file_name);</p><p> $save_url = sprintf('%s/%s', $data_url, $file_name);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>@move_uploaded_file($tmp_name, $save_dir);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$url .= "&bNewLine=true";</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$url .= "&sFileName=".$name;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$url .= "&sFileURL=".$save_url;</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p><p>// FAILED</p><p>else {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$url .= '&errstr=error';</p><p>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p>header('Location: '. $url); </p><p>
다음 file_uploader_html5.php 파일을 아래와 같이 수정하였습니다.
</p><span style="font-size: 13.3333330154419px; line-height: 1.5;">$data_dir = "mydir";
</span><span style="font-size: 13.3333330154419px; line-height: 1.5;">$data_url = "myurl";</span><p> </p><p> </p><p> </p><p> <span class="Apple-tab-span" style="white-space:pre"> </span>$sFileInfo = '';</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$headers = array();</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span> </p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>foreach($_SERVER as $k => $v) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>if(substr($k, 0, 9) == "HTTP_FILE") {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$k = substr(strtolower($k), 5);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$headers[$k] = $v;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>} </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$file = new stdClass;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>//$file->name = str_replace("\0", "", rawurldecode($headers['file_name']));</p><p> $file->name = str_replace("\0", "", rawurldecode($headers['file_name']));</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$file->size = $headers['file_size'];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$file->content = file_get_contents("php://input");</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>$filename_ext = strtolower(array_pop(explode('.',$file->name)));</p><p> </p><p> if (!preg_match("/(jpe?g|gif|bmp|png)$/i", $filename_ext)) {</p><p> echo "NOTALLOW_".$file->name;</p><p> exit;</p><p> }</p><p> </p><p> </p><p><span class="Apple-tab-span" style="white-space:pre"> </span>//$file_name = iconv("utf-8", "cp949", $file->name);</p><p> $file_name = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR'])).'_'.time().".".$filename_ext;</p><p> $newPath = $data_dir."/".$file_name;</p><p> $save_url = sprintf('%s/%s', $data_url, $file_name);</p><p> </p><p> if(file_put_contents($newPath, $file->content)) {</p><p> $sFileInfo .= "&bNewLine=true";</p><p> $sFileInfo .= "&sFileName=".$file->name;</p><p> $sFileInfo .= "&sFileURL=".$save_url;</p><p> }</p><p> </p><p> echo $sFileInfo; </p><p>
그리고 php폴더에 있는 index.php 파일을 아래와 같이 수정하였습니다.
</strong></p><p><strong style="font-family: 돋움, dotum, helvetica; font-size: 12px; line-height: 21.6000003814697px; background-color: rgb(255, 255, 255);"></strong><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>/*</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> * jQuery File Upload Plugin PHP Example 5.14</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> * <a href="https://github.com/blueimp/jQuery-File-Upload" target="_blank" rel="noopener noreferrer">https://github.com/blueimp/jQuery-File-Upload</a></b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> *</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> * Copyright 2010, Sebastian Tschan</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> * <a href="https://blueimp.net" target="_blank" rel="noopener noreferrer">https://blueimp.net</a></b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> *</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> * Licensed under the MIT license:</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> * <a href="<a href="http://www.opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer">http://www.opensource.org/licenses/MIT</a>"><a href="http://www.opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer">http://www.opensource.org/licenses/MIT</a></a></b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> */</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>include_once("./_common.php");</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>@include_once("./JSON.php");</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>
</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>if( !function_exists('json_encode') ) {</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> function json_encode($data) {</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> $json = new Services_JSON();</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> return( $json->encode($data) );</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> }</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>}</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>
</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>@ini_set('gd.jpeg_ignore_warning', 1);</b></span></font></p><span style="font-size: 13.3333330154419px; line-height: 1.5;">$data_dir = "mydir";
</span><span style="font-size: 13.3333330154419px; line-height: 1.5;">$data_url = "myurl";</span><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>
</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>require('UploadHandler.php');</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>$options = array(</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> 'upload_dir' => $data_dir,</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> 'upload_url' => $data_url,</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> // This option will disable creating thumbnail images and will not create that extra folder.</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> // However, due to this, the images preview will not be displayed after upload</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b> 'image_versions' => array()</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>);</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>
</b></span></font></p><p><font face="돋움, dotum, helvetica"><span style="font-size: 12px; line-height: 21.6000003814697px;"><b>$upload_handler = new UploadHandler($options);</b></span></font><strong style="font-family: 돋움, dotum, helvetica; font-size: 12px; line-height: 21.6000003814697px; background-color: rgb(255, 255, 255);">
</strong></p><p><strong style="font-family: 돋움, dotum, helvetica; font-size: 12px; line-height: 21.6000003814697px; background-color: rgb(255, 255, 255);">
결론적으로 3개의 파일속 소스에서 $data_dir , $data_url 의 경로만 바꾼상태입니다.
그리고 사진 업로드 테스트 하면 해당 경로에 파일은 정상적으로 올라가는 상태입니다.
그러나 화면에는 아래와 같은 에러가 나네요
그누보드가 아닌 다른 일반 사이트에 쓸려면 또 수정해야되는 파일이 있는지요?
버전이라든지 알라봐야 할 부분좀 알려주시면 감사하겠습니다.
집에 가고 싶습니다. ㅠㅠ
답변 3개
그누보드5 배포에 포함된 스마트에디터의 사진 첨부하기 창은 네이버에서 배포하는 소스랑 틀립니다.
사진 올리는 부분은
https://github.com/blueimp/jQuery-File-Upload 에서 배포하는 것을 조금 수정해서 만든거에요.
위 스샷의 사진 첨부하기 창의 파일첨부를 수정하실려면,
php폴더에 있는 index.php, UploadHandler.php 를 수정하시면 됩니다.
file_uploader.php, file_uploader_html5.php 는 사용하고 있지 않습니다.( 위의 파일첨부창과 아무런 상관이 없음 )
$data_dir = "mydir";
$data_url = "myurl";
위의 $data_dir 은 절대 경로로 적어주시는게 좋습니다. ( 쓰기 권한 주셔야 합니다 )
예) /home/내계정/www/파일저장되는폴더/
$data_url 은 url 형식으로 해줘야 합니다.
예) http://www">http://www.도메인.com/파일저장되는폴더/
답변에 대한 댓글 2개
protected function handle_file_upload <<< 파일저장처리 함수인데요. 여기서 파일 저장이 잘 되는지 관련값은 잘 리턴 되는지 디버깅 해 보세요.
댓글을 작성하려면 로그인이 필요합니다.
http://lovelydoll.tistory.com/35">http://lovelydoll.tistory.com/35
참고해 보시구요..
기억으로는 실제업로드는
UploadHandler.php
에서 실행되는거로 알고 있습니다.
개발자도구 네트워크 탭열어놓으시고 테스트해보세요
화면에 나오지 않는 에러가 보일수도 있습니다.
네이버 스마트에디터는
그누에서 시작된게 아니기 때문 에러부분 캐치되시면 검색통해 해결하실 수 있으세요..
그나저나...아직도 집에 못가시면..ㅜ.ㅜ
집에 들어가셔서 쉬시고 내일 해결하시는게 빠를수도 있습니다....
답변에 대한 댓글 2개
일단 다시한번 삽질 해봐야 겠네요 ㅠ
hoyaf79@naver.com 입니다.
감사합니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
UploadHandler.php 보니 뭔가 어려워 어딜 수정해야될지 감이 안오네요 ㅠ
혹시 건드려봐야할 라인이나 함수부분 알려주시면 감사하겠습니다.