how to set folder destination path outside laravel project - php

I need to set $destination path to outside project need to copy folder from project file to computer location i have already tried using copy directory function
public function approovenew()
{
$source = "themes/call";
$destination = "/new-theme";
File::copyDirectory(base_path($source), base_path($destination));
}

Try to do this it will help you out
File::copyDirectory(__DIR__ . '/../whatever'$source, __DIR__ . '/../../$destination');

Related

File is not moving in directory in lumen

I am trying to store a file in the following path "public/uploads/images" with lumen.
Here is my code
if($request->hasFile('photo')){
$file = $request->file('photo');
$fileName = time().$counter.'.'.$request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move('/uploads/images', $fileName);
return $fileName;
}
But no file is moving to the path. only the file name is returned. What is the problem and how to solve?
try to use the public_path function to upload it into public folder.
Example:
$request->file('photo')->move(public_path("/uploads"), $newfilename);
Visit https://laracasts.com/discuss/channels/laravel/image-upload-using-storage-function-to-public-folder

Getting a Storage file and copying to another storage place

So, I have storage files saved and can access them like this:
$file = Storage::disk('public')->url("featured/{$i->image}")
That nets me the full url of the file I have in my storage/ directory. How can I make it so that can copy and paste that same exact file into a new directory but with a new file name?
I have tried:
$url = Storage::disk('public')->url("featured/{$i->image}");
$fileName = 'u_' . (new \DateTime())->getTimestamp() . '.png';
$fileDestination = '/new_folder/' . $fileName;
$i->image = $fileDestination;
File::copy($url, $fileDestination);
I am getting PHP execution timeout errors. I don't think I am referencing the paths correctly.
I want to copy /storage/app/public/featured/imageOne.png to /storage/app/public/new_folder/imageOneWITHNEWFILENAME.png
Is there a Storage facade solution?
Use Storage facade instead of File::copy to copy file to the new destination like
Storage::disk('public')->copy("featured/{$i->image}", $fileDestination);
Reference: Laravel 5.5 file storage

Cannot save image intervention image. NotWritableException in Image.php line 138

Im trying to save a manipulated image which i will them push to s3.
My code that works This code saves the image directly within the public folder*
public function store(Filesystem $filesystem)
{
$request = Input::all();
$validator = Validator::make($request, [
'images' => 'image'
]);
if ($validator->fails()) {
return response()->json(['upload' => 'false']);
}
$postId = $request['id'];
$files = $request['file'];
$media = [];
$watermark = Image::make(public_path('img/watermark.png'));
foreach($files as $file) {
$image = Image::make($file->getRealPath());
$image->crop(730, 547);
$image->insert($watermark, 'center');
$image->save($file->getClientOriginalName());
}
}
What i would like to achieve is to be able to save it within a folder of it's own. Firstly what is the best place to store an image for a blog post, within the storage of public folder? But anyway when i do this:
$image->save('blogpost/' . $postId . '/' . $file->getClientOriginalName());
// Or this
$image->save(storage_path('app/blogpost/' . $postId . '/' . $file->getClientOriginalName()));
I get the error:
folder within public
NotWritableException in Image.php line 138: Can't write image data to
path (blogpost/146/cars/image.jpg)
or
storage path
NotWritableException in Image.php line 138: Can't write image data to
path /code/websites/blog/storage/app/blogpost/146/image.jpg
I've tried
cd storage/app/
chmod -R 755 blogpost
And it still wont work
Thank you for reading this
Ok so here is how i solved it, I made the directory first before storing,
Storage::disk('local')->makeDirectory('blogpost/' . $postId);
Once the folder is created i then go on to store the manipulated images like so:
$image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName));
And then pushing the image to S3
$filesystem->put('blogpost/' . $postId . '/' . $imageName, file_get_contents(storage_path('app/blogpost/' . $postId . '/' . $imageName)));
This worked
You can solve it by casting the Intervation/Image variable to a data stream using function stream. Then use the Storage Laravel facade to save the image.
$img = Image::make('path-to-the-image.png')->crop(...)->insert->stream('jpg', 90)
Storage::put('where_I_want_the_image_to_be_stored.jpg', $img);
Laravel 5 needs permission to write to entire Storage folder so try following,
sudo chmod 755 -R storage
if 755 dont work try 777.
Am improving #Amol Bansode answer.
You are getting this error because $postId folder does not exist in the path you specified.
You could do it like this:
//I suggest you store blog images in public folder
//I assume you have created this folder `public\blogpost`
$path = public_path("blogpost/{$postId}");
//Lets create path for post_id if it doesn't exist yet e.g `public\blogpost\23`
if(!File::exists($path)) File::makeDirectory($path, 775);
//Lets save the image
$image->save($path . '/' . $file->getClientOriginalName());
In my case I migrated a project from Windows 10 to Parrot (Debian-Linux) and I had the same problem and turned out the slashes were backward slashes and Linux interpret them differently. Unlike Windows, it doesn't really matter.
//Windows Code:
$image_resize->save(public_path('\storage\Features/' .'Name'.".".'png'));
//Linux Code (Working):
$image_resize->save(public_path('storage/Features/' .'Name'.".".'jpg'));
I know the question is related to Laravel but I came across from making it to work with WordPress. If somebody is coming from WordPress world, this code works (I was getting the same 'cannot write' error) and changing directory permission would not work as the library probably needs the relative path (the code below is valid for direct installation of Intervention through composer into any PHP application, not Laravel per se)-
require 'vendor/autoload.php';
use Intervention\Image\ImageManager;
$manager = new ImageManager(array('driver' => 'imagick'));
$image = $manager->make('PUBLIC IMAGE URL/LOCAL IMAGE PATH');
$image->crop(20, 20, 40, 40);
$image->save(__DIR__ . '/img/bar.png');
in my case:
i double check my symlinks in filesystem.php in config folder in laravel
and remove all symlink then regenerate them
php artisan storage:link
in my case my fileName is not valid format for naming in windows but it worked on linux or docker
$make_name = date('Y-m-d-H:i:s') . hexdec(uniqid()) . '.' . $img->getClientOriginalExtension();
when I remove this date('Y-m-d-H:i:s') it worked as well
I have faced the same issue but 775 permission not sort it so I changed the write permission to the folder(also sub-folders) to 777 to get over this issue.

How to get all images in a folder using CakePHP

I am trying get all images which are located in standard cakePHP image folder. I am using:
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
$dir = new Folder('/app/webroot/img/');
$files = $dir->find('.*\.png');
pr($files);
but i always get empty array. Where is the problem?
Ina addition when I try make dir in that folder i get error:
mkdir(): No such file or directory [CORE\Cake\Utility\Folder.php, line 515]
By doing new Folder('/app/webroot/img/'); you're actually saying your app folder is in the root of the drive, and since it isn't, CakePHP will try and create it, which it can't (that mkdir error).
You probably need to do something like
$dir = new Folder(App.imageBaseUrl);
or
$dir = new Folder(APP_DIR . DS . "webroot" . DS . "img");.
Check the constants CakePHP gives you to handle paths http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#core-definition-constants should be usefull
Knowing the answer is given and accepted,
This is a right way given by the documents.
<?php
// Find all .png in your app/webroot/img/ folder and sort the results
$dir = new Folder(WWW_ROOT . 'img');
$files = $dir->find('.*\.png', true);
http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::find

recursive copying of files and folders and sub folders

I am using the function i have written to copy directories and files. It just copies the directories and files once. How can i copy the files from source folder to destination folder only if the file does not exist?
here is the function. Basically i want to copy the folder labref in analyst_uploads and its content to the second folder store_for_issue. If a file is uploaded to the source folder on the same day, the files should be copied to the destination folder 'store_for_issue'. Create folder if does not exist or update the contents
public function full_copy( ) {
$labref= $this->uri->segment(3);
$source='analyst_uploads/'.date('Y').'/'.date('M').'/'.$labref;
$newfolder='store_for_issue';
if(is_dir($newfolder)){
mkdir($newfolder.'/'.$labref,0777,TRUE);
}
$target=$newfolder.'/'.$labref.'/';
copy( $source, $target );
}
How about this:
public function full_copy( ) {
$labref= $this->uri->segment(3);
$source='analyst_uploads/'.date('Y').'/'.date('M').'/'.$labref;
$newfolder='store_for_issue';
if(!is_dir($newfolder)){
mkdir($newfolder.'/'.$labref,0777,TRUE);
}
$target=$newfolder.'/'.$labref.'/';
shell_exec('cp -ur '. $source . ' ' . $target);
}
This uses linux cp -ur and will only copy files that don't exist in target directory, or replace files in target directory that are older than the source files. It will operate recursively.
Of course if you wanted a PHP-only solution, you could perhaps use a RecursiveDirectoryIterator or similar to iterate through all the files in the source directory and compare/copy them one by one.
if (!file_exists($target)) {
copy( $source, $target );
}

Categories