【问题标题】:how to add a user id to each file for downloading in laravel 5.2?如何在 laravel 5.2 中为每个文件添加用户 ID 以供下载?
【发布时间】:2017-03-27 05:08:21
【问题描述】:

我有两个表''users''和''announcement'',哪个公告表有3个下载文件,所以我想在用户下载文件时添加一个功能,将用户ID添加到下载文件中?

公告表

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAnnoucementsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('announcements', function (Blueprint $table) {
            $table->increments('project_id')->unsigned();;
            $table->integer('types_id')->unsigned();
            $table->foreign('types_id')->references('id')->on('types')->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->string('project_name');
            $table->string('extension');
            $table->date('deadline');
            $table->string('biddingdocuments');
            $table->string('amendmentdocuments');
            $table->string('noticestenders');
            $table->timestamps();
        });

    }
/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('announcements');
}
}


Users Table 
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('securityq');
            $table->string('securitya');
            $table->string('name');
            $table->string('fname');
            $table->string('lname');
            $table->string('gender');

            $table->string('cname');
            $table->integer('lnumber')->unique();
            $table->string('province');
            $table->string('city');
            $table->string('pnumber');

            $table->text('address');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}
userAnnoucement controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

use App\Http\Requests;
use App\Announcements;



class UserAnnoucment extends Controller
{
     public function index(Request $request)
    {

         $announcements = Announcements::orderBy('project_id','DESC')->paginate(5);
        return view('userAnnoucements.index',compact('announcements'))->with('i',

($request->input('page', 1) - 1) * 5); }

     /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

     public function count(){
        $announcements = new Announcements;
          $announcements->extension = Auth::user()->id;
          $announcements->save();  
      return  $announcements;

     }



}
``

【问题讨论】:

  • 请正确格式化您的代码。我懒得看那个。

标签: javascript php ajax laravel-5.2


【解决方案1】:

如果网站不是太忙也没问题。否则,如果同时下载,您可能会发生冲突。

如果几乎​​没有流量,以编程方式将下载文件复制到一个临时文件夹,将用户 ID 添加到文件名,然后从那里下载,即:1024_file.pdf。如果用户中断下载或返回,则文件已经存在,尽管您可能希望在每次下载后清除文件以节省磁盘空间,并且每次都使用相同的下载代码。

在繁忙的服务中,为每个登录的用户创建一个文件夹(如果尚不可用)并在这些文件夹中开展业务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-08
    • 2016-08-07
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2016-11-07
    • 2017-04-25
    • 2019-03-07
    相关资源
    最近更新 更多