上傳檔案的網頁

引用 http://www.php5.idv.tw/html.php?mod=article&do=show&shid=6

<form enctype="multipart/form-data" action="__URL__" method="POST">

<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

接收參數陣列
檔案名稱
$_FILES['userfile']['name']

檔案格式(image/jpeg)
$_FILES['userfile']['type']

檔案大小
$_FILES['userfile']['size']

檔案暫存的位置(伺服器上暫存的位置,必須要移動到正確的位置)
$_FILES['userfile']['tmp_name']

錯誤訊息
$_FILES['userfile']['error']

語法例:


<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/'
;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'
]);

echo
'<pre>'
;
if (
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile
)) {
echo
"File is valid, and was successfully uploaded.\\\\n"
;
} else {
echo
"Possible file upload attack!\\\\n"
;
}

echo
'Here is some more debugging info:'
;
print_r($_FILES
);

print
"</pre>"
;

?>

多檔同時傳送陣列實例

<form action="" method="post" enctype="multipart/form-data">
<p>Pictures: <input type="file" name="pictures[]" />
<input type="file" name="pictures[]" /> <input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
<?php
foreach ($_FILES["pictures"]["error"] as $key => $error
) {
if (
$error == UPLOAD_ERR_OK
) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key
];
$name = $_FILES["pictures"]["name"][$key
];
move_uploaded_file($tmp_name, "data/$name"
);
}
}
?>
文章標籤
全站熱搜
創作者介紹
創作者 sean 的頭像
sean

My Source Code

sean 發表在 痞客邦 留言(0) 人氣(50)