小弟這有一支上傳程式,自行修改試試看吧~
還有顯示錯誤訊息的功能
複製程式
<?php
define("UPLOAD_DIR", "./");
// 用來顯示錯誤訊息
function error($str)
{
print "<font color=\"#ff0000\">{$str}</font>\n";
exit();
}
header("Content-Type: text/html; charset=big5");
print <<<_EOT_
<form method="POST" action="301.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="102400">
<input type="file" name="file">
<input type="submit" value="傳送">
<input type="hidden" name="mode" value="upload">
</form>
_EOT_;
if($_POST["mode"] == "upload")
{
// 錯誤檢查
switch ($_FILES["file"]["error"])
{
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
error("超過檔案大小限制。");
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
error("無法上傳。");
}
// 為求慎重,也檢查一次檔案的大小
if($_FILES["file"]["size"] > 1024 * 100)
{
error("超過檔案大小限制。");
}
///////////////////
//關閉阻擋功能//
//////////////////
// // 拒絕png與jpeg之外的檔案
// if(!preg_match("/^image\/.*(png|jpeg)$/i", $_FILES["file"]["type"]))
// {
// error("檔案形式不合法。");
// }
// 拒絕使用了特定字元以外之文字的檔名
if(preg_match("/[^\w\d\-\.]/", $_FILES["file"]["name"]))
{
error("檔名不合法。");
}
// 移動檔案
$upload_file = UPLOAD_DIR . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $upload_file))
{
chmod($upload_file, 0644);
$img_size = getimagesize($upload_file);
print "以下的檔案已經上傳成功。<br>\n";
print '<img src="' . $upload_file . '" ';
print $img_size[3] . ' border="0">';
}
else
{
error("上傳失敗。");
}
}
?>