¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

PHP/¥µ¥ó¥×¥ë/File Uploader

Last-modified: 2018-11-14 (¿å) 15:23:57 (2153d)
Top / PHP / ¥µ¥ó¥×¥ë / File Uploader

PHP/¥µ¥ó¥×¥ë

¤Ï¤¸¤á¤Ë

¥Õ¥©¡¼¥à¤ÇÁ÷¿®¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤Ï¥Æ¥ó¥Ý¥é¥ê¥Õ¥¡¥¤¥ë¤È¤·¤Æ¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Þ¤¹¡£
¤³¤Î¥Æ¥ó¥Ý¥é¥ê¥Õ¥¡¥¤¥ë¤Ï½èÍý½ªÎ»¸å¤Ëºï½ü¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢Êݸ¤¹¤ë¾ì¹ç¤Ï½ñ¤­¹þ¤ß¸¢¸Â¤ÎÍ­¤ë¥Ç¥£¥ì¥¯¥È¥ê¤Ë¥³¥Ô¡¼¤·¤Þ¤¹¡£
¤³¤Î¤È¤­¥¢¥Ã¥×¥í¡¼¥É¤Î¸µ¥Õ¥¡¥¤¥ë̾¤Ï¼èÆÀ¤Ç¤­¤ë¤Î¤Ç¸µ¥Õ¥¡¥¤¥ë̾¤Ç¥³¥Ô¡¼¤Ç¤­¤Þ¤¹¡£

¥µ¥ó¥×¥ë

½ñ¤­¹þ¤ß²Äǽ¤Êupload¥Ç¥£¥ì¥¯¥È¥ê¤òÍÑ°Õ¤·¡¢¼¡¤Î¥Õ¥¡¥¤¥ë¤òÀßÃÖ¤·¤Þ¤¹¡£

uploader.html

<html>
<head><title>File Uploader</title></head>

<body>
<h1>File Uploader</h1>

<form method="post" enctype="multipart/form-data" action="uploader.php">
  <input type="file" name="upfile">
  <input type="submit" value="upload">
</form>

</body>
</html>

uploader.php

<html>
<head><title>File Uploader</title></head>

<body>
<h1>File Uploader</h1>

<?php
    define("UPLOAD_DIRECTORY", "./upload/");

    $fname = $_FILES['upfile']['name'];
    if (move_uploaded_file($_FILES['upfile']['tmp_name'], UPLOAD_DIRECTORY . $fname)) {
        chmod(UPLOAD_DIRECTORY . $fname, 0644);
        print("<b>" . $fname . "</b> uploaded!");
    } else {
        print("<b>" . $fname . "</b> upload failed ...<br />" . $_FILES['upfile']['error']);
    }
?>

</body>
</html>

´ØÏ¢¾ðÊó

php.ini¤Î¥Õ¥¡¥¤¥ë¥¢¥Ã¥×¥í¡¼¥É´ØÏ¢¹àÌÜ

file_uploads¥Õ¥¡¥¤¥ë¥¢¥Ã¥×¥í¡¼¥É²ÄÈÝ
upload_tmp_dir¥¢¥Ã¥×¥í¡¼¥ÉÍѥƥó¥Ý¥é¥ê¥Ç¥£¥ì¥¯¥È¥ê¡Ê»ØÄꤷ¤Ê¤¤¤È¤­¤Ï¥·¥¹¥Æ¥à¥Ç¥Õ¥©¥ë¥È¤È¤Ê¤ê¤Þ¤¹¡Ë
upload_max_filesize¥¢¥Ã¥×¥í¡¼¥É¥Õ¥¡¥¤¥ë¤ÎºÇÂ祵¥¤¥º

²¼µ­¥µ¥ó¥×¥ë¤Î'upfile'¤È¤¤¤¦name¤ÇÁ÷¿®¤·¤¿¾ì¹ç¤Î¥Õ¥¡¥¤¥ë¥á¥¿¾ðÊó

$_FILES['upfile']['name']¼ÂºÝ¤Î¥Õ¥¡¥¤¥ë̾
$_FILES['upfile']['tmp_name']¥Æ¥ó¥Ý¥é¥ê¥Õ¥¡¥¤¥ë¤È¤·¤Æ¥¢¥Ã¥×¤µ¤ì¤Æ¤¤¤ë¤È¤­¤Î¥Õ¥¡¥¤¥ë̾
$_FILES['upfile']['type']'image/jpeg'¤È¤¤¤Ã¤¿MIME¥¿¥¤¥×
$_FILES['upfile']['size']¥Õ¥¡¥¤¥ë¥Ð¥¤¥È¿ô
$_FILES['upfile']['error']¥¨¥é¡¼¥³¡¼¥É¿ôÃÍ UPLOAD_ERR_OK(= 0)¤Ê¤É

¥¨¥é¡¼¥³¡¼¥É¿ôÃÍ

Äê¿ôÃͳµÍ×
UPLOAD_ERR_OK0¥¢¥Ã¥×¥í¡¼¥ÉÀ®¸ù
UPLOAD_ERR_INI_SIZE1¥¢¥Ã¥×¥í¡¼¥É¥Õ¥¡¥¤¥ë¤ÎÍÆÎ̤¬php.ini¤Îupload_max_filesizeÃͤòĶ¤¨¤Æ¤¤¤ë
UPLOAD_ERR_FORM_SIZE2¥¢¥Ã¥×¥í¡¼¥É¥Õ¥¡¥¤¥ë¤ÎÍÆÎ̤¬HTML¥Õ¥¡¥¤¥ë¤Ç»ØÄꤷ¤¿max_file_sizeÃͤòĶ¤¨¤Æ¤¤¤ë<input type="hiddden" name="max_file_size" value="1000000">
UPLOAD_ERR_PARTIAL3¥¢¥Ã¥×¥í¡¼¥É¥Õ¥¡¥¤¥ë¤Î°ìÉô¤·¤«¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Ê¤¤
UPLOAD_ERR_NO_FILE4¥¢¥Ã¥×¥í¡¼¥É¼ºÇÔ

¥ê¥ó¥¯