If I delete file like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
$result = File::delete($destinationPath . DIRECTORY_SEPARATOR . $filename);
It works
But if I delete folder like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
File::delete($destinationPath);
It does not work
Why delete folder does not work?
How can I solve this problem?
You are attempting to use the wrong method. You need to use deleteDirectory and not delete:
$destinationPath = public_path('img/products/'. $id);
File::deleteDirectory($destinationPath);
If you want to delete folder with files than use:-
use Illuminate\Support\Facades\Storage;
Storage::deleteDirectory($public_path);
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I use laravel 5.6
My method to upload image like this :
public function uploadImage($file) {
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
return $fileName;
}
The code works. If the method run, it will save a file in product folder
But I want to add a logic. If the image success save in product folder, then it will make a folder thumb in folder product
I try add this code below $file->move($destinationPath, $fileName); like this :
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$file->move($destinationPathThumb, $fileName)
Then I rum my method, there exist error like this :
Parse error: syntax error, unexpected 'return' (T_RETURN)
It seems the code line is still wrong
How can I solve this problem?
you missed semicolan next to this line
$file->move($destinationPathThumb, $fileName)
put this
$file->move($destinationPathThumb, $fileName);
You are missing a semicolon.
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$file->move($destinationPathThumb, $fileName);
Your IDE can show this you.
I use laravel 5.6
I want to upload files on 2 folders at once. So if the image uploaded successfully in the product folder, I want the image uploaded in the thumbs folder as well
I try like this :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
If the code run, it just success to upload in the product folder. It did not upload in the thumbs folder
There exist error like this :
message Unable to find file (/tmp/phpUSxbEJ).
exception Intervention\Image\Exception\NotReadableException
I try run this code :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
So I remove code to upload in product folder. I try it and it works. It success upload on the thumbs folder
So I think in one process, it only upload in one folder
Is there any other way to upload on 2 folders?
You first upload the temporary file which will be removed once you save it on your disk, thats why u can't reuse it, instead of reusing it, you fetch the saved image and do resizing on it and save it with a different name:
public function uploadImage($file)
{
...
$file->move($destinationPath, $fileName);
//$file here doesn't exist anymore, hence it can't be read
...
$uploadedFile = Storage::get($destinationPath . DIRECTORY_SEPARATOR . $filename);
$image_resize = Image::make($uploadedFile);
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
I find a solution
I try like this :
$file = $file->move($destinationPath, $fileName);
It works
I'm trying to get this path:
C:\xampp\htdocs\site\folder\filename.jpg
By doing:
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\folder\
But it doesn't have the $filename.
I have also tried:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . 'folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\files\folder\filename.jpg
So I tried adding ..\ to get away from the files folder:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . '..\folder' . DIRECTORY_SEPARATOR . $filename);
But that returns:
C:\xampp\htdocs\site\files\..\folder\filename.jpg
How can I get to the correct folder?
$filename = 'filename.jpg';
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This should give the correct value
My method to upload file in laravel like this :
private function addPhoto(UploadedFile $photo, $fileName)
{
dd(public_path() . DIRECTORY_SEPARATOR . 'img');
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img';
$photo->move($destinationPath, $fileName);
}
The result of dd(public_path() . DIRECTORY_SEPARATOR . 'img'); like this :
C:\xampp\htdocs\myshop\public\img
But I want to change it
So, I want to save image uploaded in here :
C:\xampp\htdocs\myshop\storage\temp
How can I save the image in storage folder?
You can use the exact same way, but replace public_path() with storage_path(). This will link to the storage folder for this application.
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'temp';
All path helpers can be found in the Laravel documentation.
Use storage_path.
private function addPhoto(UploadedFile $photo, $fileName)
{
$photo->move(storage_path('temp'), $fileName);
}
My image directory is \webroot\files\thumbs
I am trying to add file_exists condition. For that I tried bellow code
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'jpg';
$file_exists = file_exists($file);
It's always returning zero.
If I echo $file it's giving me output like this
c:\xampp\htdocs\myproject\files\thumbs\_61.jpg
You're missing the . before the extension. Update your $file definition as follows:
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'.jpg';
// This was missing ---^
$file_exists = file_exists($file);