Rename all files in directory using PHP and creation date - php

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");
}
?>

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

How to add parent dir variable to scandir (PHP)

Hi sorry this is probably a really newbie question, but I'm searching for the answer for 2 days now and no luck.
I'm using a script what generates an image gallery from an uploaded folder.
Script info:
Tim-thumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks.
You need to add the directory name what contains the images to the script like:
$dirname = "images/" ;
$images = scandir($dirname);
shuffle($images);
What I'm trying to do is make the script work automatically if i upload it in a new folder with new images so I don't need to add the $dirname every time I upload a gallery.
for example:
$dirname = $mydir ;
where mydir returns the path of the current directory like:
$mydir = basename(getcwd()) . DIRECTORY_SEPARATOR ;
but it's not working.
also tried to make it work from a function:
function current_dir()
{$path = dirname($_SERVER[PHP_SELF]);$position = strrpos($path,'/') + 1;print substr($path,$position);}
than -->
$dirname = current_dir() ;
but no luck.
I think I'm missing something here, I'm a totally noob and maybe it's just a syntax issue but can't make it work.
I always get
[function.scandir]: failed to open dir ...
or creates the gallery but the images not working (I see only alt tags)
thank you for any help.
>>>>>>>>>>>>>>>>>>>
EDIT :
!!!
Just realized... :o !!!
The answer is:
$dirname = "./" ;
omg
<<<<<<<<<<<<<<<<<<<
"I think I'm missing something here..."
:D
Try
$parent = dirname(__DIR__);
http://php.net/manual/en/language.constants.predefined.php

get name of single file in a directory using php

Although I can find plenty of examples of listing all of the files in a directory
$dir = '../upload/'.$id.'/ask_temp';
But I need to get the name of a single file so I can store it in a variable to use elsewhere.
There is and only ever will be one file in there.
In regards to ComFreek's answer, make sure to filter out any directory. (. and ..)
I'm assuming you're using at least PHP 5.3
$files = array_filter(scandir($dir), function($val) use($dir)
{
return is_file($dir.'/'.$val);
});
$myFile = $files[0];
Another way is to this, this is probably easier. (first 2 are always '.' and '..')
$files = scandir($dir);
$myFile = $files[2];

Search for file in folder using php

I am storing user uploaded files like pdf images and txt files in separate folders using my php script i want to retrieve the file names from the folder upload and give the pdf and txt in a group and also way to search for specific file.
I also need to rename the file before to $ja variable
$ja
$da = date("dmY");
$ja = $uid.$da;
move_uploaded_file($mi, $uploadpath)
also used this code which i found in stack
Example 01:
<?php
// read all files inside the given directory
// limited to a specific file extension
$files = glob("./ABC/*.txt");
?>
Example 02:
<?php
// perform actions for each file found
foreach (glob("./ABC/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
the scandir(); function will help you
<?php
$dir = '/tmp';
$files1 = scandir($dir);
print_r($files1);
?>
http://www.php.net/manual/en/function.scandir.php
now you have array of all files in a location you specified you can use array functions to get your work done
you can try listing the directory with scandir, and then filter as you want in the php array of filenames you will get

Move File starting with A-D and E-G and H-L and M-P and Q-T and U-W and X-Z

I have millions of file in a Folder its difficult to read with script so i need to store file in separate folder. just like
Move File to A-D and E-G and H-L and M-P and Q-T and U-W and X-Z with there starting character..
I try to use FTP but million so file did not show in directory correctly (took very long time to display and for moving, some time FTP stuck), also try cpanel etc. but no luck.
if there is a solution with PHP script please let me know, its help me alot.
Well, you could use DirectoryIterator and move the files based on the filename. I'd still rather recommend a more direct approach with SSH or similar (there are things PHP wasn't meant to do - like non-website things); this approach is Remote Client --request--> Web Server --access--> File System.
Other than that, something like
$dirname = dirname(__FILE__);
$iterator = new DirectoryIterator($dirname);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile() && $fileinfo->isWritable()) {
$fname = $fileinfo->getFilename();
rename($dirname. DIRECTORY_SEPARATOR .$fname, $dirname. DIRECTORY_SEPARATOR .ucfirst(substr($fname, 0, 1)). DIRECTORY_SEPARATOR .$fname);
}
}
Should move the files into a subdirectory with the same letter as the first character in the filename. If you want to move based on category groups of characters, just put the rename() call into a switch statement.

Categories