Skip to content

Instantly share code, notes, and snippets.

@otkrsk
Last active June 12, 2025 02:20
Show Gist options
  • Save otkrsk/5727740 to your computer and use it in GitHub Desktop.
Save otkrsk/5727740 to your computer and use it in GitHub Desktop.
Some PHP code snippets to manipulate directories and files.
<?php
$source = "/path/to/your/file/";
$files = scandir("$source");
foreach($files as $file) {
// inside this loop, we strip the information from the CSV file
$arrLines = file($source . $file);
foreach($arrLines as $lines) {
// display each element
$eemport = explode(',', $lines);
// foreach ($eemport as $value) {
// echo $value . "\t";
// }
$manuel = strtolower($eemport[4]);
$ma = substr($manuel, 0, 2);
}
}
# delete the file
unlink($source.$file)
/**
* Use this loop if you want to ignore the '.' and '..' in the diretory
*/
foreach ($files as $file) {
if($file != '.' && $file != '..') {
$file_info = pathinfo($file);
$file_ext = $file_info['extension'];
if($file_ext == 'jpg') {
$new_name = 'owsm_rakuunz.' . $file_ext;
rename($source.$file, $source.$new_name);
echo $new_name . ' modified on ' . date('Y-m-d H:i:s', filemtime($source.$new_name)) . '<br>';
// filemtime function is used to display the time the file last modified
} else {
echo $file . ' modified on ' . date('Y-m-d H:i:s', filemtime($source.$file)) . '<br>';
}
}
}
// Another way to skip files and dots is to declare an array of unwanted files.
$ignored = array('.', '..', '.svn', '.htaccess');
// then skip if with this:
if(in_array($file, $ignored)) continue;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment