Skip to content

Instantly share code, notes, and snippets.

@mustafauysal
Last active December 17, 2015 02:49
Show Gist options
  • Select an option

  • Save mustafauysal/5539128 to your computer and use it in GitHub Desktop.

Select an option

Save mustafauysal/5539128 to your computer and use it in GitHub Desktop.
If file exist in the path, rename it
<?php
/**
* Create a better name for file
* @param mixed $context
* @return type $new_text
*/
function rename_file($context) {
$context = trim($context);
$search = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
$replace = array('c','c','g','g','i','i','o','o','s','s','u','u','-');
$new_text = str_replace($search,$replace,$context);
return strtolower($new_text);
}
/**
* If file exists in there append number. // filename_0.jpg
* @param type $path
* @param type $filename
* @link http://css-tricks.com/snippets/php/check-if-file-exists-append-number-to-name/
* @return strıng
*/
function rename_exists_file($path, $filename){
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path.'/'.$filename;
$newname = $filename;
$counter = 0;
while (file_exists($newpath)) {
$newname = $name .'_'. $counter . $ext;
$newpath = $path.'/'.$newname;
$counter++;
}
return $newpath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment