首页 > 编程技术 > php

php文件下载代码(支持远程文件下载)

发布时间:2016-11-25 16:27

本文章提供的三款文件下载代码有二款是支持本地服务器文件下载的,另一款支持下载远程服务器上的文件下载到本地哦。
 代码如下 复制代码

function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
    $file_dir = chop($file_dir);//去掉路径中多余的空格
    //得出要下载的文件的路径
    if($file_dir != '')
    {
        $file_path = $file_dir;
        if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
            $file_path .= '/';
        $file_path .= $file_name;
    }           
    else
        $file_path = $file_name;   
   
    //判断要下载的文件是否存在
    if(!file_exists($file_path))
    {
        echo '对不起,你要下载的文件不存在。';
        return false;
    }

    $file_size = filesize($file_path);
 
    header("content-type: application/octet-stream");
    header("accept-ranges: bytes");
    header("accept-length: $file_size");
    header("content-disposition: attachment; filename=".$file_name);
   
    $fp = fopen($file_path,"r");
    $buffer_size = 1024;
    $cur_pos = 0;
   
    while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
    {
        $buffer = fread($fp,$buffer_size);
        echo $buffer;
        $cur_pos += $buffer_size;
    }
   
    $buffer = fread($fp,$file_size-$cur_pos);
    echo $buffer;
    fclose($fp);
    return true;

}

?>

<?php 
   

 代码如下 复制代码
$file_name = "info_check.exe";
    $file_dir = "/public/www/download/";
    if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
    echo "文件找不到";
    exit;
    } else {
    $file = fopen($file_dir . $file_name,"r"); // 打开文件
    // 输入文件标签
    header("content-type: application/octet-stream");
    header("accept-ranges: bytes");
    header("accept-length: ".filesize($file_dir . $file_name));
    header("content-disposition: attachment; filename=" . $file_name);
    // 输出文件内容
    echo fread($file,filesize($file_dir . $file_name));
    fclose($file);
    exit;}

?>


<?
// 如果文件路径是http和ftp,下载代码如下:

   

 代码如下 复制代码

$file_name = "info_check.exe";
    $file_dir = " http://www.111cn.net/"; 
    $file = @ fopen($file_dir . $file_name,"r");
    if (!$file) {
    echo "文件找不到";
    } else {
    header("content-type: application/octet-stream");
    header("content-disposition: attachment; filename=" . $file_name);
    while (!feof ($file)) {
    echo fread($file,50000);
    }
    fclose ($file);
    }

    ?>

这二款文件上传程序是非常简单的,很适合初学者的学习php时的文件上传实例教程哦。
 代码如下 复制代码

if(!$uploadaction):
?>

<html>
<head>
<title>文件上载界面</title>
</head>
<body>

<table>
<tr align="center">
<td><form enctype="multipart/form-data" name="submitform" action="upload.php" method="post">
<input type="hidden" name="max_file_size" value="1000000"><input type="hidden" name="uploadaction" value="1">
</td></tr>
<tr align="center">
<td><input name="uploadfile" type="file" size="30"></td>
</tr>
<tr align="center">
<td><input name="submit" value="提交" type="submit"></td>
<td><input name="reset" value="重置" type="reset"></td>
</tr>
</form>
</table>
</center>
</body>
</html>

<?
else:
?>

<html>
<head>
<title>文件上载代码</title>
</head>
<body>

<?
$uploadaction=0;
echo "good!";
$timelimit=60; /*设置超时限制时间 缺省时间为30秒 设置为0时为不限时 */
set_time_limit($timelimit);

if(($uploadfile !="none" ))
{  
    $uploadpath=addslashes(dirname($path_translated))."\upload\"; //上载文件存放路径

    $filename=$uploadpath.$uploadfile_name; //上载文件名

    if($uploadfile_size < 1024) //上载文件大小
    {
  $filesize=(string)$uploadfile_size." 字节"; 
 }
     elseif ($uploadfile_size <(1024 * 1024))
     {
  $filesize=number_format((double)($uploadfile_size/1024), 1) . " kb";
     }
  else{
  $filesize=number_format((double)($uploadfile_size / (1024 * 1024)), 1) . " mb";
     }
   
    if(!file_exists($filename))
    {
         if(copy($uploadfile,$filename))
         {  echo "文件 $uploadfile_name ($filesize)上载成功!"; }
         else
         { echo "文件 $uploadfile_name上载失败!"; }

         unlink($uploadfile);
    }
    else
    { echo "文件 $uploadfile_name已经存在!"; }
}
else
{ echo "你没有选择任何文件上载!"; }

set_time_limit(30); //恢复缺省超时设置

?>

<br><a href = "upload.php">返回</a>

</body>
</html>

<?
endif;
?>

方法二

<html>
<head>
<title>文件上传</title>
</head>
<body>
<table>
<form enctype="multipart/form-data" name=myform method="post">
<tr><td>文件上传</td><td><input name="myfile" type="file"></td></tr>
<tr><td colspan="2"><input name="submit" value="上传" type="submit">
</form>
<?php
if(isset($submit)){
 if($myfile != "none") {
  $upload_dir = "c:winnt emp";
  $up=copy($myfile,$upload_dir);
  if($up==1) {
   print("文件上传成功!");
  }
  else {
   print ("文件上传失败.");
  }
  unlink ($myfile);
 }
 else {
  print("你没有上传任何文件");   
 }
}
?>
</td></tr>
</table>
</body>
</html>

本程序分为简单的单文件重名,加上getfile就可以实现文件批量重命名了,$path为要你重命名的目录,它可以把指定目录下所指定文件类型的议论后次批量重命名哦。非常好用。

*/

 代码如下 复制代码

$format ='php';
$path ='www.111cn.net/';
$files = getfile($path,$format);

foreach( $files as  $v )
{
 $tv = basename($v);
 list($name,$ext) = explode('.',$tv);
 $newname =$path.time().mt_rand(1,10000).'.'.$ext;
 if(rename($v,$newname))
 {
  echo '成功将'.$v.'重命名'.$newname.'<br />'; 
 }
}

function getfile($path,$format)
{
 $dirs    = array();
 foreach(glob("$path*") as $d)
 {
  $tmp = explode('.',$d);
  $k = end($tmp);
  if(is_file($d) && ($k ==strtolower($format) ))
  {
   $dirs[]    = $d;
  }
 }
 return $dirs;
}

/*
成功将rename/12819320034756.php重命名rename/12819402065296.php
成功将rename/12819320036890.php重命名rename/12819402067216.php
成功将rename/12819320037133.php重命名rename/12819402067656.php

本站原创教程,坟载注明来源http://www.111cn.net/phper/php.html
*/
?>

这里又提供一款php购物车实例代码,这是一款适合种位要开发商城或购物系统参考的开发实例了,告诉你如果增加商品到购物并删除,与购物车的数据库设计实例。

inventory表

 代码如下 复制代码

create table inventory (
  product tinytext not null,
  quantity tinytext not null,
  id int(4) default '0' not null auto_increment,
  description tinytext not null,
  price float(10,2) default '0.00' not null,
  category char(1) default '' not null,
  key id (id),
  primary key (id),
  key price (price)
);
insert into inventory values ('硬盘','5','1','80g','5600','1');
insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
insert into inventory values ('dvd-rom','7','3','12x','2000','1');
insert into inventory values ('主板','3','4','asus','5000','2');
insert into inventory values ('显示卡','6','5','64m','4500','1');
insert into inventory values ('刻录机','4','6','52w','3000','1');

shoping表

create table shopping (
  session tinytext not null,
  product tinytext not null,
  quantity tinytext not null,
  card tinytext not null,
  id int(4) default '0' not null auto_increment,
  key id (id),
  primary key (id)
);

shoper表

create database shopper;
use shopper;
create table shopping (
  session tinytext not null,
  product tinytext not null,
  quantity tinytext not null,
  card tinytext not null,
  id int(4) default '0' not null auto_increment,
  key id (id),
  primary key (id)
);
create table inventory (
  product tinytext not null,
  quantity tinytext not null,
  id int(4) default '0' not null auto_increment,
  description tinytext not null,
  price float(10,2) default '0.00' not null,
  category char(1) default '' not null,
  key id (id),
  primary key (id),
  key price (price)
);
insert into inventory values ('硬盘','5','1','80g','5600','1');
insert into inventory values ('cpu','12','2','p4-2.4g','6600','1');
insert into inventory values ('dvd-rom','7','3','12x','2000','1');
insert into inventory values ('主板','3','4','asus','5000','2');
insert into inventory values ('显示卡','6','5','64m','4500','1');
insert into inventory values ('刻录机','4','6','52w','3000','1');
*/

 

 代码如下 复制代码

<!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> php教程文件上传代码</title>
</head>

<body>

<form method="post" enctype="multipart/form-data" action ="uploads.php">
<input type = "file" name="uploadedfile" size="30">
<input type = "hidden" name = "max_file_size" value="100000">
<input type = "submit" value = "上传文件">
</form>

</body>
</html>

<?php

 代码如下 复制代码
if ($uploadedfile<>"none") {
 if (!copy($uploadedfile, "$uploadedfile_name")) {
 echo "<font face='arial' size='2'> $name 文件上传失败 ,<br>";
 echo "也可能是文件太大<br>";
 echo "请使用 back 按键再试一次";
 } else {
 echo "<font face='arial' size='2'>文件上传成功 !<br>";
 echo "文件类型:$uploadedfile_type <br>";
 echo "文件大小:$uploadedfile_size <br>";
 echo "文件名称:$uploadedfile_name <br>";
 echo "文件说明:$description <br>";
 }
}
/*
这是一款很实例的 php文件上传代码   程序,拿着就能用的。
*/

?>

标签:[!--infotagslink--]

您可能感兴趣的文章: