directive local master
magic_quotes_gpc On On
magic_quotes_runtime Off Off
magic_quotes_sybase Off Off
<?php
/**
* Calls a function for every file in a folder.
*
* @author Vasil Rangelov a.k.a. boen_robot
*
* @param string $callback The function to call. It must accept one argument that is a relative filepath of the file.
* @param string $dir The directory to traverse.
* @param array $types The file types to call the function for. Leave as NULL to match all types.
* @param bool $recursive Whether to list subfolders as well.
* @param string $baseDir String to append at the beginning of every filepath that the callback will receive.
*/
function dir_walk($callback, $dir, $types = null, $recursive = false, $baseDir = '') {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
if (is_file($dir . $file)) {
if (is_array($types)) {
if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true)) {
continue;
}
}
$callback($dir,$file);
} elseif ($recursive && is_dir($dir . $file)) {
dir_walk($callback, $dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR);
}
}
closedir($dh);
}
}
function CheckFileName($dir, $file) {
// check that the filename doesn't contain an apostrophe
$new_file = $str_replace("'", "", $file);
// no unnecessary renaming of files
if (strcmp($new_file,$file)<>0) {
// rename the file
rename($dir . $file, $dir . $new_file);
}
}
dir_walk(CheckFileName, ".", null, true);
?>
<?php // RAY_temp_hibbsusan.php
error_reporting(E_ALL);
// NEED A FUNCTION LIKE THIS
// $filename = stripNonAlphaNumericCharacters($filename)
function stripNonAlphaNumericCharacters($string, $plug='_')
{
// A REGULAR EXPRESSION
$regex
= '#' // REGEX START DELIMITER
. '[' // CHARACTER CLASS DELIMITER
. '^' // NEGATION - MATCH NONE OF THESE
. 'A-Z0-9_.' // LETTERS, NUMBERS, UNDERSCORE, DOT
. DIRECTORY_SEPARATOR // CONTEXT-AWARE CONSTANT FOR THE SLASH
. ']' // CLOSING CHARACTER CLASS DELIMITER
. '#' // REGEX ENDED DELIMITER
. 'i' // CASE INSENSITIVE
;
// SEE http://php.net/manual/en/function.preg-replace.php
return preg_replace($regex, $plug, $string);
}
// SOME TEST DATA
$filenames = array
( 'ThisIsAGoodName.jpg'
, 'This has blanks.png'
, "This one's not so Good.gif"
, 'path/to/but_this_is_ok.bmp'
)
;
// TEST THE REGULAR EXPRESSION
foreach ($filenames as $f)
{
// TRANSFORM THE FILE PATH
$g = stripNonAlphaNumericCharacters($f);
// SHOW THE WORK PRODUCT
echo '<br/>';
echo $f;
echo ' BECOMES ';
echo $g;
echo PHP_EOL;
}
http://php.net/manual/en/function.stripslashes.php