Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上传文件并仅允许某些用户下载(LARAVEL)_Php_Laravel - Fatal编程技术网

Php 上传文件并仅允许某些用户下载(LARAVEL)

Php 上传文件并仅允许某些用户下载(LARAVEL),php,laravel,Php,Laravel,所以我需要用户上传文件。然后,非用户的客户可以购买这些文件。stripe处理付款后,买家应收到一封电子邮件,其中包含下载文件的链接。我需要的是这些文件的下载链接,该链接仅对上传文件的用户和购买文件的客户可用 这是我的控制器,用于处理用户填写的表单以上载文件。非用户是来自上一个表单的文件的详细信息 $note = new Note; $note->title = $nonUser->title; $note->description = $nonUser-

所以我需要用户上传文件。然后,非用户的客户可以购买这些文件。stripe处理付款后,买家应收到一封电子邮件,其中包含下载文件的链接。我需要的是这些文件的下载链接,该链接仅对上传文件的用户和购买文件的客户可用

这是我的控制器,用于处理用户填写的表单以上载文件。非用户是来自上一个表单的文件的详细信息

    $note = new Note;
    $note->title = $nonUser->title;
    $note->description = $nonUser->description;
    $note->mark = $nonUser->mark;
    $note->page_count = $nonUser->page_count;
    $note->subject_id = $nonUser->subject_id;
    $note->year = $nonUser->year;
    $note->modules = $nonUser->modules;
    $note->price = $nonUser->modules*25;
    $note->user_id = $user->id;
    $note->exam_id=$user->exam_id;
    $note->save();

     Storage::put(
    'notes/' . $note->id . '.pdf',
    file_get_contents($request->file('notes')->getRealPath())
    );
我的条纹处理

public function charge()
{
        $nid = Session::get('nid');
        $note = Note::where('id','=',$nid)->first();
        $price = $note->price;

        \Stripe\Stripe::setApiKey("sk_test_key");

        $token = $_POST['stripeToken'];

        try {
          $charge = \Stripe\Charge::create(array(
            "amount" => $price*100, // amount in cents, again
            "currency" => "aud",
            "source" => $token,
            "description" => "Example charge"
            ));
        } catch(\Stripe\Error\Card $e) {
            flashWarning('An error occured');
            return back();
        }

        flash('payment succesful! Check your email for a download link!');
        return back();
}

当付款成功处理后,将从数据库上传的文件的PK存储在一个新表中,我们称之为购买下载,并使用唯一的令牌在该表中查找文件的PK。这将是您随电子邮件一起发送的令牌,供他们下载文件


创建一个接受此令牌的新控制器,并在“购买的下载”表中查找唯一令牌,然后,如果令牌验证,您可以使用类似于X-Sendfile头的内容,让Web服务器从文件系统向客户端提供文件。如果愿意,您还可以在此令牌上设置过期时间。

我将使用以下步骤为买家提供服务:

付款成功后,在DB中存储orderID,fileID应与所有文件的主键匹配,存储在不同的表中,随机散列作为下载\u票证,日期时间作为票证到期,票证的使用次数作为下载\u计数

通过电子邮件向买家发送指向php脚本的下载链接。脚本应该包含下载票证。例如:

example.com/download.php?ticket=m54hm4j390534gi2frew0094

在脚本download.php中,您将执行以下操作:

从查询字符串中获取票证:$ticket=$\u GET['ticket'] 在数据库中获取记录:从tbl中选择*,其中票证=m54hm4j390534gi2frew0094 如果不匹配,则找不到错误404和http_响应_代码404并中止。 如果票证过期,删除记录,错误403禁止并中止。 如果下载计数超过限制,请删除记录,错误429请求过多并中止。 使用fileID列查找购买的文件 如果全部选中训练,则可以将文件发送给用户。不要将用户重定向到文件的真实位置。相反,请执行以下操作:

$path = '...'; // real path on disk
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($path));
$pipe = fopen($path, 'rb');
fpassthru($pipe); //sends file to user
fclose($pipe);

//TODO: increment `download_count` in the purchase record

您可以通过以下方式执行此操作

步骤1:生成链接

等你做完了

Storage::put(
    'notes/' . $note->id . '.pdf',
    file_get_contents($request->file('notes')->getRealPath())
    );
生成与的链接

第二步:发送电子邮件

向上载邮件的用户触发邮件

谁买了这个文件

附加步骤:确保文件安全

不要直接指向文件,你可以这样做

生成这样的链接 yourapp.com/pdf/12/143

其中12是用户id,143是pdf文件的id

在控制器下,您可以检查id为12的用户是否有权下载id为143的文件,如果有,则生成pdf视图或将其下载给用户


希望这对您有所帮助

您不能创建一个中间件吗?然后,当您通过电子邮件发送链接时,用户应在网站上进行身份验证。如果是,则下载开始;如果不是,则下载;如果他没有权限,则下载;如果没有权限,则下载;然后取消;确保文件不在公共文件夹中。OP表示客户不是用户,因此他们不能login@DanielPahor,没有一个答案对你有帮助吗?你没有选择一个,甚至没有投赞成票,也没有给任何一个留下反馈
$data['link'] = URL::to('/notes/'.$note->id.'pdf'); //Modify path according to your need
$message = 'Your Custom HTML';
   Mail::send(email.file, function ($message) {
    $message->from('youremail@example.com', 'File Received');
    $message->to(Auth::user()->email); // Email id of one who uploads it
});
   $userlist = User::where('file_id' , $fileid)->get(); //modify the condition according to the tables you have
   foreach($userlist as $users)
   {
Mail::send(email.file, function ($message) {
        $message->from('youremail@example.com', 'File Received');
        $message->to($user['email']); // Looped Email id
    });
   }