本文主要是介绍php文件的上传下载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上传的前端:
view.php
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<div class="register">
<form enctype="multipart/form-data" method="post" action="FileUpload.php" name="myform">
<table>
<tr><td align="center" colspan="2"><font style="font-size: 40px;font-family:华文彩云;">文件上传</font></td></tr><br/>
<tr><td>请填写用户名:</td><td><input type="text" name="username"></td></tr>
<tr><td>经简单介绍该文件:</td><td><textarea name="fileintro" rows="10" cols="80"></textarea></td></tr>
<tr><td>请选择你要上传的文件:</td><td><input type="file" name="myfile"></td></tr>
<tr><td><input type="submit" value="上传文件"></td><td></td></tr>
</table>
</form>
</div>
</html>
后台上传代码:
<?php
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];
echo "<pre>";
echo print_r($_FILES);
echo "</pre>";
$file_size=$_FILES['myfile']['size'];
if ($file_size>2*1024*1024){
echo "上传的文件不能超过2M";
return;
}
if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
$upload_file=$_FILES['myfile']['tmp_name'];
$move_to_file=$_SERVER['DOCUMENT_ROOT']."/file/up/".$_FILES['myfile']['name'];
$move_to_file=iconv("utf-8", "gb2312",$move_to_file);
if(move_uploaded_file($upload_file, $move_to_file)){
echo $_FILES['myfile']['tmp_name']."上传成功";
}else{
echo "上传失败";
}
}
?>
文件下载代码:
<?php
$file_name="HDFS.txt";
$file_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$file_name;
if(!file_exists($file_path)){
echo "文件不存在";
return ;
}
$fp=fopen($file_path,"r");
$file_size=filesize($file_path);
header("conten-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Accept-length:$file_size");
header("content-Disposition:attachment;file=".$file_path);
$buffer=1024;
while (!feof($fp)){
$file_data=fread($fp, $buffer);
echo $file_data;
}
fclose($fp);
?>
这篇关于php文件的上传下载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!