easiest way to get only folder names with .zip in php - php

I have seen a few example on the web but they seem pretty messy. I am looking for a nice and clean way to say, get me only the files/folders that have .zip on them. What I have so far is:
foreach(scandir(__DIR__) as $files) {
var_dump($files);
}
What I wonder is is if I need pre match or if the ZipArchive class has any functions that state "return only files with .zip

This should work for you, you can use glob():
<?php
foreach (glob("*.zip") as $filename) {
echo $filename . "<br />";
}
?>
possible Output:
test - Kopie.zip
test.zip
test2.zip
For more information about glob() see the manual: http://php.net/manual/en/function.glob.php

Related

Remove files which have not filename duplicates

For each document (.pdf, .txt, .docx ecc) I have also a corresponding json file with the same filename.
Example:
file1.json,
file1.pdf,
file2.json,
file2.txt,
filex.json,
filex.pdf,
But I got also some json files which are not accompanied with the corresponding document.
I want to delete all json files which have no corresponding document. Im really stucked because I cant find a proper solution to my problem.
I know how to scandir() get the filename, extensions from pathinfo() ecc. but the issue is that for each json file I find in directory I have to perform another foreach on that directory excluding all json files and see If the same filename exists or not so than I can decide to delete it. (This is how I think to solve it).
The problem here is with performance since there are millions of files and for each json I have to run a foreach on millions of files.
Can anyone guide me to a better solution?
Thank you!
Edit: Since no one will help without first posting a piece of code (and this approach in stackoverflow is definitively wrong) here is how I'm trying.:
<?php
$dir = "2000/";
$files = scandir($dir);
foreach ($files as $file) {
$fullName = pathinfo($file);
if ($fullName['extension'] === 'json') {
if (!in_array($fullName['filename'].'.pdf', $files)){
unlink($dir.$file);
}
}
}
Now as you can see I can only search only for one type of document (.pdf in this case). I want to search for every extension excluding .json and also I don't want that for each json file to run a foreach/in_array() but achieving all this in just one foreach.
Maybe you should consider it in another way? I mean, iterate through all files, and try to find corresponding files to json, if not found remove it.
It would look like follows:
$dir = "2000/";
foreach (glob($dir . "*.json") as $file) {
$file = new \SplFileInfo($dir . $file);
if (count(glob($dir . $file->getBasename('.' . $file->getExtension()) . ".*")) === 1) {
unlink($dir . $file->getFilename());
}
}
Manual
PHP: SplFileInfo
PHP: glob

Rename all files in directory using PHP and creation date

i have a problem/challenge on my Synology NAS. I have a IPcam connected which takes pictures with file names like:
00A8F700CB18()_1_20140107000224_3674.jpg
Now i would like to rename all those files to something like:
Tue 07-01-2014_11-17-26.jpg (containing date & time)
And here's the kicker: I've seen (PHP) scripts using "jhead" or "stat -c", unfortunately those are not an option on the Synology!
I cooked something up which works when i use a single file, now i would like to run this script on all the files in a directory!
Please help, i'm not an experienced PHP programmer and i take much joy in the explanation lines in the scrips, gives me and anybody who is whatching this post a learning curve ;)
The script u could use on a single file is something like this:
<?php
$stat = stat('/volume1/Ipcam/_Test/00A8F700CB18()_1_20140107000223_3673.jpg');
$motdate = ($stat['ctime']);
$newname = (gmdate("D d-m-Y_H-i-s", $motdate));
rename("/volume1/Ipcam/_Test/00A8F700CB18()_1_20140107000223_3673.jpg" . "/volume1/Ipcam/_Test/" . $newname . ".jpg");
?>
any help would be appreciated!
Read into scandir or readdir php functions.
They read a bunch of files in a specified directory and returns an array of file names.
You can then loop through these files and apply the above code to each file.
The examples on php.net are pretty easy to use and modify :)
You can use this modification of your code
<?php
$dir = '/volume1/Ipcam/_Test/';
$files = scandir($dir);
foreach($files as $file) {
$stat = stat($file);
$motdate = ($stat['ctime']);
$newname = (gmdate("D d-m-Y_H-i-s", $motdate));
rename($file, $dir . $newname . ".jpg");
}
?>

Recursively list files in an archive

I'm creating a method that will inspect files in an upload folder and some of these files are archive (tar, tar.gz, zip, rar, etc). I would like to read these archive files and list all files in a nested tree format.
For example, I have an archive file called sandwich.tar.gz, I would like it to be listed as below:-
sandwich.tar.gz
lettuce
mayonaise
cheese
bread (directory)
wholemeal
My code thus far:-
<?php $archive = new PharData('/upload/directory/sandwich.tar.gz');
foreach($archive as $file) {
echo $file . "<br />";
}
But it failed to list files inside the bread directory. How do I fix this?
You can do this using a RecursiveIteratorIterator, because PharData extends the Phar class and the Phar class extends the RecursiveDirectoryIterator class:
$archive = new PharData('/upload/directory/sandwich.tar.gz');
foreach (new RecursiveIteratorIterator($archive) as $file) {
echo $file . "<br />";
}
Although this question is rather dated, considering the stupidity and the uselessness of the previous answer, I cannot but reply.
It seems whatever PHP does is always non-standard and awkward at best...
You got it right to inspect the first level. However, the objects returned from the iteration are not of the same type, thus calling foreach on them will lead to failure.
Iterating on a PharData object gives you PharFileInfo ones. From there you can check for the file type (checking if it is a directory with the inherited isDir() method).
From there, you will need to create another PharData object on the path to the resource obtained from the (also inherited) getPathname() (sic, not getPathName() say the docs!) method. You will then be able to iterate over it...
Full example:
<?php
$archive = new PharData('/upload/directory/sandwich.tar.gz');
foreach($archive as $file) {
echo $file;
if($file->isDir()) {
echo ':';
$dir = new PharData($file->getPathname());
foreach($dir as $child) {
echo "<br />$child";
}
}
echo "<br />";
}
?>
Of course, that kills easy recursion if you have multiple levels in your archive. You will have to cook your own recursive crawler...
Some advice that I was given once, and think about often, comes to mind: code will do EXACTLY what you ask it to do: nothing more, nothing less.
Ask the machine: 'if this is a directory, please display the contents?'
Your function thinks that it is looking for a $file, and thus outputs the name of that file.
Consider http://php.net/manual/en/function.readdir.php and http://www.php.net/manual/en/function.is-dir.php

Listing files in Directory without the path

Im using the following to list the files in a directory in a intranet site I am making. The problem is that is is also listing the path to the file too, does anyone know what im doing wrong ?.
thanks :-)
<?php
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./customer-files/28734f6d045f8a5a93.18936710')) as $filename)
{
echo '<p>';
echo "$filename\n";
echo '</p>';
}
?>
You know the path you're passing, just use either:
str_replace($path,'',$filename);
or
substr($filename,strlen($path));
If you don't want ANY PATH in there, you can just get the filename with $filename->getFilename();
however, that will lead to confusion, as subdirectories won't be visible.
Surely you can just use basename()
echo basename($filename) . "\n";

Searching for specific file extensions in a folder/directory (PHP)

I'm trying to design a program in PHP that would allow me to find files with specific file extensions (example .jpg, .shp etc) in a known directory which consists of multiple folders.
Sample code, documentation or information about what methods I will be required to use will be much appreciated.
glob is pretty easy:
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
There are a few suggestions for recursive descent at the readdir page.
Take a look at PHP's SPL DirectoryIterator.
I believe PHP's glob() function is exactly what you are looking for:
http://php.net/manual/en/function.glob.php
Use readdir to get a list of files, and fnmatch to work out if it matches your required filename pattern. Do all this inside a function, and call your function when you find directories. Ask another question if you get stuck implementing this (or comment if you really have no idea where to start).
glob will get you all the files in a given directory, but not the sub directories. If you need that too, you will need to: 10. get recursive, 20. goto 10.
Here's the pseudo pseudocode:
function getFiles($pattern, $dir) {
$files = glob($dir . $pattern);
$folders = glob($dir, GLOB_ONLYDIR);
foreach ($folders as $folder) {
$files = $files + getFiles($folder);
}
return $files;
}
The above will obviously need to be tweaked to get it working, but hopefully you get the idea (remember not to follow directory links to ".." or "." or you'll be in infinite loop town).

Categories