20698

laravel 5.3 upload file

Question:

I'm using laravel 5.3, and i want to upload a file to a directory. i've been following laravel documentation for uploading file, like this code :

public function store(Request $request) { $path = $request->attachment->store('attachment'); }

but it get an error

<blockquote>

Call to a member function store() on string

</blockquote>

previously on laravel 5.2, i've been use this code for uploading file and it's work

if ($request->hasFile('attachment')) { $destination = 'upload/attachment'; $file = $request->file('attachment'); $file->move($destination, $file->getClientOriginalName()); } $filelocation = $destination. "/" .$file->getClientOriginalName();

but in laravel 5.3 it's doesn't work, and i get an error

<blockquote>

Undefined variable: destination

</blockquote>

can you help what wrong with my code ?

Answer1:

public function store(Request $request) { $path = $request->attachment->store('attachment'); }

I don't think $request->attachment does what you are trying to do. Change that line to:

$request->file('attachment')->store('/your/destination/path') <hr />if($request->hasFile('attachment')){ $destination = 'upload/attachment'; $file = $request->file('attachment'); $file->move($destination, $file->getClientOriginalName()); } $filelocation = $destination. "/" .$file->getClientOriginalName();

When $request->hasFile('attachment') is false, $destination will not get declared. So when it reaches the $filelocation line you get an undefined variable.

Answer2:

In Larave 5.3 you can use simple upload file like this:

$request->file('file')->storePublicly($folderSrc);

This save you file to storage/app/public/you-folder-src

Recommend

  • alert() and console.log() not working in Firefox 26
  • PHP - Domain to IP
  • Check if music playing in android media player API
  • laravel-5.4 - error :Creating default object from empty value
  • Displayin tinyMCE after twice ajax call
  • jQuery function before the Postback for FileUpload in ASP.NET
  • how to get the full path of file upload control in asp.net?
  • Asp.net fileupload control postback problems
  • Powershell Transcript is empty when running script from SQL Agent Job in 2005 SQL Server
  • Sorting elements of vector where each element is a pair [duplicate]
  • TextBox controls are not Working with Export To PDF(iTextSharp)
  • How to update the UI immediately when a new record is added? Related to ember-cli-pagination
  • SOAP with Attachment / MIME content
  • Fixed Background Works in Chrome but Not Firefox?
  • How to create virtual printer with iOS Simulator?
  • removing last 3 characters on a file (file extension)
  • SetWindowsHookEx does not react on media keys
  • Checking free space on FTP server
  • output of program is not same as passed argument
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • Hits per day in Google Big Query
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Busy indicator not showing up in wpf window [duplicate]
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?