Can't upload images to a public directory in laravel - php

I'm trying to upload images into a Laravel app that will then need to be publicly displayed. However, I seem to only be able to upload to a folder within the application root. Any attempt to upload to the public directory throws the following error:
protected function getTargetFile($directory, $name = null)
{
if (!is_dir($directory)) {
if (false === #mkdir($directory, 0777, true)) {
throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
I'm assuming this means that Laravel is going to prevent me from uploading to any file that is publicly accessible. I actually rather like this, however, how can I link to images that are outside of the public directory I've tried to ../ my way out of the public folder, but appropriately, that didn't work.
Alternately, anything that will let me uploaded directly to the public folder would be great too.
If it helps, here is my controller method that puts the file in public folder:
$thumbnail_file = Input::file('thumbnail');
$destinationPath = '/uploads/'. str_random(8);
$thumbnail_filename = $thumbnail_file->getClientOriginalName();
$uploadSuccess = Input::file('thumbnail_url')->move($destinationPath, $thumbnail_filename);

Route::post('upload', function(){
$avarta = Input::file('avatar');
if(strpos($avarta->getClientMimeType(),'image') !== FALSE){
$upload_folder = '/assets/uploads/';
$file_name = str_random(). '.' . $avarta->getClientOriginalExtension();
$avarta->move(public_path() . $upload_folder, $file_name);
echo URL::asset($upload_folder . $file_name); // get upload file url
return Response::make('Success', 200);
}else{
return Response::make('Avatar must be image', 400); // bad request
}});
will upload to /public/assets/uploads folder, maybe help you

Your destination path should be:
$destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));

Try using this one in your controller after creating a folder called uploads in your public and another folder called photos in uploads.
$data = Input::all();
$rules = array(
'photo' => 'between:1,5000|upload:image/gif,image/jpg,image/png,image/jpeg',
);
$messages = array(
'upload' => 'The :attribute must be one of the following types .jpg .gif .png .jpeg',
'between' => 'The :attribute file size must be 1kb - 5mb'
);
$validator = Validator::make($data, $rules, $messages);
if($validator->passes())
{
$uploads = public_path() . '/uploads/photos';
$photo = Input::file('photo');
$photodestinationPath = $uploads;
$photoname = null;
if($photo != null)
{
$photoname = $photo->getClientOriginalName();
Input::file('photo')->move($photodestinationPath, $photoname);
}
$thumbnail = new Model_name();
$thumbnail ->column_name_in_table = $photoname;
and then put this in your routes
Validator::extend('upload', function($attribute,$value, $parameters){
$count = 0;
for($i = 0; $i < count($parameters); $i++)
{
if($value->getMimeType() == $parameters[$i])
{
$count++;
}
}
if($count !=0)
{
return true;
}
return false;});

In the ImageProductController.php (Controller) file insert the following code:
public function store(Request $request, $id)
{
if ($request->hasFile('image')){
$rules = [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg'
];
$this->validate($request, $rules);
// Save file in directory
$file = $request->file('image');
$path = public_path() . '/images';
$fileName = md5(rand(0,9999)).'.'.$file->getClientOriginalExtension();
$file->move($path, $fileName);
$imageProduct = new ImageProduct();
$imageProduct->image = $fileName;
}
In the file create.blade.php (View) insert the following code:
<form enctype="multipart/form-data" method="post" action=" " >
{{ csrf_field() }}
<label for="image" class="btn btn-primary">Inserir Imagem</label>
<input type="file" name="image" id="image" accept="image/*"
class="form-control" value="{{ old('image') }}>
</form>
In the ImageProduct.php file (Model) insert the following code:
public function getUrlAttribute()
{
if(substr($this->image, 0, 4) === "http"){
return $this->image;
}
return '/images/'.$this->image;
}
Good luck!

You need to give permission the laravel folder where you want to store images.
The commend is:
sudo chown www-data:www-data /var/www/html/project-name/path/to/folder.

Related

how to solve laravel file uploading problem

I tried to upload image on my public folder but i constantl getting error like this
The "C:\xampp\tmp\phpCE7B.tmp" file does not exist or is not readable.
Her is my code that i tried so far
public function create(Register $request)
{
//Registering Students
$student=new Student;
$student->name = $request->input('name');
$student->username = $request->input('username');
$student->email = $request->input('email');
$student->password = bcrypt($request->input('password'));
$student->gender = $request->input('gender');
$student->phone = $request->input('phone');
if ($request->hasFile('image')) {
$file=$request->File('image');
$ext=$student->username. "." .$file->clientExtension();
$path = public_path(). '/images/';
$file->move($path,$ext);
$student->image = $ext;
}
$student->save();
}
it saved my info with image in database but after storing gives me the error.Please help me to solve this
"UPDATE"
I dont Know what is happening to me, but now its working autometically
I have the same problem when i'm trying to upload image to public path.
I solved the problem by defining a file system disk in config (filesystems) like that:
'YourDiskName' => [
'driver' => 'local',
'root' => public_path(),
],
then you can upload images to this path by using StoreAs method:
$file = $file->storeAs(YourPathInPublic, YourFileName, [
'disk' => 'YourDiskName'
]);
// Get Form Image
$image = $request->file('image');
$slug = str_slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
if (!file_exists('storage/uploads/post'))
{
mkdir('storage/uploads/post',0777,true);
}
$image->move('storage/uploads/post',$imagename);
}else{
$imagename = "default.png";
}
//getting coding
public function index(){
return view('admin.post.index',compact('posts'));
}
// show code
<div class="body">
<img class="img-responsive img-thumbnail" src="{{ asset('storage/uploads/post/'.$post->image) }}" >
</div>
I also had the same issue when I use public_path()
Try something like this rather than using public_path. Actually there is nothing wrong with your code. But this happens most of the time due to permission levels. As others said you can try reinstalling Xampp or just try this.
if ($request->hasFile('image')) {
$file = $request->File('image');
$ext = $student->username.".".$file->clientExtension();
$ext->save('your_public_path/'.$ext);
//$path = public_path().'/images/';
//$file->move($path,$ext);
$student->image = $ext;
}
Example code from one of my project for your info
$image = $request->file('uploadUserAvatar');
$fileName = time().'.'.request()->uploadUserAvatar->getClientOriginalExtension() ;
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300,300);
$image_resize->save(('assets/img/user_avatars/'.$fileName));

Laravel Upload Multiple Images all records with same name and not moved files

I am new in Laravel and I develop a web application where someone can upload a car ad and other users can write comments bellow that. Instead of developing a simple CRUD application I thought about developing also a table that will be related to these ads and will store the photos. Up to this point, I have made the relations, I can show images and I have two problems: The first problem is that not all images are moved to storage/photos folder and the second problem is that all the records in the database have the same name. For example, if I want to upload images 1,2,3 then in the database all the files will have the name 1.
Bellow, you may find the store code. This code stores all the ads and also the images if they exist.
public function store(Request $request)
{
//
$this->validate($request, [
'name' => 'required',
'cc' => 'required',
'desc' => 'required',
'date' => 'required'
]);
$ad = new Ad;
$ad->name = $request->input('name');
$ad->cc = $request->input('cc');
$ad->desc = $request->input('desc');
$ad->date = $request->input('date');
$ad->user_id = auth()->user()->id;
$ad->save();
if ($request->hasFile('photos')) {
$allowedfileExtension = ['pdf', 'jpg', 'png', 'docx'];
$files = $request->file('photos');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
//$file->move('storage/photos', $filename);
$check = in_array($extension, $allowedfileExtension);
if ($check) {
foreach ($request->photos as $photo) {
$photo->move('storage/photos', $filename);
$img = new AdsPhoto;
$img->ad_id = $ad->id;
$img->filename = $filename;
$img->save();
}
return redirect('/home')->with('success', 'ad + image created');
} else {
echo '<div class="alert alert-warning"><strong>Warning!</strong> Sorry Only Upload png , jpg , doc</div>';
}
}
}
return redirect('/home')->with('success','ad created');
}
Thank you in advance!
1st mistake, you did looping twice
2nd mistake (you might not realize this) you didnt merge the filename and extension, this will cause an issue when you retrieving the image in blade
therefore correction on your code plus comment to explain it
if ($request->hasFile('photos')) {
$allowedfileExtension = ['pdf', 'jpg', 'png', 'docx'];
$files = $request->file('photos');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
//$file->move('storage/photos', $filename);
$check = in_array($extension, $allowedfileExtension);
$fullpath = $filename . '.' . $extension ; // adding full path
if ($check) {
// removing 2nd loop
$file->move('storage/photos', $fullpath); // you should include extension here for retrieving in blade later
$img = new AdsPhoto;
$img->ad_id = $ad->id;
$img->filename = $filename;
$img->save();
}
} else {
echo '<div class="alert alert-warning"><strong>Warning!</strong> Sorry Only Upload png , jpg , doc</div>';
}
}
return redirect('/home')->with('success', 'ad + image created');
}
From the documentation, if you want to put your file on storage instead of on public folder you need to put these few lines of code as below in the your controller class
<?php
...
use Illuminate\Support\Facades\Storage;
...
class YourController extends Controller{
...
// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));
...
}
This command will put your file into storage folder

Unable to upload image with laravel after fetch

I am trying to upload an image from my react native project using laravel as my backend framework.
This is the data I send :
I receive a warning that my network request failed.
Here is my backend code :
public function upload(Request $request)
{
$image = $request->get('data');
$name = 'Sup';
Image::make($request->get('data'))->save(public_path('images/').$name);
$fileupload = new Fileupload();
$fileupload->filename=$name;
$fileupload->save();
return response()->json(['message' => 'Success']);
}
I have a function, you can try it!
Please change the path before doing anything else (this is the code used to upload one - multiple files at once)
public function uploadImage (Request $request) {
$files = $request->file('images');
$fileText = '';
foreach($files as $file) {
$rules = array('file' => 'required|mimes:png,gif,jpeg');
$validator = Validator::make(array('file' => $file), $rules);
if($validator->passes()){
$destinationPath = 'storage/images/';
$filename = $file->getClientOriginalName();
$unique_name = md5($filename. time()).$filename;
$upload_success = $file->move($destinationPath, $unique_name);
$fileText .= url('storage/images/' . $unique_name) . '|';
}
}
return rtrim($fileText, '|');
}

Error uploading file in Laravel: Call to a member function on null

I want to upload a file in laravel.
I have used this code in controller:
function store1(Request $request){
$file=$request->file('myfile');
$folder=$request->file('folder');
$link=$request->input('link');
$filename1 = $file->getClientOriginalName();
$filename2 = $folder->getClientOriginalName();
//$projectname
$path="files/";
$file->move($path,$filename1);
$folder->move($path,$filename2);
DB::table('groups')->update(['last_report'=>$filename1,'DropBox'=>$filename2]);
return view('successfulUpload');
}
I want to enable user to upload more than one file but it shows Call to a member function getClientOriginalName() on null.
Try this for multiple upload in Laravel.
The HTML form:
<form method="post" action="controller/function" enctype="multipart/form-data">
<input type="file" name="files[]" multiple />
<input type="submit" />
</form>
In your controller:
// get posted input file
$files = Input::file('files');
$errors = "";
$file_data = array();
foreach($files as $file)
{
if(Input::hasFile('files'))
{
// validating each file.
$rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(
[
'file' => $file,
'extension' => Str::lower($file->getClientOriginalExtension()),
],
[
'file' => 'required|max:100000',
'extension' => 'required|in:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt'
]
);
if($validator->passes())
{
// path is root/uploads
$destinationPath = 'path/to/upload';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
if($upload_success)
{
//do after success ....
}
else
{
$errors .= json('error', 400);
}
}
else
{
// redirect back with errors.
return Redirect::back()->withErrors($validator);
}
}
}

Laravel 5 upload multiple image validation

I am trying to upload multiple images at once. The files are being uploaded but they pass the validation even if they are not a valid iamge. I want to upload all valid files and return a message with the ammount of files that werent uploaded succesful.
My function:
public function addImages($id, Request$request)
{
$files = Input::file('images');
$uploaded = 0;
$failed = 0;
foreach ($files as $file) {
// Validate each file
$rules = array('file' => 'required|image');
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()) {
$destinationPath = 'uploads';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
if($upload_success){
$uploaded ++;
}
else {
$failed ++;
}
} else {
$failed ++;
}
}
\Session::flash('success_message',$uploaded.' files uploaded.');
if($failed > 0){
\Session::flash('errors','Some files werent valid.');
}
return redirect('admin/myroute/'.$id);
}
For some reason if I upload files withhout any extension they are still uploaded. What am I doing wrong?
You should not validate it in controller, You should use each method for it, example in Your Request file.
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
$validator->each('files', ['image']);
return $validator;
}
This simply checks all the files.
more information,
Laravel API about validation
if your taking about validation you should specify like file types you can specify the the name and make a validation like this 'image' => 'mimes:jpeg,jpg,bmp,png' . Hope this would help

Categories