0

我正在使用 symfony1.0 开发,我正在使用这样的文件验证(validate/upload.yml):

    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

到目前为止一切都很好,但我还想对图像名称进行验证,我希望在输入数据库之前从无效字符中清除名称?

编辑

当然不是这样,现在我在 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;
}

编辑-2

所以现在我有很多图像名称存储在数据库中,带有invlaid charater,所以我希望创建一个脚本或一种方法来传输feald“image_name”中的所有数据并直接更改数据库中的所有无效字符,我的第一个想法是使用“任务”?!任何想法?

编辑-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 **********************************/
       ?>

所以我可以从数据库中的文件名中更改无效字符,我的新问题是如何更改他的目录中的文件名,我的意思是更改文件本身的文件名,我不知道如何?

编辑-4

所以这是我的最终代码:

批处理/更新logoName.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;
        }

如果您看到任何错误或优化,请告诉我...

4

1 回答 1

2

您可以generateUniqueName使用 striptext 方法清理函数中的文件名。例如,在Askeet中使用的那个:

<?php

class myTools
{
  public static function stripText($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;
  }
}

然后:

public static function generateUniqueName($fileName, $fileExtension)
{
    // Create a name 
    $fileUniqueSuffix = PublicIdGeneratorPeer::getPublicIdForTable(self::UNIQUE_FILE_ID);
    $finalFileName    = myTools::stripText($fileName).'-'.$fileUniqueSuffix.$fileExtension;

    return $finalFileName;
}

编辑

对于您的第二个请求,您应该创建一个任务。其实它是一个任务,但是在sf1.0中它被称为批处理。请参阅此处的简短文档。基本上:

  • 您获取所有图像表
  • 你清理所有名字
  • 您用新名称重命名磁盘上的所有图像

编辑2:

如果您对 utf8 有疑问,您应该urlizeDoctrine_Inflector进行检查。

然后使用:

$finalFileName    = Doctrine_Inflector::urlize($fileName).'-'.$fileUniqueSuffix.$fileExtension;
于 2012-08-08T15:32:18.720 回答