yuu_nkjm blog


2009-08-18(Tue) [長年日記] 編集

[Apache][Webアプリ][w2box] PHPで書かれたファイルアップローダw2boxの日本語環境における設定とカスタマイズ

はじめに

  • クライアントPCはWindowsを想定.特にdownloadをShift-JISに変えているあたり.
  • langファイルのjaをenに差し替えるのが吉.
    • 通常状態だと,インタフェースの日本語が化ける.化けないようにindex.phpのHTMLヘッダの文字コードを"iso-8859-1"から"utf8"に変更すると,日本語ファイル名のファイルがアップロード出来なくなる.
  • 初期状態で日本語ファイル名のアップロードは可能
    • しかし,アップローダが作成する保存ファイル名が良くない.保存ファイル名の文字コードがUTF8でないため,サーバ上でファイル名が日本語として可読でない.また,保存ファイル名がURLエンコードされていないため,そのファイルへのダウンロードURLがブラウザが解釈できない文字列となってしまう.その結果,ダウンロードした時のファイル名が日本語にならない.
  • dataとtmpをwwwrunの持ち物にする.777ではなく755にしないとだめっぽい.

拡張子の制限を外す

拡張子なんでもいけるよってのをconfigファイル中で示す.

$config['allowed_ext'] = array("*");

index.phpの拡張子チェック部分をなくす.

 / if (!in_array(strtolower(extname($filename)),
 /$config['allowed_ext'])) {
 / $errormsg = $lang['upload_badext'];
 / return;
 / }

w2box.jsの拡張子チェック部分を無効化

 if (ALLOWED_TYPES.indexOf(ext) == -1) {
   / document.getElementById("allowed").className ='red';
   / document.getElementById("upload").disabled = true;
   document.getElementById("allowed").className ='';
   document.getElementById("upload").disabled = false;
  } else {
   document.getElementById("allowed").className ='';
   document.getElementById("upload").disabled = false;
  }

ファイルサイズの上限を2Gに上げる

"/home/hoge/uploader/upload.cgi"を編集する.

 $high_max_upload = 52428800; #it will be change by params

"will be change"と書いてあるが,更新されない模様.

  $high_max_upload = 2048576000; #it will be change by params

としたらうまく行った.

"/home/hoge/uploader/config.php"を編集する.

 / maximum allowed file size in megabytes.
 $config['max_filesize'] = 2000; / MBytes

"/home/hoge/uploader/.htaccess"を編集する.一番最後の"memory_limit"の設定が重大な障害を引き起こす可能性がありそう.だけど,特定のディレクトリ以下だからいいか.

lv /home/hoge/uploader/.htaccess
 LimitRequestBody 2048576000
 php_value upload_max_filesize 2048576000
 php_value post_max_size 2048576000
 php_value memory_limit 2048576000
 php_value error_log /home/hoge/uploader/php.log

ファイル名を省略しない

index.php 513行目を変更.$maxlenが固定値ならもう少し上の方に書いておいて欲しいところ.



 echo '<a href="'.$url.'">';
 $maxlen=29;
 if ($maxlen>0 && strlen($file['file'])>$maxlen)
 /echo substr($file['file'],0,$maxlen-3)."...";
 echo $file['file'];
 else
   echo $file['file'];
 echo '';

テーブルのファイル名のカラムの割合を広くする

w2box.cssを変更

 table.sortable #th1 {width: 65%;}
 table.sortable #th2 {width: 13%;}
 table.sortable #th3 {width: 8%;}
 table.sortable #th4 {width: 3%;}
 table.sortable #th5 {width: 2%;}

雑多な設定

"/home/hoge/uploader/config.php"を編集する.

// title of your w2box
$config['w2box_title'] = "好きな名前";
 
// プログレスバー
//activate upload progress bar
$config['upload_progressbar']=true;
 
// .htaccessでアクセス制限しているのでw2boxではパスワードをかけない
// if true, activate admin authentication
$config['admin_actived'] = false;
 
// 何かとログは欲しい
//--- activity logging --
// if true, log activity to file
$config['log'] = true;
 
// アップロードはだいたい2.0GBまでだよ,と.
$lang['warning_msg'] ="------------- MAX File Size = about 2.0GB  ---------------"

アップロードしたファイルや新規ディレクトリの権限を変更(2009-11-10追記)

同じグループに属するプログラムからごにょごにょしたいので.

//250行目あたり
chmod ($filedest, 0775);
//280行目あたり
chmod($newdir, 0775);

文字コードの変更(2009-11-10追記)

index.phpに手を入れた.日本語ファイル名に対してbasename関数が正しく動作しないようなので,それを回避する.

//405行目あたり
 

削除の修正.ミスってたら怖い.

function deletefile($cell){
  global $config, $lang;
   //decode str
  $str=stripslashes(urldecode($cell));
  $str=substr($str, strpos($str,'href="')+6);
  $str=substr($str,0, strpos($str,'"'));
  if (substr($str,0,10)=="?download=")
  $str = substr($str,10,strlen($str));
 // ディレクトリを削除する時はディレクトリ名だけ,ファイルを削除の時はhttpからなことに注意.
  $str_tmp = preg_replace('/^http.*\//i','',$str);
   $file = $config['storage_path'].'/'.$str_tmp;
  //$file = $config['storage_path'].'/'.basename($str);
   if (!file_exists($file))
  echo $lang['delete_error_notfound']." ($file)";
  else {
    if (is_dir($file))
      $return = @rmdir($file);
    else
      $return = @unlink($file);
     if ($config['log_delete']) logadm($lang['DELETE']." ".$file);
    if ($return)
      echo "successful";
    else {
      if (is_dir($file))
        echo $lang['delete_error_cant_dir'];
      else
        echo $lang['delete_error_cant'];
    }
  }
  exit;
}

ディレクトリを作成する関数を修正

//make dir
if (isset ($_POST['dir'])) {
  if ($config['protect_makedir']) authorize();
  if ($dirlevel < $config['enable_folder_maxdepth']) {
`;
    //この置換では日本語のフォルダが通らん
    //$newdir = preg_replace("/[^0-9a-zA-Z\(\)\s]/i",'', $_POST['dir']);
         $newdir =$_POST['dir'];
     if ($newdir <> "") {
      $newdir = $config['storage_path']."/".$newdir;
      if (file_exists($newdir))
        $errormsg = $lang['make_error_exist'];
      else {
        if (mkdir($newdir)) {
    // necessary to allow upload files to a new folder
    // chmod($newdir, 0755);
    chmod($newdir, 0775);
          $loc = rooturl();
          if (sizeof($dir)>0) $loc .= join("/",$dir)."/";
          Header("Location: ".$loc);
          exit;
        } else
          $errormsg = $lang['make_error_cant'];
      }
    }
  } else {
    $errormsg = $lang['make_error_maxdepth'];
  }
}

アップロード部分を変更.

function uploadfile($file) {
  global $config, $lang, $max_filesize, $errormsg,$dir;
   if ($file['error']!=0) {
    $errormsg = $lang['upload_error'][$file['error']];
    return;
  }
   //determine filename
  $filename=$file['name'];
  if (isset($_POST['filename']) && $_POST['filename']!="") $filename=$_POST['filename'];
//   $filename=basename($filename);
//   $filename=explode(".",basename($filename));
//   $ext = $filename[count($filename)-1];
//   unset($filename[count($filename)-1]);
//   $filename=join('_',$filename).'.'.$ext;
 //   if (!in_array(strtolower(extname($filename)), $config['allowed_ext'])) {
//     $errormsg = $lang['upload_badext'];
//     return;
//   }
   $filesize=$file['size'];
  if ($filesize > $max_filesize) {
    @unlink($file['tmp_name']);
    $errormsg = $lang['upload_error_sizelimit'].' ('.getfilesize($max_filesize).').';
    return;
  }
   $filedest = $config['storage_path'].'/'.$filename;
  if (file_exists($filedest) && !$config['allow_overwrite']) {
    @unlink($file['tmp_name']);
    $errormsg = "$filename ".$lang['upload_error_fileexist'];
    return;
  }
   $filesource=$file['tmp_name'];
  if (!file_exists($filesource)) {
    $errormsg = "$filesource do no exist!";
    return;
  } else if (!move_uploaded_file($filesource,$filedest)) {
    if (!rename($filesource,$filedest)) {
      $errormsg = $lang['upload_error_nocopy'];
      return;
    }
  }
   if  ($errormsg=="") {
//     chmod ($filedest, 0755);
    chmod ($filedest, 0775);
     if ($config['log_upload']) logadm($lang['UPLOAD'].' '.$filedest);
    $loc = rooturl();
    if (sizeof($dir)>0) $loc .= join("/",$dir)."/";
    Header("Location: ".$loc);
    exit;
  }
}

ダウンロード部を修正.IEへの対応のためにShift-JISで統一した.

function downloadfile($file){
        global $config, $lang;
        // download linkは日本語のまま
//      $file = $config['storage_path'].'/'.basename($file);
        $filename = preg_replace('/^http.*\/\?download=/i','',$file);
        $file = $config['storage_path'].'/'.$filename;
        if (!is_file($file)) { return; }
        header("Content-Type: application/octet-stream; charset=Shift-JIS");
        header("Content-Size: ".filesize($file));
        $filename = mb_convert_encoding($filename, "Shift-JIS", "UTF-8");
        header("Content-Disposition: attachment; filename=\"".$filename."\"");
        header("Content-Length: ".filesize($file));
        header("Content-transfer-encoding: binary");
        @readfile($file);
        if ($config['log_download']) logadm($lang['DOWNLOAD'].' '.$file);
        exit;
}

今後の課題

mixi openidに対応したファイルあぷろだの作り方(修正)@labolo : mixiとかopenidでアカウント管理しても良いかもな.


2009-08-28(Fri) [長年日記] 編集

[お友達][生活][料理][blog] アーツのレシピ紹介

アーツのレシピ紹介 ― アーツとそのお友達のスペシャルショットと共に、冷凍してもおいしい料理や簡単おもてなし料理を紹介します。

お友達のSちゃんのお母さんが開設されている,ワンコのアーツによるレシピの紹介ブログです.

Sちゃん宅には大学の友達と何度かお邪魔したことがあるのですが,アーツの可愛さとご飯の美味しさには定評がありました.僕がこれまでに食べたお好み焼きのなかでは,Sちゃん宅のお好み焼きが一番美味しかったです.レシピが公開されているので,今度,家でも試してみたいところ.

僕は「ぎゅうぎゅう ひじき」の回と「帰る たことトマトのマリネ」の回のアーツがお気に入りです.要チェックです.


トップ «前月 最新 翌月» 追記 設定
2006|01|06|12|
2007|06|09|
2008|01|03|04|06|07|08|09|10|12|
2009|01|02|05|06|07|08|10|11|12|
2010|03|04|05|06|07|08|09|10|11|
2011|01|02|03|04|05|06|07|08|09|11|12|
2012|01|02|04|06|07|08|10|11|12|
2013|01|02|03|07|08|10|11|12|
2014|01|02|04|05|06|07|08|09|10|11|
2015|01|02|07|11|12|
2016|01|03|05|07|08|09|