{"id":22133,"date":"2020-08-05T17:43:09","date_gmt":"2020-08-05T17:43:09","guid":{"rendered":"http:\/\/laravel_file_upload_to_a_local_server_or_to_the_cloud"},"modified":"2024-06-04T15:56:57","modified_gmt":"2024-06-04T22:56:57","slug":"laravel_file_upload_to_a_local_server_or_to_the_cloud","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud","title":{"rendered":"Laravel File Upload to a Local Server Or to the Cloud"},"content":{"rendered":"<div class=\"wp-block-cloudinary-markdown \"><p>Currently, Laravel is the most renowned PHP framework, boasting of a large developer community; several open-source packages, such as Cashier, Sanctum, Scout, and Telescope; and a host of paid platforms, e.g., Laravel Forge, Envoyer, and Vapor. Laravel Forge &amp; Envoyer ably supports deployment and use of Laravel production-based apps.<\/p>\n<hr \/>\n<p>Introducing <a href=\"https:\/\/cloudinary.com\/blog\/introducing_cloudinary_s_new_laravel_sdk\">Cloudinary\u2019s Laravel SDK<\/a>. Learn the benefits and capabilities of the Laravel PHP framework and the way to upload to and transform files.<\/p>\n<hr \/>\n<p>The sections below walk you through the process of setting up Laravel <a href=\"https:\/\/cloudinary.com\/blog\/automating_file_upload_and_sharing\">file uploads<\/a>.<\/p>\n<h2>Set Up a Laravel Project<\/h2>\n<ol>\n<li>\n<p>Install <a href=\"https:\/\/getcomposer.org\/\">Composer<\/a> and <a href=\"https:\/\/www.php.net\/manual\/en\/install.php\">PHP<\/a> on your development or production machine and then run this command:<\/p>\n<pre class=\"js-syntax-highlighted\"><span><code class=\"hljs shcb-wrap-lines\">composer create-project --prefer-dist laravel\/laravel upload\n<\/code><\/span><\/pre>\n<\/li>\n<li>\n<p>Go to the <code>upload<\/code> directory and rename the <code>env.example<\/code> file to <code>.env<\/code>.<\/p>\n<\/li>\n<li>\n<p>Run the project with the command <code>php artisan serve<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p>Your Laravel project is now up and running.<\/p>\n<h2>Set Up the Mechanics for File Uploads<\/h2>\n<ol>\n<li>\n<p>Create a file-upload controller (<code>FileUpload Controller<\/code>) in your project:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">php<\/span> <span class=\"hljs-selector-tag\">artisan<\/span> <span class=\"hljs-selector-tag\">make<\/span><span class=\"hljs-selector-pseudo\">:controller<\/span> <span class=\"hljs-selector-tag\">FileUploadController<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<\/li>\n<li>\n<p>Open the FileUploadController.php file and add a method for displaying the upload form:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Controllers<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Request<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">FileUploadController<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Controller<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">showUploadForm<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> view(<span class=\"hljs-string\">'upload'<\/span>);\n    }\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<\/li>\n<li>\n<p>Create an <code>upload.blade.php<\/code> file in the <code>resources\/views<\/code> directory and populate the file with the code below:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"utf-8\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"width=device-width, initial-scale=1\"<\/span>&gt;<\/span>\n\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>Laravel File Upload<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n\n        <span class=\"hljs-comment\">&lt;!-- Fonts --&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/fonts.googleapis.com\/css?family=Nunito:200,600\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span>&gt;<\/span>\n\n        <span class=\"hljs-comment\">&lt;!-- Styles --&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span>&gt;<\/span><span class=\"css\">\n            <span class=\"hljs-selector-tag\">html<\/span>, <span class=\"hljs-selector-tag\">body<\/span> {\n                <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#fff<\/span>;\n                <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#636b6f<\/span>;\n                <span class=\"hljs-attribute\">font-family<\/span>: <span class=\"hljs-string\">'Nunito'<\/span>, sans-serif;\n                <span class=\"hljs-attribute\">font-weight<\/span>: <span class=\"hljs-number\">200<\/span>;\n                <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100vh<\/span>;\n                <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\n            }\n\n            <span class=\"hljs-selector-class\">.full-height<\/span> {\n                <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100vh<\/span>;\n            }\n\n            <span class=\"hljs-selector-class\">.flex-center<\/span> {\n                <span class=\"hljs-attribute\">align-items<\/span>: center;\n                <span class=\"hljs-attribute\">display<\/span>: flex;\n                <span class=\"hljs-attribute\">justify-content<\/span>: center;\n            }\n\n            <span class=\"hljs-selector-class\">.position-ref<\/span> {\n                <span class=\"hljs-attribute\">position<\/span>: relative;\n            }\n\n            <span class=\"hljs-selector-class\">.top-right<\/span> {\n                <span class=\"hljs-attribute\">position<\/span>: absolute;\n                <span class=\"hljs-attribute\">right<\/span>: <span class=\"hljs-number\">10px<\/span>;\n                <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">18px<\/span>;\n            }\n\n            <span class=\"hljs-selector-class\">.content<\/span> {\n                <span class=\"hljs-attribute\">text-align<\/span>: center;\n            }\n\n            <span class=\"hljs-selector-class\">.title<\/span> {\n                <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">84px<\/span>;\n            }\n\n            <span class=\"hljs-selector-class\">.links<\/span> &gt; <span class=\"hljs-selector-tag\">a<\/span> {\n                <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#636b6f<\/span>;\n                <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">25px<\/span>;\n                <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">13px<\/span>;\n                <span class=\"hljs-attribute\">font-weight<\/span>: <span class=\"hljs-number\">600<\/span>;\n                <span class=\"hljs-attribute\">letter-spacing<\/span>: .<span class=\"hljs-number\">1rem<\/span>;\n                <span class=\"hljs-attribute\">text-decoration<\/span>: none;\n                <span class=\"hljs-attribute\">text-transform<\/span>: uppercase;\n            }\n\n            <span class=\"hljs-selector-class\">.m-b-md<\/span> {\n                <span class=\"hljs-attribute\">margin-bottom<\/span>: <span class=\"hljs-number\">30px<\/span>;\n            }\n        <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"flex-center position-ref full-height\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"content\"<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"title m-b-md\"<\/span>&gt;<\/span>\n                    Laravel File Upload\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\n                @if ($message = Session::get('success'))\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"alert alert-success alert-block\"<\/span>&gt;<\/span>\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"button\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"close\"<\/span> <span class=\"hljs-attr\">data-dismiss<\/span>=<span class=\"hljs-string\">\"alert\"<\/span>&gt;<\/span>\u00d7<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n                            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">strong<\/span>&gt;<\/span>{{ $message }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">strong<\/span>&gt;<\/span>\n                    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n                @endif\n\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"links\"<\/span>&gt;<\/span>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">form<\/span> <span class=\"hljs-attr\">action<\/span>=<span class=\"hljs-string\">\"\/upload\"<\/span> <span class=\"hljs-attr\">method<\/span>=<span class=\"hljs-string\">\"POST\"<\/span> <span class=\"hljs-attr\">enctype<\/span>=<span class=\"hljs-string\">\"multipart\/form-data\"<\/span>&gt;<\/span>\n                        @csrf\n                        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"row\"<\/span>&gt;<\/span>\n\n                            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"col-md-6\"<\/span>&gt;<\/span>\n                                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"file\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"file\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-control\"<\/span>&gt;<\/span>\n                            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\n                            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"col-md-6\"<\/span>&gt;<\/span>\n                                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"submit\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"btn btn-success\"<\/span>&gt;<\/span>Upload a File<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n                            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\n                        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n                    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">form<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>The code above displays the form along with a confirmation message if the upload succeeded. Concurrently, the file-upload controller posts the form data to a <code>\/upload<\/code> route in the <code>routes\/web.php<\/code> file. <strong>Note:<\/strong> The related code will be shown later in this post.<\/p>\n<\/li>\n<li>\n<p>Go to the <code>routes\/web.php<\/code> directory and add two routes: one to display the form and the other to process the file upload:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">Route::get(<span class=\"hljs-string\">'\/upload'<\/span>, <span class=\"hljs-string\">'FileUploadController@showUploadForm'<\/span>);\nRoute::post(<span class=\"hljs-string\">'\/upload'<\/span>, <span class=\"hljs-string\">'FileUploadController@storeUploads'<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<\/li>\n<\/ol>\n<p>Now reload the app and go to the <code>\/upload<\/code> route. This page is displayed:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/image\/upload\/w_700,c_fill,f_auto,q_auto,dpr_2.0\/Web_Assets\/blog\/laravel_file_upload.png\" alt=\"laravel file upload\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"1400\" height=\"890\"\/><\/p>\n<h2>Process the Uploads<\/h2>\n<p>Next, ensure that uploaded files are stored though the form by adding a <code>storeUploads<\/code> method to the <code>FileUploadController.php<\/code> file, as follows:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">\u2026.\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">storeUploads<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n    <\/span>{\n        $request-&gt;file(<span class=\"hljs-string\">'file'<\/span>)-&gt;store(<span class=\"hljs-string\">'images'<\/span>);\n\n        <span class=\"hljs-keyword\">return<\/span> back()\n            -&gt;with(<span class=\"hljs-string\">'success'<\/span>, <span class=\"hljs-string\">'File uploaded successfully'<\/span>);\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>The code above grabs the file uploaded through the POST request, creates on your local service an <code>images<\/code> directory, and stores the file there.<\/p>\n<p>Test it: upload a file for your app and see if the file is in the <code>storage\/app\/images<\/code> directory.<\/p>\n<p><strong>Note:<\/strong> For more details on a recently uploaded file, call these methods:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"> $fileName = $request-&gt;file(<span class=\"hljs-string\">'file'<\/span>)-&gt;getClientOriginalName();\n $extension = $request-&gt;file(<span class=\"hljs-string\">'file'<\/span>)-&gt;extension();\n $mime = $request-&gt;file(<span class=\"hljs-string\">'file'<\/span>)-&gt;getMimeType();\n $clientSize = $request-&gt;file(<span class=\"hljs-string\">'file'<\/span>)-&gt;getSize();\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2>Set Up File Uploads to Cloudinary<\/h2>\n<p>Uploading files to the local disk and serving them yourself is rife with limitations, a major one being the lack of scalability. Best to outsource to an external service like Cloudinary, which, besides upload and storage capabilities, offers features for manipulating and managing media, including images, videos, audio, and other emerging types.<\/p>\n<p>To enable file uploads to Cloudinary:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/cloudinary.com\/users\/register_free\">Sign up<\/a> for a free Cloudinary account, log in, and note your cloud name and API keys from the dashboard.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/image\/upload\/w_700,c_fill,f_auto,q_auto,dpr_2.0\/Web_Assets\/blog\/media_library.png\" alt=\"media library\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"1400\" height=\"899\"\/><\/p>\n<\/li>\n<li>\n<p>Install <a href=\"https:\/\/github.com\/cloudinary-labs\/cloudinary-laravel#installation\">Cloudinary\u2019s Laravel SDK<\/a>:<\/p>\n<pre class=\"js-syntax-highlighted\"><code>composer require cloudinary-labs\/cloudinary-laravel\n<\/code><\/pre>\n<p><strong>Note:<\/strong> Please follow the instructions in the #Installation section. Ensure you publish the config file and add your credentials to the <code>.env<\/code> file of your app.<\/p>\n<\/li>\n<li>\n<p>Rewrite the file-upload controller (<code>FileUploadController.php<\/code>) for straight uploads to the cloud:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Controllers<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Request<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">FileUploadController<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Controller<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">showUploadForm<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> view(<span class=\"hljs-string\">'upload'<\/span>);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">storeUploads<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n    <\/span>{\n        $response = cloudinary()-&gt;upload($request-&gt;file(<span class=\"hljs-string\">'file'<\/span>)-&gt;getRealPath())-&gt;getSecurePath();\n\n        dd($response);\n\n        <span class=\"hljs-keyword\">return<\/span> back()\n            -&gt;with(<span class=\"hljs-string\">'success'<\/span>, <span class=\"hljs-string\">'File uploaded successfully'<\/span>);\n    }\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<\/li>\n<\/ol>\n<p>The code above uploads the file straight to your Cloudinary account and returns the image URL by utilizing the <code>cloudinary()<\/code> helper function. We didn\u2019t have to use the <code>Cloudinary<\/code> Facade.<\/p>\n<p><strong>Note:<\/strong> Be sure to replace <code>&lt;your-cloud-name&gt;<\/code>, <code>&lt;your-api-keys&gt;<\/code>, and <code>&lt;your-api-secret&gt;<\/code> with their values from your dashboard. Furthermore, for security while in production environments, always load those credentials from the environment variables. That is, invoke the config method to load the credentials before uploading the file to Cloudinary. <code>dd($response)<\/code> then dumps the response returned from Cloudinary for the uploaded file. Here\u2019s an example of a response:<\/p>\n<p>You can now store the returned URL in the database, and display the image to users anywhere in your app.<\/p>\n<h2>Leverage More Cloudinary Capabilities<\/h2>\n<p>Uploading files barely scratches the surface of media management. Cloudinary helps you administer the entire spectrum of your media\u2019s lifecycle, end to end, from upload and transformation to optimization and delivery. Do <a href=\"https:\/\/www.cloudinary.com\">check it out<\/a>.<\/p>\n<h2>Want to Learn More About File Uploads?<\/h2>\n<ul>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/automating_file_upload_and_sharing\">Automating File Upload and Sharing<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/file_upload_with_php\">Uploading PHP Files and Rich Media the Easy Way<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/file_upload_with_ajax\">AJAX File Upload &#8211; Quick Tutorial &amp; Time Saving Tips<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/impressed_by_whatsapp_technology_build_a_whatsapp_clone_with_image_and_video_upload\">Impressed by WhatsApp technology? Clone WhatsApp Technology to Build a File Upload Android App<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/direct_image_uploads_from_the_browser_to_the_cloud_with_jquery\">Direct Image Uploads From the Browser to the Cloud With jQuery<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/file_upload_with_angular_or_angularjs_to_cloudinary\">File Upload With Angular to Cloudinary<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/uploading_vue_files_and_rich_media_in_two_easy_steps\">Uploading Vue Files and Rich Media in Two Easy Steps<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/node_js_file_upload_to_a_local_server_or_to_the_cloud\">Node.js File Upload To a Local Server Or to the Cloud<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\">Laravel File Upload to a Local Server Or to the Cloud<\/a>\n<\/li>\n<li>\n<a href=\"https:\/\/cloudinary.com\/blog\/javascript_file_upload_to_cloudinary\">JavaScript File Upload in Two Simple Step<\/a>\n<\/li>\n<\/ul>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":41,"featured_media":22134,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[25,119,263,303],"class_list":["post-22133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-asset-management","tag-file-upload","tag-sdk","tag-video"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Laravel File Upload to a Local Server Or to the Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to upload Laravel files to a local server or, through Cloudinary\u2019s PHP SDK, to the cloud\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Laravel File Upload to a Local Server Or to the Cloud\" \/>\n<meta property=\"og:description\" content=\"Learn how to upload Laravel files to a local server or, through Cloudinary\u2019s PHP SDK, to the cloud\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-05T17:43:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-04T22:56:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96-png?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"1540\" \/>\n\t<meta property=\"og:image:height\" content=\"847\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Laravel File Upload to a Local Server Or to the Cloud\",\"datePublished\":\"2020-08-05T17:43:09+00:00\",\"dateModified\":\"2024-06-04T22:56:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\"},\"wordCount\":11,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA\",\"keywords\":[\"Asset Management\",\"File-upload\",\"SDK\",\"Video\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2020\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\",\"url\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\",\"name\":\"Laravel File Upload to a Local Server Or to the Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA\",\"datePublished\":\"2020-08-05T17:43:09+00:00\",\"dateModified\":\"2024-06-04T22:56:57+00:00\",\"description\":\"Learn how to upload Laravel files to a local server or, through Cloudinary\u2019s PHP SDK, to the cloud\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA\",\"width\":1540,\"height\":847},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Laravel File Upload to a Local Server Or to the Cloud\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Laravel File Upload to a Local Server Or to the Cloud","description":"Learn how to upload Laravel files to a local server or, through Cloudinary\u2019s PHP SDK, to the cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud","og_locale":"en_US","og_type":"article","og_title":"Laravel File Upload to a Local Server Or to the Cloud","og_description":"Learn how to upload Laravel files to a local server or, through Cloudinary\u2019s PHP SDK, to the cloud","og_url":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud","og_site_name":"Cloudinary Blog","article_published_time":"2020-08-05T17:43:09+00:00","article_modified_time":"2024-06-04T22:56:57+00:00","og_image":[{"width":1540,"height":847,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96-png?_i=AA","type":"image\/png"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud"},"author":{"name":"","@id":""},"headline":"Laravel File Upload to a Local Server Or to the Cloud","datePublished":"2020-08-05T17:43:09+00:00","dateModified":"2024-06-04T22:56:57+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud"},"wordCount":11,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA","keywords":["Asset Management","File-upload","SDK","Video"],"inLanguage":"en-US","copyrightYear":"2020","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud","url":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud","name":"Laravel File Upload to a Local Server Or to the Cloud","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA","datePublished":"2020-08-05T17:43:09+00:00","dateModified":"2024-06-04T22:56:57+00:00","description":"Learn how to upload Laravel files to a local server or, through Cloudinary\u2019s PHP SDK, to the cloud","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA","width":1540,"height":847},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/laravel_file_upload_to_a_local_server_or_to_the_cloud#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Laravel File Upload to a Local Server Or to the Cloud"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":""}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649720809\/Web_Assets\/blog\/Laravel-File-Upload_221346ce96\/Laravel-File-Upload_221346ce96.png?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/22133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=22133"}],"version-history":[{"count":3,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/22133\/revisions"}],"predecessor-version":[{"id":34374,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/22133\/revisions\/34374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/22134"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=22133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=22133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=22133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}