So I found this class on the internet that can put files into a zip.
I have a form where I can upload pictures into a folder, then I tried to loop into these files and store them into a zip file but seems I failed somewhere.
zipper class
<?php
class zipper {
private $_files = array(),
$_zip;
public function __construct() {
$this->_zip = new ZipArchive;
}
public function add($input){
if(is_array($input)){
$this->_files = array_merge($this->_files, $input);
}else{
$this->_files[] = $input;
}
}
public function store($location = null){
if(count($this->_files) && $location){
foreach ($this->_files as $index => $file) {
if(!file_exists($file)){
unset($this->_files[$index]);
}
print_r($file . "<br>");// here gives me the exact path of the files.
}
if($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach ($this->_files as $file) {
$this->_zip->addFile($file, $file);
}
$this->_zip->close();
}
}
}
}
and this is my uploading file
> $uniqUser = uniqid(strtoupper($userName). "-" , true);
> $directory ='../upload/';
> $file_name is $files['name']
if(!is_dir("../upload/". $uniqUser ."/")) {
mkdir("../upload/". $uniqUser ."/");
}
if(move_uploaded_file($file_tmp, $directory . $uniqUser . "/" . $file_name)){
$uploaded[$position] = $directory . $uniqUser . "/" . $file_name;
$zipper = new zipper;
$zipper->add(BASE_URL . "/upload/" . $uniqUser . "/" . $file_name);
$zipper->store(BASE_URL . "/upload/" . $uniqUser . ".zip");
the last code is inside a foreach that loops into the files uploaded.
I don't know why I fail in creating the archive at the end ...
Could be an easy way to turn these new created folders into a zip or add them into a zip ?
First problem was because I added BASE_URL I had to write ../upload/.
Second problem was kinda stupid ...
I had to create the class outside the foreach $zipper = new zipper; and $zipper-store("../upload/" . $uniqUser . ".zip");for unnecessary repeating (added it at the bottom).
Related
this is the 1st time I posted on StackOverflow. I need to create a zip file of multiple images. I've tried Zipper and also ZipArchive and my code still fails.
$zip = new \ZipArchive();
foreach ($students as $student) {
$download = 'album' . $student->album_id . '.zip';
if ($zip->open(public_path('albums/' . $student->album_id . '/' . $download), \ZipArchive::CREATE) === TRUE) {
$files = Storage::allFiles('public/albums/' . $student->album_id . '/image_files');
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
}
I can assure that all the images exist. I put the images in Storage/app/public/albums/$student->album_id/image_files/. Please help me.
First, you can add an additional step to verify that your file exists by using PHP built in function: file_exists()
Most times, if you your files do noT exists, no files will be added to the zip, and your code will run without throwing any error but still will not work.
What you're passing to addFile() is not a correct path. addFile() needs an absolute path to where your file is stored. You will need to add this line $file_path = storage_path("app/{$file}");
See code below:
$zip = new \ZipArchive();
foreach ($students as $student) {
$download = 'album' . $student->album_id . '.zip';
if ($zip->open(public_path('albums/' . $student->album_id . '/' . $download), \ZipArchive::CREATE) === TRUE) {
$files = Storage::allFiles('public/albums/' . $student->album_id . '/image_files');
foreach ($files as $file) {
$file_path = storage_path("app/{$file}");
if (file_exists($file_path)) {
$zip->addFile($filepath);
}
}
$zip->close();
}
}
Optionally, if you wish to download any of the zipped files, you can return the download response at the end of your code to download:
return response()->download(public_path('albums/' . $student->album_id . '/' . $download));
I have upload form where I'm able to upload any type of files and save their path to database. Without saving any relevant information in database such as: filetype, filesize etc can I get this information while I loop the results and showing them on page?
Here is what I tried so far
public function fileUpload()
{
$allFiles = Documents::paginate(20);
$files = array();
foreach ($allFiles as $file) {
$files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file));
}
return View::make('admin.files', [
'files' => $files
]);
}
public function fileInfo($filePath)
{
$file = array();
$file['name'] = $filePath['filename'];
$file['extension'] = $filePath['extension'];
$file['size'] = filesize($filePath['dirname'] . '/' . $filePath['basename']);
return $file;
}
The error which I get when I run the page is
ErrorException: filesize(): stat failed for .....
Is this the correct way of doing this?
Update: dd($filePath['dirname'] . '/' . $filePath['basename']); return
string(126) "/var/www/html/site/public/uploads/{"id":2,"document_path":"aut6MnADFrPZTz4TJf0Y.pdf","document_name":"Some title for the file"}"
Change this line
$files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file));
to
$files[] = $this->fileInfo(pathinfo(public_path() . '/uploads/' . $file->document_path));
Here is my code
$file = 'post.php';
$root = '/' . $dir_auth1 . '/'. $file;
$folder = mkdir(rand(10,10000));
$folder5 = $folder . '/' . $file;
echo $folder5;
if($folder) {
if (!copy($root, $folder5)) {
echo "failed to copy $file...\n";
} else {
echo "<p style='font-size:35px;font-family:verdana;text-align:center;'>status was successfuly created.</p>";
}
}
Basically what I am trying to do is upon a form submit create a directory with random digits and place the $file variable inside of the randomized directory
mkdir - return bool. Please read about mkdir
And rewrite you code something like this:
$file = 'post.php';
$root = '/' . $dir_auth1 . '/'. $file;
$folder = rand(10,10000);
mkdir($folder);
$folder5 = $folder . '/' . $file;
And check you if. You always true. (not empty string = true)
So I found this class on the internet that can put files into a zip.
I have a form where I can upload pictures into a folder, but when I try to add them into the zip I could add only the last file from that folder (loop).
zipper class
<?php
class zipper {
private $_files = array(),
$_zip;
public function __construct() {
$this->_zip = new ZipArchive;
}
public function add($input){
if(is_array($input)){
$this->_files = array_merge($this->_files, $input);
}else{
$this->_files[] = $input;
}
}
public function store($location = null){
if(count($this->_files) && $location){
foreach ($this->_files as $index => $file) {
if(!file_exists($file)){
unset($this->_files[$index]);
}
print_r($file . "<br>");// here gives me the exact path of the files.
}
if($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach ($this->_files as $file) {
$this->_zip->addFile($file, $file);
}
$this->_zip->close();
}
}
}
}
and this is my uploading file
> $uniqUser = uniqid(strtoupper($userName). "-" , true);
> $directory ='../upload/';
> $file_name is $files['name']
if(!is_dir("../upload/". $uniqUser ."/")) {
mkdir("../upload/". $uniqUser ."/");
}
if(move_uploaded_file($file_tmp, $directory . $uniqUser . "/" . $file_name)){
$uploaded[$position] = $directory . $uniqUser . "/" . $file_name;
$zipper = new zipper;
$zipper->add(BASE_URL . "/upload/" . $uniqUser . "/" . $file_name);
$zipper->store(BASE_URL . "/upload/" . $uniqUser . ".zip");
the last code is inside a foreach that loops into the files uploaded.
When I did a print_r("Added file:" . $file . "<br>"); to see if the all files are added, I get a positive response. But I always get the last file from the folder.
Thank you
EDIT
Well, Guess the problem is after each loop to add the files in the folder, the zip class adds the file into the zip, then after checks if the zip exists or no ... So after each loop the zip is overwritten and adds only the last file.
How can I make this better ?
The problem was kinda stupid ...
I had to create the class outside the foreach $zipper = new zipper; and $zipper-store("../upload/" . $uniqUser . ".zip");for unnecessary repeating (added it at the bottom).
How can I delete all folders and files except a specific folder?
uploaded->
. folder_A->
. . folder_A1 //empty folder
. . folder_A2 //full folder
. . img.png // a file
. .
. folder_B //empty
. .
. folder_c->
. . folder_c1 //empty folder
. . file.doc // a file
. .
I want remove all folders and file in it inside "uploaded" folder except a specific folder that I determined.
For example I want remove all folders and files except folder_c
you should try like this
function Delete($path)
{
if ((is_dir($path) === true) && ($path!='folder_c'))
{
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
Delete(realpath($path) . '/' . $file);
}
return rmdir($path);
}
else if (is_file($path) === true)
{
return unlink($path);
}
return false;
}
Hope this helps.