這是一個PHP的文件上傳類帶圖片顯示的.其實做成函數就可以了.不過還是做成類好玩一點.~~~~
本來應該用JS來驗證上傳文件類型的.但懶得做了.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>
<body>
<form id="form1" name="upload" enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE " />
<input type="file" name="file" />
<input type="submit" name="Submit" value="提交" />
</form>
</body>
</html>
<?php
Class upload
{
var $upload_name;
var $upload_tmp_address;
var $upload_server_name;
var $upload_filetype ;
var $file_type;
var $file_server_address;
var $image_w=900; //要顯示圖片的寬
var $image_h=350; //要顯示圖片的高
var $upload_file_size;
var $upload_must_size= 50000; //允許上傳文件的大小,自己設置
function upload_file()
{
$this->upload_name = $_FILES["file"]["name"]; //取得上傳文件名
$this->upload_filetype = $_FILES["file"]["type"];
$this->upload_server_name = date("Y_m_dH_i_s").$this->upload_name;
$this->upload_tmp_address = $_FILES["file"]["tmp_name"]; //取得臨時地址
$this->file_type = array("image/gif","image/pjpeg"); //允許上傳文件的類型
$this->upload_file_size = $_FILES["file"]["size"]; //上傳文件的大小
if(in_array($this->upload_filetype,$this->file_type))
{ if($this->upload_file_size < $this->upload_must_size)
{
echo("上傳成功,謝謝支持");
$this->file_server_address = "D:\usr\www\html\upload/".$this->upload_server_name;
move_uploaded_file($this->upload_tmp_address,$this->file_server_address);//從TEMP目錄移出
echo("<img src=$this->file_server_address width=$this->image_w height=$this->image_h/>"); //顯示圖片
}
else
{
echo("文件容量太大");
}
}
else
{
echo("不支持此文件類型,請重新選擇");
}
}
}
$dd = new upload;
$dd->upload_file();
?>