php文件操作

文件写入

$date = date('Ymd');
$myfile = fopen($date."register.txt", "a+") or die("Unable to open file!");
$txt = $phone." ".$invite_code." ".$j."\r\n";
fwrite($myfile, $txt);
fclose($myfile);

文件读取

$date = date('Ymd');
$myfile = fopen($date."register.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize($date."register.txt"));
fclose($myfile);

下载文件

//二维码下载
public function download_qrcode_img(){
    //接收并检测文件
    $img = $this->input->get('img');
    if(!file_exists($img)){ echo '文件不存在';die; }

    //输出文件
    Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: ".filesize($img));
    Header("Content-Disposition: attachment; filename=" . basename($img));

    //清楚多余输出
    ob_start();
    ob_end_flush();
    ob_end_clean();

    readfile($img);exit;
}

多文件打包下载

//多文件打包下载
function download_zip($needDown=array(), $filename=''){
    if(!$filename){
        $filename = "./uploads/cps/download_qrcode_zip/" . date ( 'YmdHis' ) . ".zip"; // 最终生成的文件名(含路径)
    }

    // 生成文件
    $zip = new ZipArchive (); // 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
    if ($zip->open ( $filename, ZIPARCHIVE::CREATE ) !== TRUE) {
        exit ( '无法打开文件,或者文件创建失败' );
    }

    //$fileNameArr 就是一个存储文件路径的数组 比如 array('/a/1.jpg,/a/2.jpg....');

    foreach ( $needDown as $val ) {
        $zip->addFile ( $val, basename ( $val ) ); // 第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下
    }
    $zip->close (); // 关闭

    //下面是输出下载;
    header ( "Cache-Control: max-age=0" );
    header ( "Content-Description: File Transfer" );
    header ( 'Content-disposition: attachment; filename=' . basename ( $filename ) ); // 文件名
    header ( "Content-Type: application/zip" ); // zip格式的
    header ( "Content-Transfer-Encoding: binary" ); // 告诉浏览器,这是二进制文件
    header ( 'Content-Length: ' . filesize ( $filename ) ); // 告诉浏览器,文件大小
    @readfile ( $filename );//输出文件;
    exit();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值