林develloping與symfony1.0,我使用的是文件驗證(驗證/ upload.yml)這樣的:上傳文件驗證1
methods:
post: [logo_file]
get: [logo_file]
names:
logo_file:
required: Yes
required_msg: Please select a file to upload 23008
validators: myFileValidator
file: true
myFileValidator:
class: sfFileValidator
param:
mime_types:
- 'image/jpeg'
- 'image/png'
- 'image/gif'
- 'image/x-png'
- 'image/pjpeg'
mime_types_error: Only PNG, GIF and JPEG images files are allowed 23009
max_size: 512000
max_size_error: Max size is 512Kb 23010
所有的罰款,直至現在,但我想也對圖像名稱進行驗證,我希望在數據庫中進行stroring之前從無效字符中清除名稱?
編輯
當然不是那麼現在即時通訊使用的功能sving在DB前:
public static function generateUniqueName($fileName, $fileExtension)
{
// Create a name
$fileUniqueSuffix=PublicIdGeneratorPeer::getPublicIdForTable(self::UNIQUE_FILE_ID);
$finalFileName = $fileName.'-'.$fileUniqueSuffix.$fileExtension;
//here I want to replace or remove invalid character from $filename
return $finalFileName;
}
EDIT 2:
所以現在我有很多的名字因此我希望創建一個腳本或一種方式來傳送feald「image_name」中的所有數據,並直接在數據庫中更改所有無效字符,我的第一個想法是使用「任務」?任何想法?
EDIT-3:
所以,現在我把我的一批這樣的:
<?php
define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
define('SF_APP', 'backend');
define('SF_ENVIRONMENT', 'prod');
define('SF_DEBUG', false);
// symfony directories
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
sfContext::getInstance();
/********************************** Begin **********************************/
$criteria = new Criteria();
$listCompanyLogo = CompanyLogoPeer::doSelect($criteria);
foreach($listCompanyLogo as $CompanyLogo)
{
if(!is_null($CompanyLogo))
{
$filename= $CompanyLogo->getFileName();
$filepath=$CompanyLogo->getFilePath();
$fileurl=$CompanyLogo->getFileUrl();
$finalFileName=StringTool::stripText($filename);
$finalFilePath=StringTool::cleanUrl($filename,$filepath);
$finalFileUrl=StringTool::cleanUrl($filename,$fileurl);
$CompanyLogo->setFileName($finalFileName);
$CompanyLogo->setFilePath($finalFilePath);
$CompanyLogo->setFileUrl($finalFileUrl);
$CompanyLogo->save();
echo ' the name of logo : '.$filename.' is modified by ==============>'.$finalFileName.'<br>' ;
exit();
}
}
/********************************** End **********************************/
?>
這樣我就可以無效字符從數據庫文件的名稱更改,我的新問題是如何在他的direcotry中更改文件的名稱,我的意思是更改文件本身的文件名稱,我不知道如何?
編輯-4
因此,這裏是我的最終代碼:
批/ updatelogoName.php
<?php
define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
define('SF_APP', 'backend');
define('SF_ENVIRONMENT', 'prod');
define('SF_DEBUG', false);
// symfony directories
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
sfContext::getInstance();
/********************************** Begin **********************************/
$criteria = new Criteria();
$listCompanyLogo = CompanyLogoPeer::doSelect($criteria);
foreach($listCompanyLogo as $CompanyLogo)
{
if(!is_null($CompanyLogo))
{
$filename= $CompanyLogo->getFileName();
$filepath=$CompanyLogo->getFilePath();
$fileurl=$CompanyLogo->getFileUrl();
$thumbnailName= $CompanyLogo->getThumbnailName();
$ThumbnailPath= $CompanyLogo->getThumbnailPath();
$ThumbnailUrl= $CompanyLogo->getThumbnailUrl();
$finalFileName=StringTool::cleanName($filename);
$finalFilePath=StringTool::cleanUrl($filename,$filepath);
$finalFileUrl=StringTool::cleanUrl($filename,$fileurl);
$finalThumbnailName=StringTool::cleanName($thumbnailName);
$finalThumbnailPath=StringTool::cleanUrl($filename,$ThumbnailPath);
$finalThumbnailUrl=StringTool::cleanUrl($filename,$ThumbnailUrl);
$CompanyLogo->setFileName($finalFileName);
$CompanyLogo->setFilePath($finalFilePath);
$CompanyLogo->setFileUrl($finalFileUrl);
$CompanyLogo->setThumbnailName($finalThumbnailName);
$CompanyLogo->setThumbnailPath($finalThumbnailPath);
$CompanyLogo->setThumbnailUrl($finalThumbnailUrl);
$CompanyLogo->save();
if(rename('../web/'.$ThumbnailUrl, '../web/'.$finalThumbnailUrl) !== 'false')
{
rename('../web/'.$ThumbnailUrl, '../web/'.$finalThumbnailUrl);
}
echo 'The logo : '.$filename.' is modified by ==============>'.$finalFileName.'<br>' ;
}
}
/********************************** End **********************************/
?>
類StringTool.php:
public static function stripText($text)
{
$accFrom = array('ë','é','è','ê','Ê','Ë','É','È','à','â','á','ä','ã','å','Â','Å','À','Á','Ã','Ä','ç','Ç','Î','Ï','Ì','Í','ì','í','î','ï','Ó','Ô','Õ','Ö','ò','ó','ô','õ','ö','Ù','Ú','Û','Ü','ù','ú','û','ü');
$accTo = array('e','e','e','e','e','e','e','e','a','a','a','a','a','a','a','a','a','a','a','a','c','c','i','i','i','i','i','i','i','i','o','o','o','o','o','o','o','o','o','u','u','u','u','u','u','u','u');
$text = str_replace($accFrom,$accTo,$text);
$text = strtolower($text);
// strip all non word chars
$text = preg_replace('/\W/', ' ', $text);
// replace all white space sections with a dash
$text = preg_replace('/\ +/', '-', $text);
// trim dashes
$text = preg_replace('/\-$/', '', $text);
$text = preg_replace('/^\-/', '', $text);
return $text;
}
/*
* Remove extnesion File
*
*/
public static function RemoveExtension($fileName)
{
$extension = strrchr($fileName, '.');
if($extension !== false)
{
$fileName = substr($fileName, 0, -strlen($extension));
}
return $fileName;
}
/*
* Remove all non alpha-numeric characters from URLs and Files
*
*/
public static function cleanName ($fileName)
{
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$extremove = self::RemoveExtension($fileName);
$result = self::stripText($extremove);
$finaleFileName= $result.'.'.$extension;
return $finaleFileName;
}
public static function cleanUrl($fileName,$filePath)
{
$finaleFileName = self::cleanName($fileName);
$finaleFilePath=str_replace($fileName, $finaleFileName, $filePath);
return $finaleFilePath;
}
如果您看到任何錯誤或優化告訴我...
你肯定你的陽明的缺口? – j0k 2012-08-08 14:31:08
@ j0k我編輯我的第一條消息,你看到我的答案... – Nll 2012-08-08 15:18:45
那麼你爲什麼不在'generateUniqueName'中進行消毒? – j0k 2012-08-08 15:26:01