文件写入
$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();
}