Laravel

Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4



Naming Files when uploading with Laravel on Windows

From WikiOD

Parameters[edit | edit source]

Param/Function Description
file-upload name of the file <input> field
$sampleName could also be dynamically generated string or the name of the file uploaded by the user
app_path() is Laravel helper to provide the absolute path to the application
getCLientOriginalExtension() Laravel wrapper to fetch the extension of the file uploaded by the user as it was on the user machine

Generating timestamped file names for files uploaded by users.[edit | edit source]

Below won't work on a Windows machine

$file = $request->file('file_upload');
$sampleName = 'UserUpload';
$destination = app_path() . '/myStorage/';
$fileName = $sampleName . '-' . date('Y-m-d-H:i:s') . '.' . 
$file->getClientOriginalExtension();
$file*>move($destination, $fileName);

It will throw an error like "Could no move file to /path..."

Why? - This works perfectly on a Ubuntu server

The reason is that on Windows colon ':' is not allowed in a filename where as it is allowed on linux. This is such a small thing that we may not notice it upfront and keep wondering that why a code which is running well on Ubuntu (Linux) is not working?

Our first hunch would be to check the file permissions and things like that but we may not notice that colon ':' is the culprit here.

So in order to upload files on Windows, Do not use colon':' while generating timestamped filenames, instead do something like below:

$filename = $sampleName . '-' . date('Y-m-d-H_i_s') . '.' . $file->getClientOriginalExtension();  //ex output UserUpload-2016-02-18-11_25_43.xlsx

                                   OR

$filename = $sampleName . '-' .date('Y-m-d H i s') . '.' . $file->getClientOriginalExtension();  //ex output UserUpload-2016-02-18 11 25 43.xlsx

                                   OR

$filename = $sampleName . '-'.date('Y-m-d_g-i-A').'.' . $file->getClientOriginalExtension(); //ex output UserUpload-2016-02-18_11-25-AM.xlsx

Credit:Stack_Overflow_Documentation