I have a folder containing many PDF files. I have built a script that zip these pdf files into 100mb batches each.
#!/usr/bin/php
<?php
$pathToFiles = "../pdffakturor_test/";
$maxFileSize = 100 * 1024 * 1024;
$counter = 1;
$currentsize = 0;
$created_at_datum = date("Ymd");
$created_at_clock = date("Hi");
$zip = new ZipArchive;
if($counter <= 10)
{
$counter = sprintf("%02s", $counter);
}
$zip->open('PROD_SE_C_S_E_'.$created_at_datum.'_'.$created_at_clock.$counter.'.zip', ZipArchive::CREATE);
if ($handle = opendir($pathToFiles))
{
while (false !== ($entry = readdir($handle)))
{
if (substr($entry, -4) == ".pdf")
{
$filesize = filesize($pathToFiles.$entry);
if($currentsize >= $maxFileSize)
{
$zip->close();
$zip = null;
$zip = new ZipArchive;
$currentsize = 0;
if($counter <= 10)
{
$counter = sprintf("%02s", $counter);
}
$zip->open('PROD_SE_C_S_E_'.$created_at_datum.'_'.$created_at_clock.$counter.'.zip', ZipArchive::CREATE);
$counter++;
}
$zip->addFile($pathToFiles.$entry, $entry);
$currentsize += $filesize;
}
}
closedir($handle);
}
?>
The problem I have is that the first zip batch becomes 183mb and the others 91,6mb. I can't figure out why the first becomes 183mb?
Related
I am trying to import 6 files which are in zip files. First I extract those files after that I want to get all the data in these files. But currently I am getting only the first file data. The script had not read the second file. I don't understand how to get rid from this problem.
Here is my code.
<?php
if ($_FILES) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array(
'application/zip',
'application/x-zip-compressed',
'multipart/x-zip',
'application/x-compressed'
);
foreach ($accepted_types as $mime_type) {
if ($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if (!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = "zip/" . $filename;
if (move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
$col = array();
if ($x === true) {
for ($x = 0; $x < $zip->numFiles; $x++) {
$csv = $zip->getNameIndex($x);
$zip->extractTo("zip/");
$csv_path = "zip/" . $csv;
if (($handle = fopen($csv_path, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
echo "<pre>";
print_r($col);
}
fclose($handle);
}
}
$zip->close();
unlink($target_path);
exit;
}
$message = "Your .zip file was uploaded and unpacked.";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
?>
Any help would be Highly appreciated.
Look at this part of your code...
<?php
// ...code...
$zip->extractTo("zip/");
$csv_path = "zip/" . $csv;
if (($handle = fopen($csv_path, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// ...code...
?>
extractTo() extracts the files. There are six files, you said. Then you do fopen(), and you do it once. You want to do that fopen() on each of the files.
What you'll want is...
<?php
// ...code... (files are extracted at this point)
$files = files('zip/');
for($i = 0; i < count($files); $i++) {
$file = $files[$i];
// ...do csv stuff here for $file
}
// ...code...
?>
i am using this piece of code to unzip a .zip file
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Lets say: there is a .php file in the .zip and i do not want a .php file to be extracted. How can i prevent that?
You can try something like this for PHP >= 5.5:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
if( pathinfo($zip->getNameIndex($i)['extension'] != "php")){
$zip->extractTo('/my/destination/dir/', $zip->getNameIndex($i));
}
}
$zip->close();
}
Or this for PHP < 5.5:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
if( $ext != "php")){
$zip->extractTo('/my/destination/dir/', $zip->getNameIndex($i));
}
}
$zip->close();
}
The only difference between the two is the pathinfo function. Both will loop all files inside the zip file and, if the file extension isn't php, extracts it to /my/destination/dir/.
$zip= new ZipArchive;
if($zip->open('test.zip') === TRUE){
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = pathinfo($zip->getNameIndex($i));
$fileinfo = $filename['extension'];
if($fileinfo!="php"){
$zip->extractTo('extract/',$zip->getNameIndex($i));
}
$zip->close();
}
i have this php code to list all files in a directory and output filesize and a download link.
<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . #$size[$factor];
}
$excludedFiles = array('.','..');
$excludedExtensions = array ('html','htm','php');
// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
// Define the full path to your folder from root
$dir = "./";
// Open the folder
$dir_handle = #opendir($dir) or die("Unable to open $dir");
// Loop through the files
while ($file = readdir($dir_handle)) {
$extn = explode('.',$file);
$extn = array_pop($extn);
if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){
if($file == "." || $file == ".." )
continue;
echo "<tr>
<td>
<a class='Testo' href=\"$file\" download>$file</a></td>
<td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
</tr>";
}
}
// Close
closedir($dir_handle);
?>
i wanted the list to be alphabetically ordered so i added
$files = scandir($dir);
after the $dir line and
foreach ($files as $file){
after the while ($file = readdir($dir_handle)) { line
and a
}
before the closedir($dir_handle); line
now the files list in alphabetical order, but the list is endless. the list starts over and over, like a loop.
What am I doing wrong? Is this the correct way to accomplish this?
Any help would be much appreciated.
Thanks!
You can put your folder in an array and sort it with the php sort function. Then print them :
<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . #$size[$factor];
}
$excludedFiles = array('.','..');
$arrayFiles = array();
$excludedExtensions = array ('html','htm','php');
// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
// Define the full path to your folder from root
$dir = "./";
// Open the folder
$dir_handle = #opendir($dir) or die("Unable to open $dir");
// Loop through the files
while ($file = readdir($dir_handle)) {
$extn = explode('.',$file);
$extn = array_pop($extn);
if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){
if($file == "." || $file == ".." )
continue;
$arrayFiles[] = $file;
}
} // dunno what are these } so i put my loop after
// Close
closedir($dir_handle);
sort($arrayFiles);
foreach ($arrayFiles as $file) {
echo "<tr>
<td>
<a class='Testo' href=\"$file\" download>$file</a></td>
<td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
</tr>";
}
?>
How do we return the absolute path of largest file in a particular directory?
I've been fishing around and haven't turned up anything concrete?
I'm thinking it has something to do with glob()?
$sz = 0;
$dir = '/tmp'; // will find largest for `/tmp`
if ($handle = opendir($dir)) { // will iterate through $dir
while (false !== ($entry = readdir($handle))) {
if(($curr = filesize($dir . '/' . $entry)) > $sz) { // found larger!
$sz = $curr;
$name = $entry;
}
}
}
echo $dir . '/' . $name; // largest
$dir = 'DIR_NAME';
$max_filesize = 0;
$path= '';
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($dir),FilesystemIterator::SKIP_DOTS)) as $file){
if ($file->getSize() >= $max_filesize){
$max_filesize = $file->getSize();
$path = $file->getRealPath(); // get absolute path
}
}
echo $path;
function getFileSize($directory) {
$files = array();
foreach (glob($directory. '*.*') as $file) {
$files[] = array('path' => $file, 'size' => filesize($file));
}
return $files;
}
function getMaxFile($files) {
$maxSize = 0;
$maxIndex = -1;
for ($i = 0; $i < count($files); $i++) {
if ($files[$i]['size'] > $maxSize) {
$maxSize = max($maxSize, $files[$i]['size']);
$maxIndex = $i;
}
}
return $maxIndex;
}
usage:
$dir = '/some/path';
$files = getFileSize($dir);
echo '<pre>';
print_r($files);
echo '</pre>';
$maxIndex = getMaxFile($files);
var_dump($files[$maxIndex]);
What is the best way to get the size of a directory in PHP? I'm looking for a lightweight way to do this since the directories I'll use this for are pretty huge.
There already was a question about this on SO, but it's three years old and the solutions are outdated.(Nowadays fopen is disabled for security reasons.)
Is the RecursiveDirectoryIterator available to you?
$bytes = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $i)
{
$bytes += $i->getSize();
}
You could try the execution operator with the unix command du:
$output = du -s $folder;
FROM: http://www.darian-brown.com/get-php-directory-size/
Or write a custom function to total the filesize of all the files in the directory:
function getDirectorySize($path)
{
$totalsize = 0;
$totalcount = 0;
$dircount = 0;
if($handle = opendir($path))
{
while (false !== ($file = readdir($handle)))
{
$nextpath = $path . '/' . $file;
if($file != '.' && $file != '..' && !is_link ($nextpath))
{
if(is_dir($nextpath))
{
$dircount++;
$result = getDirectorySize($nextpath);
$totalsize += $result['size'];
$totalcount += $result['count'];
$dircount += $result['dircount'];
}
else if(is_file ($nextpath))
{
$totalsize += filesize ($nextpath);
$totalcount++;
}
}
}
}
closedir($handle);
$total['size'] = $totalsize;
$total['count'] = $totalcount;
$total['dircount'] = $dircount;
return $total;
}