PHP5.4ã®æ°æ©è½ã®ä¸ã¤: ã¢ãããã¼ãé²æã®åå¾
PHP5.4 Advent Calendarã®13æ¥ç®ã§ãã@co3kさんの記事ã«å¼ãç¶ããPHP5.4ã®æ°ããæ©è½ãç´¹ä»ãã¾ãã
PHP5.4ã§è¦éããªãæ°æ©è½ã®ï¼ã¤ã¨ãã¦ãä»ã¾ã§PHPåä½ã§ã¯åºæ¥ãªãã£ããã¡ã¤ã«ã®ã¢ãããã¼ãã®é²ææ å ±ãåå¾ã§ããããã«ãªãã¾ãããä»ã¾ã§ã¢ãããã¼ãã®é²ææ å ±ã¯ãAPCãFlashãªã©ãå©ç¨ããªããã°åå¾ã§ããªããã®ã§ããããã®è¨äºã§ã¯å®éã«åããµã³ãã«ã³ã¼ãã¨å ±ã«ãã®æ©è½ãç´¹ä»ãã¾ãã
å°å ¥
以ä¸ã®ãããªãã¡ã¤ã«ã¢ãããã¼ãã®ããã®ãã©ã¼ã ãç¨æãã¾ãããã®æãã©ã¡ã¼ã¿ã«session.upload_progress.nameãã£ã¬ã¯ãã£ãã®ã¨ã³ããªã追å ãã¾ããããã¯ã¢ãããã¼ãã®é²ææ å ±ãåå¾ããã¨ãã宣è¨ã§ããããç¡ãã¨é²æã¯ä¿åãããªãã®ã§æ³¨æã§ãããã®ä¾ã§ã¯ã"example"ã¨ããå¤ãã¤ãã¦ãã¾ãã
<?php session_start() ?> <form action="upload.php" target="hoge" method="POST" enctype="multipart/form-data"> <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="example" /> <input id="file1" type="file" name="file1" /> <input type="submit" value="upload" /> </form>
以ä¸ã®ãã©ã¼ã ããä½ãã§ã£ãããã¡ã¤ã«ãã¢ãããã¼ãããã¨ãã»ãã·ã§ã³å ã«ã¢ãããã¼ãã®é²æãè¨å®ãããããã«ãªãã¾ããã¢ãããã¼ããã¤ã¤ã以ä¸ã®ããã°ã©ã ã«ã¢ã¯ã»ã¹ããã¨ãé²ææ å ±ããã³ãããã¾ãã
<?php session_start(); $key = ini_get("session.upload_progress.prefix") . "example"; var_dump($_SESSION[$key]);
session.upload_progress.prefixãã£ã¬ã¯ãã£ã + å ã»ã©ã¤ãã"example" ã¨ãããã¼ã§ã»ãã·ã§ã³ä¸ã«é²ææ å ±ãä¿åããã¾ããä¸ã®ä¾ã ã¨ä»¥ä¸ã®ããã«è¨å®ããã¾ãã
array(5) { ["start_time"]=> int(1323713434) ["content_length"]=> int(155908) ["bytes_processed"]=> int(155908) ["done"]=> bool(true) ["files"]=> array(1) { [0]=> array(7) { ["field_name"]=> string(5) "file1" ["name"]=> string(13) "APC-3.1.9.tgz" ["tmp_name"]=> string(22) "/private/tmp/phpeMGDne" ["error"]=> int(0) ["done"]=> bool(true) ["start_time"]=> int(1323713434) ["bytes_processed"]=> int(155540) } } }
å種æ å ±ãä¿åããã¦ãããã¨ããããã¨æãã¾ãã100 * (content_lengthãã©ã¡ã¼ã¿ / byte_processedãã©ã¡ã¼ã¿) ã§ãã¢ãããã¼ãå ¨ä½ãã©ããããé²ãã ãããã¼ã»ã³ãã§æ±ãããã¾ããã¾ããåãã¡ã¤ã«ãã¨ã®é²æãããã§åå¾ãããã¨ãã§ãã¾ãã
å®éã«é²æä»ãã®ã¢ãããã¼ããã©ã¼ã ãä½ãå ´åã¯ãAjaxã§é²æãåå¾ãã¦è¡¨ç¤ºãããã¨ã«ãªãã¨æãã¾ãã以ä¸ã¯jQueryã使ã£ã¦é²æã%ã§è¡¨ç¤ºããä¾ã§ãã
<?php // index.php session_start(); ?><html><head></head> <body> <form action="index.php" method="POST" enctype="multipart/form-data"> <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="example" /> <input id="file1" type="file" name="file1" /> <input type="submit" value="upload" /> <span id="progress">0%</span> </form> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="text/javascript"> $(function() { $("form").submit(function() { var progress = $("#progress"); var f = function() { $.getJSON("./progress.php", function(data) { console.dir(data); if (data != null) { progress.text( "" + Math.round(100 * (data["bytes_processed"] / data["content_length"])) + "%" ); if (!data["done"]) { setTimeout(f, 200); } } }); }; setTimeout(f, 300); }); }); </script> </body></html>
<?php # progress.php session_start(); $key = ini_get("session.upload_progress.prefix") . 'example'; echo isset($_SESSION[$key]) ? json_encode($_SESSION[$key]) : json_encode(null);
以ä¸ã¯ãµã³ãã«ãä½æããéã«php.iniã«è¿½å ã§è¨å®ãããã£ã¬ã¯ãã£ãã§ãã
upload_max_filesize = 2000M error_reporting = E_ALL session.upload_progress.min_freq = 0.000000001 session.upload_progress.cleanup = off post_max_size = 2000M memory_limit = 2000M
å種ãã£ã¬ã¯ãã£ãã¯ãå®éã«æ¬çªã§å©ç¨ããéã¯é©åã«è¨å®ãã¦ä¸ãããç¹ã«session.upload_progress.cleanupã¯ã»ãã·ã§ã³ä¸ã«ã´ããæ®ããªãããã«onã«ããã»ããããã¨æãã¾ãã
åè
çµãã
ã¨ããããã§ãPHP5.4ã®ãã¡ã¤ã«ã¢ãããã¼ãã®é²æãåå¾ããã³ã¼ããç´¹ä»ãã¾ããã次ã¯@tpyamamotoãããé¡ããããã¾ãã¼ã