html中的代码:
<form action="file.php" method="post"enctype="multipart/form-data" target="demo">
<label for="file">
</label><input type="file" name="file" id="file"/> <br />
<input type="submit" name="name" value="上传" />
</form>
<iframe height="300px" width="300px" frameborder=0 marginheight="10px" marginwidth="10px" name="demo">
</iframe>
file.php中的代码:
//请求接收
if(isset($_FILES["file"])){
$F=$_FILES["file"];
$File->upfile($path,$F);
}
//文件保存主函数
class File{
public function upfile($path,$file){
$type=$file["type"];
$size=$file["size"];
$name=$file["name"];
$tmp_name=$file["tmp_name"];
if (($type=="image/gif"||$type=="image/jpeg"||$type=="image/pjpeg")&&($size<200000)){
if ($file["error"] > 0){
echo "<b>返回代码:".$file["error"]."<b><br />";
}else{
//判断文件是否已经存在
if (file_exists($path.$name)){
echo $name."<b>经存在<b>";
}else{
//保存在服务器指的路径$path.$name
move_uploaded_file($tmp_name,$path.$name);
}
}
}
else{
echo "<b>无效的文件</b>";
}
}
}