Home > PHP Framework > Laravel > laravel HTTP request: get cookies and uploaded files

laravel HTTP request: get cookies and uploaded files

齐天大圣
Release: 2020-12-15 09:28:14
Original
1895 people have browsed it

COOKIE

Laravel will encrypt the cookie value at the bottom layer and use the authorization code for signature. Therefore, if the client modifies the cookie value, it will become invalid. This way, we don't have to worry about cookie forgery.

Setting the cookie value

Setting the cookie value will use the knowledge of laravel response (Response), which is briefly mentioned here.

You can use the cookie function to create a cookie, and then return the cookie to the client through the response function:

// cookie($name, $value, $minutes, $path, $domain, false, ...);
$cookie = cookie('username', 'php.cn', 3600);
return response('hello laravel')->cookie($cookie);
Copy after login

In addition to using the cookie function, you can also use the cookie method of the response instance to set and return . The parameters of this method and cookie function are consistent.

return response('Hello World')->cookie(
    'name', 'php.cn', 3600
);
Copy after login

Get the cookie value

There are two ways to get the cookie value. These two methods are demonstrated below:

use Illuminate\Http\Request;
$value = $request->cookie('name');
// 或
use Illuminate\Support\Facades\Cookie;
$value = Cookie::get('name');
Copy after login

Get uploaded files

The laravel framework is very simple to get uploaded files and save files.

There are two ways to get the uploaded file, use the file method or the dynamic attribute method. This method returns an Illuminate\Http\UploadedFile instance.

$logo = $request->file('logo');
// or
$logo = $request->logo;
Copy after login

Of course, you can determine whether the file exists before getting it. Here, use the hasFile method:

if ($request->hasFile('logo')) {
    $logo = $request->file('logo');
}
Copy after login

The request request instance can also verify the uploaded file. We highly recommend that you verify the uploaded file before saving it. Do this:

if ($request->file('logo')->isValid()) {
    //
}
Copy after login

The uploadFile instance has many methods to obtain the temporary saving path, extension and other information of the uploaded file. The following demonstrates how to obtain the file type suffix

$logo = $request->file('logo');
$logo->path();  // png
Copy after login

Finally, how to save the file. Save the file using the store method. This method has two parameters. The first parameter fills in the path to save the file, and the second parameter fills in the file. In which home directory (or third-party platform) it is saved. Laravel will automatically generate a unique ID as the file name. This information is in the configuration file config/filesystems.php, as follows:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],
],
Copy after login

If you want to set the file name for saving the price request file yourself, you need to use the storeAs method. The second parameter of this method is the file to be set. name.

$request->file('logo')->storeAs('img/logo', '1.png');
Copy after login

Finally, post a complete file upload code example:

html code is as follows:

<form method="post" action="/index?a=32" enctype="multipart/form-data">
    @csrf
    <input type="file" name="logo" >
    <input type="submit" value="sub" />
</form>
Copy after login

save file code as follows:

if ($request->hasFile(&#39;logo&#39;)) {
    $logo = $request->file(&#39;logo&#39;);
    if ($logo->isValid()) {
        $ext = $logo->extension();
        $fileName = date(&#39;YmdHis&#39;) . mt_rand(10000,99999);
        $path = $logo->storeAs(&#39;img/logo&#39;, $fileName . &#39;.&#39; . $ext);
        dump($path); //"img/logo/2020121413351718218.png"
    }
}
Copy after login

The above is the detailed content of laravel HTTP request: get cookies and uploaded files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template