I cannot delete file by using laravel Storage::delete fuction.
My image store code is :
$logoPath = $request->file('logo')->store('images','public');
It store image at
storage/app/public/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png
I use Storage::delete('/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png'); to delete it. But it not working;
What I have tried is:
Storage::delete(asset('storage/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png'));
Storage::delete(storage_path('images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png'));`
Even I try to delete some picture at public folder. It's not working.
Storage::delete(public_path('images/logo3.png'));
Did I do something wrong?
this is what works for me, hopefully it works for you too:
Storage::delete('public/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png');
The delete method accepts a single filename to remove from the disk:
Storage::delete('public/images/filename.png');
This is the php way of deleting a file. unlink() function.
$file = $request->input('filename');
unlink(storage_path('app/public/images/' . $file));
Related
I want to delete these files in my laravel like this. But it doesnt work!!
use Storage;
class SomeController extends Controller
{
public function delete()
{
$filesToDelete = [
'http://minio:9100/minio/myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
'http://minio:9100/minio/myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
];
// cant delete using this
Storage::disk('s3')->delete($filesToDelete);
}
}
But when I try to delete using this command in my bash it works perfectly
aws --endpoint-url http://minio:9100 s3 rm s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
delete: s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
my aws configure list is exactly the same in my project config!
I can use Storage to upload the files but when I try to delete the files it doesnt work:
// Working
Storage::disk('s3')->put('1/ImNkE0wwrstgSpzFyruw8.jpeg', $file);
Am I doing something wrong? Is there anyway to delete these files using laravel??
https://docs.min.io/docs/aws-cli-with-minio
https://readouble.com/laravel/5.4/en/filesystem.html
I think the problem is file path. Try to run as mentioned below.
Storage::disk('s3')->delete('myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg');
I solving this parsing url, like this:
$file_path = parse_url($file_url);
//return $file_path['path'];
Storage::disk('s3')->delete($file_path);
it works for me.
In your bucket, the files has a key, thath be need to delete file from your php function. to get this the object url be parsed.
You can do it by this way
$file_name='your_file_path';
unlink(storage_path($file_name));
You are adding wrong file path.In order to remove image form s3 bucket you are not need to give full path of image, Just add root path of image.
To delete multiple images
$files = [
'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
'myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
];
Storage::disk('s3')->delete($files);
Delete single image
$file = 'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg';
Storage::disk('s3')->delete($file);
I am using Zipper to extract uploaded zip file and delete the file after extract.
So I upload and extract like this:
$f = $request['file']->move(public_path($directory), $fullFileName);
\Zipper::make($f)->extractTo(public_path($directory) . $fileName);
and it works great. I've tried to delete the file using these ways.
1 - Storage::disk('products')->delete($fullFileName);
2 - File::delete(public_path($directory) . $fullFileName);
3 - $del = unlink(public_path($directory) . $fullFileName);
but in all actions get resource temporarily unavailable error.
I found this error is because of the zipper (simple files and directories works).
so my question is, How can I delete upload zip file after extract, using a zipper?
Any idea would be great.
thanks in advance.
You need to call $zipper->close(); after you extract it, so if you do something like this it should work:
$zipper = new \Chumper\Zipper\Zipper;
$zipper->make($f)->extractTo(public_path($directory) . $fileName);
$zipper->close();
unlink(public_path($directory) . $fullFileName);
If you do not close the zipper it will not write the result to the disk and keep the original file locked. See the documentation.
$zip = new Zipper;
$zip->make($pathZipFile)->extractTo($destinationPath);
$zip->close(); // important
unlink($pathZipFile); // delete Zip file after
In my project I have a folder secure in root. The project package looks like:
application
secure
system
...........
Inside secure folder I am uploading some images on form submit using
$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'];
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('thumb_image');
and it is working properly. Now while on editing the details, using another form, if I am uploading a new image instead of the current image file, I want to unlink the current one and then upload new file.
For this I am using the code:
unlink(base_url("secure/".$data['row']->videothumbnail));
I also tried with
unlink('/secure/'.$data['row']->videothumbnail);
where $data['row']->videothumbnail) is the current image file from database. New file is successfully uploaded. But old file is not getting unlinked. I have set the permission of secure folder to 777. But the images are uploaded with read only permission. Is it because of this, it is not getting unlinked?
Can anyone help me to solve this?
Thanks in advance.
Try this:
Set the permission dynamically using:
#chmod('./secure/'.$data['row']->videothumbnail, 0777);
then try unlink:
#unlink('./secure/'.$data['row']->videothumbnail);
Try echoing the path that you are providing to unlink function.
It should be something like this:
base_url()."secure/".$data['row']->videothumbnail;
I also had this issue even after setting the right permission on the folder. But the following code worked for me.
unlink(realpath(APPPATH . '../uploads').'/'.$ImageName);
Try to use $_SERVER['DOCUMENT_ROOT'] instead of base_url
$this->load->helper("file")
unlink(base_url('folder/file.ext'));
location:
\app\controller
\system\libraries
**folder\file.ext**
$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
unlink($unlinkUrl);
}
else{
echo $unlinkUrl." is not available";
}
I think you are just making a stupid mistake.
Firstly, the first param of unlink should be a relative path or absolute path, but base_url function will return you a path contains domain name, HOW CAN YOU DELETE A FILE ON REMOTE SERVER ?
Secondly, '/secure/'.$data['row']->videothumbnail here is not a relative path but a absolute path
YOU MUST change it into /the/absolute/path/to/secure/ or ./the/relative/path/to/secure/ (DO NOT MISS THE DOT)
use this to unlink
$oldthumb = "secure/".$data['row']->videothumbnail;
#unlink($oldthumb);
First Load the $this->load->helper("file") and then unlink it
unlink("secure/".$data['row']->videothumbnail);
if ($rowAffected > 0) {
if ($isMediaUpload)
if (file_exists('./uploads/' . $this->input->post('img_url')))
unlink('./uploads/' . $this->input->post('img_url'));
redirect('/admin/configration', 'location');
}
Though i came in late but someone might need this .
unlink(FCPATH."secure/".$data['row']->videothumbnail)
**FCPATH** - path to front controller, usually index.php
**APPPATH** - path to application folder
**BASEPATH** - path to system folder.
Fast to explain, but I can't get it to work:
In this simple code, the function force_download simply doesn't make any output.
$this->load->helper('download');
$data = file_get_contents("modulos/".$filename); // Read the file's contents
force_download($filename, $data);
echo $data."/".$filename;
Here I just get a white screen, but the file content is show (well you know, the strange codified content :)
I think it is simple enough, I just want the file downloaded with no other effect, am I doing something wrong?
This will work with you
$this->load->helper('download');
$path = file_get_contents(base_url()."modulos/".$filename); // get file name
$name = "sample_file.pdf"; // new name for your file
force_download($name, $path); // start download`
Just a note to anyone else who may be having this problem: Make sure you have a file extension on the filename you supply for the first argument to force_download().
CodeIgniter uses this to set the MIME type, and it doesn't seem to work without.
Remove that echo $data."/".$filename; It should be like this
$this->load->helper('download');
$data = file_get_contents("modulos/".$filename); // Read the file's contents
force_download($filename, $data);
You should not call function after force_download(), Just remove the last line.
remove base_url() and do like this
$path= file_get_contents('./uploads/abc.jpg);
how can I copy two times the same file? I'm trying to do something like this:
copy($file['tmp_name'], $folder."1.jpg");
copy($file['tmp_name'], $folder."2.jpg");
copy($file['tmp_name'], $folder."3.jpg");
And how many time does temp files has before it's destroyed by the server?
I try using move_uploaded_file also, but I can't make it work. I want to generate 2 thumbs from an uploaded file.
Some help?
Thanks,
move_uploaded_file will move the file, and not copy it -- which means it'll work only once.
If you are using copy, there shouldn't be any limit at all on the number of times you can copy : the temporay file created by the upload will only be destroyed at the end of the execution of your script (unless you move/delete it before, of course)
Still, maybe a solution would be to use move_uploaded_file first, and, then, copy ?
A bit like that, I suppose :
if (move_uploaded_file($file['tmp_name'], $folder . '1.jpg')) {
copy($folder . '1.jpg', $folder . '2.jpg');
copy($folder . '1.jpg', $folder . '3.jpg');
}
This would allow you to get the checks provided by move_uploaded_file...
If this doesn't work, then, make sure that :
$folder contains what you want -- including the final /
That $file['tmp_name'] also contains what you want (I'm guessing this is some kind of copy of $_FILES -- make sure the copy of $_FILES to $file is done properly)
Why doesn't move_uploaded_file() work? Are you trying to use it twice? You can't do that, it moves it, so the second time will fail.
I would just use move_uploaded_file() once, and then make the second copy from the location you just moved it to:
move_uploaded_file($uploaded, $destination);
copy($destination, $destination2);
I don't have a reply to your question directly but how about this workaround ?
copy($file['tmp_name'], $folder."1.jpg");
copy($folder."1.jpg" , $folder."2.jpg");
copy($folder."1.jpg" , $folder."3.jpg");
Thanks man, you give me the light.
I made something like this:
$objUpload = new Upload();
$filename = $objUpload->uploadFile($newFile,$folder);
// returns a string
$objUpload->makeThumb($filename,$folder,"thumbs",139);
// makes a 139px thumbnail from the original file uploaded on the first step
$objUpload->makeThumb($filename,$folder,"mini",75);
// makes another thumb from the same file
Using move_ulploaded_file and copy we can make only one thumb. :)