php上传文件和下载文件

单文件上传

<!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=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<!-- 判断变量$_files是否为空 -->
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

</body>
</html>

下载文件

<!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=GBK" />
<title>Insert title here</title>
</head>
<body>
<a href="0gwc.txt">通过超链接0gwc.txt</a>
<br />
<a href="1.jpg">下载1.jpg</a>
<br />
<a href="doDownload1.php?filename=D:/WWW/0gwc.txt">通过程序下载1.jpg</a>
<br />
<a href="doDownload1.php?filename=../upload/nv.jpg">下载nv.jpg</a>
<?php

?>
</body>
</html>
<?php 
$filename=$_GET['filename'];
echo $filename;
if($filename!=""){
header('content-disposition:attachment;filename='.basename($filename));
header('content-length:'.filesize($filename));
readfile($filename);
}
?>

发布了70 篇原创文章 · 获赞 1 · 访问量 491

猜你喜欢

转载自blog.csdn.net/zhupengqq1/article/details/103943447