php文件系统

php 文件上传和下载

php 不能识别中文名的文件可以使用mb_convert_encoding或iconv转码文件名

    $file = base_path() . "\\public\\word\\新建1.docx";
        header("Content-Type:application/octet-stream");
        //告诉浏览器文档类型(mime类型); octet-stream指的是二进制文件类型;下载任何类型的文件都可以这么指定
        header("Content-Disposition:attachment;filename=" . "newname.docx");//告诉浏览器以附件方式对待文件(即下载文件);并设置下载后的文件名
        header("Accept-ranges:bytes");//告诉浏览器文件大小的单位

        $content = file_get_contents(mb_convert_encoding($file, 'gb2312', 'utf-8'));
        // $content = file_get_contents(iconv( 'utf-8','gb2312',$file));
        header("Accept-Length:" . sizeof($content));
        echo $content;
        exit;

PHP通过发送header头实现文件下载
(https://blog.csdn.net/u012732259/article/details/41750269)

   $file = base_path() . "\\public\\word\\1.docx";
        header("Content-Type:application/octet-stream");
        //告诉浏览器文档类型(mime类型); octet-stream指的是二进制文件类型;下载任何类型的文件都可以这么指定
        header("Content-Disposition:attachment;filename=" . "newname.docx");//告诉浏览器以附件方式对待文件(即下载文件);并设置下载后的文件名
        header("Accept-ranges:bytes");//告诉浏览器文件大小的单位

        $content = file_get_contents($file);
        header("Accept-Length:" . sizeof($content));
        echo $content;
        exit;

利用laravel Response 实现下载
return response()->download( p a t h T o F i l e ) ; r e t u r n r e s p o n s e ( ) − > d o w n l o a d ( pathToFile); return response()->download( pathToFile);returnresponse()>download(pathToFile, $name, $headers);

// 这个方法只能一次下载一个文件
$headers = array(
            'Content-Type: application/docx',
        );
        $option = $request->get("option");
            $file= base_path()."\\public\\word\\1.docx";   
        return Response::download($file, '1.docx', $headers);
 $headers = array(
            'Content-Type: application/docx',
        );
        $file= base_path()."\\public\\word\\1.docx";
        $content= file_get_contents($file);
        return response($content)
            ->header('Content-Type', "application/docx")
            ->header("Content-Disposition","attachment;filename=test.docx");//重新设置文件名

文件压缩和解压 官方

文件压缩和下载

目录操作

清除目录及目录下文件

 static function del_dir($dir)
    {
      if(!is_dir($dir)){
          return;
        }
        $dh = opendir($dir);

        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . DIRECTORY_SEPARATOR . $file;
                if (is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    self::del_dir($fullpath);
                }
            }
        }
        closedir($dh);

        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }

laravel 常用项目路径

app_path()

app_path函数返回app目录的绝对路径:
$path = app_path();

你还可以使用app_path函数为相对于app目录的给定文件生成绝对路径:
$path = app_path('Http/Controllers/Controller.php');

base_path()

base_path函数返回项目根目录的绝对路径:
$path = base_path();

你还可以使用base_path函数为相对于应用目录的给定文件生成绝对路径:
$path = base_path('vendor/bin');

config_path()

config_path函数返回应用配置目录的绝对路径:
$path = config_path();

database_path()

database_path函数返回应用数据库目录的绝对路径:
$path = database_path();

public_path()

public_path函数返回public目录的绝对路径:
$path = public_path();

storage_path()

storage_path函数返回storage目录的绝对路径:
$path = storage_path();

还可以使用storage_path函数生成相对于storage目录的给定文件的绝对路径:
$path = storage_path('app/file.txt');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值