I can upload my images while i'm working on local but when I moved my site to host it doesn't upload images while get names of them in database,
my filesystems.php
'default' => env('FILESYSTEM_DRIVER', 'local'),
disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
....
sample of my upload codes:
if ($request->hasFile('imageOne')) {
$imageOne = $request->file('imageOne');
$filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
$location = public_path('images/');
$request->file('imageOne')->move($location, $filename);
$product->imageOne = $filename;
}
Issue:
In host i get uploaded image name in database, but no image in my
public/images folder.
any idea?
UPDATE
I found interesting folder in my host, as I moved all my public folder files to server public_html now it created another folder in my root called public and inside that has images folder including all images i uploaded so far!
Questions:
Why laravel created new public/images folder in my root instead of
using my public_html?
How to fix it?
SOLVED
I added this code to my public_html/index.php file and it's working now.
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
Hope it helps.
I change the public path inside my save image controller to look like this:
if (request()->imagefile->move(public_path('../../public_html/destination'), $imageName))
echo "Yap";
else echo "Nop";
Related
I'm generating PDF documents on the fly using php-pdftk and saving it to the public directory. Works great locally, but on a production Digital Ocean server, the file won't save to the public directory. In my code below at the ->saveAs..., the 'docs/reg-forms/' are directories within public.
...
$pdf = new Pdf('/docs/ax_reg_form.pdf');
$result = $pdf->fillForm([
'email' => $this->usercar->user->email,
'date' => $this->usercar->created_at,
...
])
->needAppearances()
->saveAs('docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');
session()->flash('url', '/docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');
return redirect()->route('usercar.show', $this->usercar->id);
try to wrap with public_path helper
public_path('docs/reg-forms/');
and check if the path exists
if (!file_exists(public_path('docs/reg-forms/'))) {
mkdir(public_path('docs/reg-forms/'), 0777, true);
}
i am trying to create a file in laravels App/Http/Controller directory
so i had used this code
$path = 'App/Http/Controller/';
$fileName = 'demo.php';
$contents = "<?php echo 'hello'; ?>";
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
$path = $path.$fileName;
file_put_contents($path, $contents);
but this is creating a file with directory in Public folder..
how to get rid of this.?
Setting up a Disk
You can setup a disk at config/filesystems.php and then benefit from Storage facade methods.
config/filesystems.php
'tmp' => [
'driver' => 'local',
'root' => public_path('..'),
],
Notice: public_path() Starts from public folder, you can go one step ahead using public_path('..')
Using Storage Facade
You can now use Storage methods such as allDirectories and allFiles for retrieving directories and files, or put for saving new files as thoroughly explained in laravel doc.
Storage::disk('tmp')->allDirectories();
Storage::disk('tmp')->allFiles('routes');
// Storing new photo
Storage::disk('tmp')->put('newfile.jpg',$content);
I'm trying to store base64Image with laravel but it returns this error:
Can't write image data to path (/home/u187504358/domains/example.com/public/images/Group-1590235658.jpg)
Code
if ($request->photo) {
$photo = $request->photo;
$filename = 'Group' . '-' . time() . '.' . 'jpg';
$location = public_path('images/'. $filename);
Image::make(file_get_contents($photo))->resize(500, 500)->save($location);
$oldFilename = $group->photo;
if(!empty($group->photo)){
Storage::delete($oldFilename);
}
$group->photo = $filename;
}
filesystem.php
'default' => env('FILESYSTEM_DRIVER', 'local'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
]
Any idea?
Solved
I've added this code to my index.php file in public aka public_html folder and it works now
$app->bind('path.public', function() {
return __DIR__;
});
Note:
As you see in error it tries to connect with folder public (domains/example.com/public/images) but in server (shared host) there is public_html folder. So it made me think twice that laravel tries to connect to the wrong path and code above solved the issue.
-Hope it help others.
I use this yii2 module for an image uploading purposes.
My upload folder is:
C:\XAMPP\HTDOCS\MYAPPNAME\WEB\UPLOAD
├───cache
├───global
└───store
└───Products
└───Product15
Where store is the folder for original pictures and the cache is for the resized ones. yii2-images is configured like this:
'yii2images' => [
'class' => 'rico\yii2images\Module',
//be sure, that permissions ok
//if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
'imagesStorePath' => 'upload/store', //path to origin images
'imagesCachePath' => 'upload/cache', //path to resized copies
'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
'placeHolderPath' => '#webroot/upload/store/no-image.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
],
and my Product model upload method is:
public function upload()
{
if ($this->validate()) {
$path = 'upload/store/' . $this->image->baseName . '.' . $this->image->extension;
$this->image->saveAs($path);
$this->attachImage($path);
#unlink($path);
return true;
} else {
return false;
}
}
Uploading images work. I mean, they fisically appear where they should. I load image
<?php $img = $model->getImage(); ?>
and in my DetailView widget instead of 'img':
[
'attribute' => 'image',
'value' => "<img src='{$img->getUrl()}'>",
'format' => 'html',
],
And what I get instead of a rendered image is:
image-by-item-and-alias?item=Product15&dirtyAlias=71273544a6-1.jpg
I have no idea where this "image-by-item-and-aliasitem=Product15&dirtyAlias?" part even comes from when it actually must be something like:
Products\Product15\71273544a6-1.jpg
and given format html parameter it must therefore render an image.
Can you please pinpoint me to the source of this error?
I need to upload a file in my Laravel eCommerce app and store it inside the respective user's folder. My code looks like this:
if ($request->hasFile('attach')) {
try {
$file = $request->file('attach');
$filename = $file->getClientOriginalName();
Storage::disk('local')->put($filename, File::get($file));
Storage::move('app/'.$filename, 'app/public/uploads/'.$user_id.'/attach/'.$filename);
I've this in config/filesystems.php:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
But I get error while uploading a file:
(1/1) FileNotFoundException: File not found at path: app/Test1.docx
How can I store/save a file in a custom location such as user's folder? The custom location where uploaded files are stored look like this:
'app/public/uploads/'.$user_id.'/attach/'
As advised by #sohel0415, here is the updated code that worked for me:
if ($request->hasFile('attach')) {
try {
$file = $request->file('attach');
$filename = $file->getClientOriginalName();
Storage::disk('local')->put('public/uploads/'.$user_id.'/attach/'.$filename, File::get($file));