fixing oc-375 - a number is appended tp the filename

This commit is contained in:
Thomas Müller 2012-04-15 16:59:39 +02:00
parent 0b426b5e64
commit a0d917fe98
2 changed files with 30 additions and 1 deletions

View File

@ -47,7 +47,8 @@ $result=array();
if(strpos($dir,'..') === false){
$fileCount=count($files['name']);
for($i=0;$i<$fileCount;$i++){
$target=stripslashes($dir) . $files['name'][$i];
// $target=stripslashes($dir) . $files['name'][$i];
$target = OC_Helper::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
if(is_uploaded_file($files['tmp_name'][$i]) and OC_Filesystem::fromTmpFile($files['tmp_name'][$i],$target)){
$meta=OC_FileCache::getCached($target);
$result[]=array( "status" => "success", 'mime'=>$meta['mimetype'],'size'=>$meta['size'],'name'=>$files['name'][$i]);

View File

@ -492,4 +492,32 @@ class OC_Helper {
}
}
}
/**
* Adds a suffix to the name in case the file exists
*
* @param $path
* @param $filename
* @return string
*/
public static function buildNotExistingFileName($path, $filename)
{
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path . '/' . $filename;
$newname = $filename;
$counter = 2;
while (OC_Filesystem::file_exists($newpath)) {
$newname = $name . ' (' . $counter . ')' . $ext;
$newpath = $path . '/' . $newname;
$counter++;
}
return $newname;
}
}