Stopping symfony from screwing up uploaded files names - PART 2
Datum: 30.09.2010 19:17:32After using my last solution for some time i came up with some improvements. Allowing all chars can be a security and usability-problem, so I switched to use some methods Doctrine uses for its slugs now.
Currently I am still looking for a solution to extend it with some utf-8 support for japanese chars, so they won't be removed. The current solution won't work, it seems that there is no easy way to exclude all kanji from beeing replaced with regex and still replacing umlaute and chars with accents.
class myValidatedFile extends sfValidatedFile
{
/**
* Generates a non random filename
*
* @return string A non random name to represent the current file
*/
public function generateFilename()
{
$ext = strtolower($this->getExtension($this->getOriginalExtension()));
$name = self::urlize(substr($this->getOriginalName(), 0, - strlen($ext)));
$filename = self::urlize($name . $ext);
$i = 1;
while (file_exists($this->getPath() . '/' . $filename))
{
$filename = self::urlize($name . '-' . $i . $ext);
$i++;
}
return $filename;
}
/**
* based on Doctrine_Inflector::urlize (without strtolower)
*/
public function urlize($text)
{
// Remove all non url friendly characters with the unaccent function
$text = Doctrine_Inflector::unaccent($text);
// Remove all none word characters
$text = preg_replace('/[^\w\.\_\-]/', ' ', $text);
$text = str_replace(' ', '-', $text);
return trim($text, '-');
}
}
Trackbacks (0)
Trackbackurl: http://www.robo47.net/trackback/blogentry/206Es sind keine Trackbacks vorhanden.
You liked it ? Link it on your homepage or blog:



Benjamin Steininger ist Webentwickler auf der Suche nach einem neuen Job und
photographiert sehr gerne. Er beschäftigt sich viel mit dem Internet, PHP, Symfony, Testing und hat einen
Kommentare (0)
Es sind noch keine Kommentare vorhanden.
Die Kommentare zu diesem Beitrag sind gesperrt.