달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

'uploadify'에 해당되는 글 1건

  1. 2012.03.12 [JQuery]Uploadfy 한글 폴더, 한글 파일 업로드 오류 수정
index.php

$('#file_upload').uploadify({
   'uploader'  : './swf/uploadify.swf',
   'script'    : './common/uploadify.php',
   'cancelImg' : './images/cancel.png',
   'folder'    : './uploads<?=$path?>',
   'multi'     : true,
   'fileExt'   : '*.jpg;*.jpeg;*.png;*.gif',
   'onComplete'  : function(event, ID, fileObj, response, data) {
    var ret = response.toString().split('|');
    if(ret[0] != 0){
     alert("Err : " + ret[0] + "\n" + ret[1]);     // ret[0] = 오류 코드 ,  ret[1] = 파일명     uploadify.php 에서 받은 리턴값
    } else {
     $("#filename").val(ret[1]);
          $.ajax({
           url : './common/upload.php',
           type : 'POST',
           dataType : 'json',
           data : $('form').serialize(),
           timeout : 5000,
           error : function(xhr){
            alert('DB 저장 중 오류가 발생하였습니다.\n' + xhr.responseText);
           },
           success : function(xhr){
       getThumbnailList(pathid);
           }
          });
    } 
      }
  });



<form name="frmFile" id="frmFile" method="POST" action="common/upload.php">
    <input type="hidden" id="filename" name="filename" value="" />
    <input type="hidden" id="pathID" name="pathID" value="<?=$pathid?>" />
    <input type="hidden" id="pathDir" name="pathDir" value="uploads<?=$path?>" />
    <div id="uploadBtn_layer"><input type="button" id="btnStartUpload" value="업로드 시작"/></div>

    <input type="file" name="file_upload" id="file_upload" />
   </form>

pathid 나  pathDir 은 DB에서 읽어온 정보를 넣는 것이므로 개인적인 작업이라 무시하시고 수정해서 사용 가능

jquery.uploadify.xx.js 파일  에서
g.folder=escape(f.folder); 를 찾아서
g.folder=encodeURIComponent(f.folder);  로 수정


Uploadifiy.php

if (!empty($_FILES)) {
  $tempFile = $_FILES['Filedata']['tmp_name'];
  $targetPath = $_SERVER['DOCUMENT_ROOT'] . urldecode($_REQUEST['folder']) . '/';    ///* 폴더 명을 urldecode 로 받는다.  */;
  $filename = iconv("UTF-8", "EUC-KR", $_FILES['Filedata']['name']);    // /* 파일 명을 urldecode 로 받고  iconv 로 처리 */
  $targetFile =  str_replace('//','/',iconv("UTF-8", "EUC-KR", $targetPath)) . $filename;   // /* 폴더명도 iconv 처리  */
  
   if(file_exists($targetFile)){
   echo "-200|overlap file name. [".iconv("EUC-KR", "UTF-8", $filename)."]";    // 이미 파일이 존재할  경우 -200cjfl
  } else {
   move_uploaded_file($tempFile, $targetFile);
   echo "0|".iconv("EUC-KR", "UTF-8", $filename);     // 업로드 성공하면 0
  }
 }
 else
 {
  echo "-100|Uploaded file is empty.";      // 파일이 없으면 -100 처리
 }
uploadify.php 에서 성공 처리 되면  upload.php 를  호출해서 DB에 데이터를 넣는다 이때 중요한것은 uploadify.php 에서 리턴시 다시 iconv("EUC-KR", "UTF-8", $filename)로
반대로 인코딩 해서 보내야 정상적으로 처리된다.. serialize 자체가 encodeURIcomponent() 가 붙다보니.. 인코딩 디코딩 부분이 까다롭다.

Upload.php

$filename = urldecode($_REQUEST['filename']);   // file명은 꼭 urldecode로
 $pathId = $_REQUEST['pathID'];
 if($pathId == "") $pathId = 1;
  $fullpath = "gallery/uploads";
 $path_result = mysql_query("select * from path where pidx = $pathId") or die(mysql_error());
 while($path_row = mysql_fetch_array($path_result)){
  $fullpath = $path_row[FULLPATH];
 }
 
 $targetFile =  $_SERVER['DOCUMENT_ROOT'] . $lib->upload_dir . iconv("UTF-8", "EUC-KR", $fullpath) . iconv("UTF-8", "EUC-KR", $filename);   // 경로명은 DB에서 가져와서 iconv 만 했지만 파라미터로 받을 경우 urldecode 후 iconv
 $ext = substr(strrchr($targetFile, '.'), 1);
 $filesize = filesize($targetFile);
 $width = 0;
 $height = 0;
 if(strtolower($ext) == "jpg" || strtolower($ext) == "gif" || strtolower($ext) == "png") {
  list($width, $height) = getimagesize($targetFile);
 }
 $now = date("Y-m-d h:i:s", time());
 //$filename = iconv("EUC-KR", "UTF-8", $filename);
 $file_query = "insert into Files (filename, pidx, extension, width, height, filesize, regdate)
  values('$filename','$pathId','$ext','$width','$height','$filesize','$now')";
 
 mysql_query($file_query) or die(mysql_error());



대충 문제가 됐던 것은 php 에서 리턴받은 한글 파일을 다시 다른 페이지로 넘기는 과정에서 한글 문제가 있었다.
어쨋든 폴더명이 한글이던 파일명이 한글이던 잘 올라간다.





Posted by SadDev
|