How to delete laravel delete file after extract? - php

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

Related

laravel - Using unllink to delete file, receive is a directory error

Helo.
I have a question. Just now, when I tried to delete a picture using only unlink without the # symbol, it returned the "unlink is a directory" error. What is the reason that causes it? I received an advice that using the symbol # to the unlink is a bad practice, but somehow this is the method that works.
$file = $request->file('image');
if($file->getSize() < 2048000){
$path = storage_path('app/public/' . $event->img);
if (file_exists($path)) {
unlink($path);
}
$filename = Str::uuid() . "." . $file->getClientOriginalExtension();
$event->img = $request->image->storeAs('events', $filename, 'public');
}
// Create and save post with validated data
$event->save();
In the for loop, it is possible that one of the image names is empty and unlink is trying to delete a directory instead of file. unlink is only used to delete files and not directories. If you try to delete a directory using unlink, you will get an error. See the documentation for unlink.
The file_exists function checks whether a file or directory exists. Since you want to check if file exists and not directory, then you should use the is_file function instead of file_exists. See the documentation for is_file.

Laravel Zipper delete files after zipping

I am using a Zipper package for making zip files out of API fetched PDF's. Zipping works fine but I would like to delete PDF files that were zipped.
$pdf_summary_filename = public_path() . $path . uniqid() . '_summary.pdf';
PDF::loadView('pdf.summary', $pdf_data)->save($pdf_summary_filename);
$zipper->make($zip_filename)->add($pdf_summary_filename);
File::cleanDirectory(public_path() . '/user_downloads');
I am using this code, however, I think that cleanDirectory() gets called before the zipping finishes, and I see no zip generated. If I comment out the last line, I get both the zip file as well as PDF's in /user_downloads.
How can I wait for the zipper to finish zipping?
UPDATE: You can try below code:
$flgFile = $zipper->make($zip_filename)->add($pdf_summary_filename);
if($flgFile){
File::cleanDirectory(public_path() . '/user_downloads');
}
This may help you better!

Codeigniter cannot unlink file created by codeigniter

I hope someone can help me with this.
I am creating a text file from the query results from a mysql DB. I then set the file to auto download. Once that is done I am trying to unlink the file. It fails to remove the file from the server. When I go to the location and manually try to delete the file it states that it is write protected.
I do not have root access to this system so I can not change the permissions on said file.
Here is the code, is there a way to not create a write protected file?
$leagueinfo = $this->livedraft_win_model->get_leagueinfo($sport, $leagueid);
$export = $this->my_model->get_export($sport, $leagueid);
$file_name = $leagueinfo['strat_id'] . '.IOD';
$export=strip_quotes($export);
$export = str_replace(", ",",",$export);
write_file('/tmp/' . $file_name, $export,'x+');
$data = file_get_contents('/tmp/' . $file_name, FILE_BINARY);
ob_clean();
force_download($file_name, $data);
array_map('unlink', glob("/tmp/*.IOD"));
I use the same unlink format to remove files that are uploaded to the same location and that works just fine. It is only when I try to remove the files that are created by codeigniter.
Thanks
Please try with below code.
$this->load->helper("url");
unlink(base_url('/tmp/' . $file_name));
Or try this
delete_files('/tmp/' . $file_name);
For more information please read this doc.
https://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

Using fopen() to write file into subdirectory

I already looked at this StackOverflow question and it didn't help (similarly titled).
I'm passing an image in from my Android application. If I type
$file = fopen('test.jpg', 'wb');
It works correctly and the image uploads; however, I want to allow for multiple uploads from android phones, so I want to randomize the name of the .jpg file so that I can save each new upload as a different name. I was trying this below:
$destination = time() + rand(1, 1000) . ".jpg";
$url_destination = "/project_images/" . $destination;
$file = fopen($url_destination, 'wb');
fwrite($file, $binary);
fclose($file);
It doesn't write the file to the server, however. I tried different variations of the URL there - with 'project_images/', '/project_images/', even trying the full URL (which the aforementioned StackOverflow post corrected me on), and I still can't get it to write.
The permissions of the project_images folder are set to allow files to be written to it. Any ideas?
Your problem is "/project_images" which is a wrong absolute path.
For it to work change it to "project_images/" or dirname(__FILE__).'/project_images/'.
For those who use XAMPP on Windows: use DIRECTORY_SEPARATOR instead /
dirname(__FILE__).DIRECTORY_SEPARATOR.'project_images'.DIRECTORY_SEPARATOR

using zipArchive addFile() will not add image to zip

I've been at this for a while. This actually worked one time, then never again. it simply does not create the zip file. The file does exist.
$zip = new ZipArchive();
$filename = "./test" . time() .".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
If I add something like
$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
it creates the zip with the txt file in it.. but a no go for anytype of existing file.
The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects the file and you never know what went wrong. As can be derived from the question, an alternative approach would be:
// $zip->addFile($file);
$content = file_get_contents($file);
$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);
In addition to getting the code working, file_get_contents() also generates decent error messages.
The ZipArchive::addFile() method accepts the path to the file as its first parameter.
Here, you are using :
$zip->addFile("/trash-icon.png", "/gabage.png");
Which means you are trying to add the /trash-icon.png file to your archive.
Are you sure this file exists ?
Note there is a / at the beginning of that file's path, which indicates it's an absolute path.
Maybe that / should be removed, to use a relative path ?
I had similar kind of issue and it was related with the file that I was going to add to the zip archive.
Before adding file to zip archive, it's always better to check if the file exists.
$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
if (file_exists($thisdir . "/trash-icon.png")) {
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
}

Categories