Frage

Ich verwende derzeit Symfony 1.4 und möchte Benutzern ermöglichen, Microsoft Word -DOCX -Dateien hochzuladen. Verwenden des SFWIDGetFormputFile -Widgets und der SFValidatorFile unten können Benutzer ihre DOCX -Dateien mithilfe eines einfachen Webformulars auswählen und erfolgreich hochladen.

$this->widgetSchema['file_name'] = new sfWidgetFormInputFile(array('label' => 'File'));

$this->validatorSchema['file_name'] = new sfValidatorFile(array(
  'required'   => true,
  'path'       => sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_dir_file_sharing').DIRECTORY_SEPARATOR,
  'mime_types' => array('application/msword',
                        'application/vnd.ms-word',
                        'application/msword',
                        'application/msword; charset=binary')
), array(
    'invalid'    => 'Invalid file.',
    'required'   => 'Select a file to upload.',
    'mime_types' => 'The file must be a supported type.'
));

Das Problem ist, dass nach dem Hochladen der Datei die Erweiterung in .zip geändert wird und die Datei einen Dateibaum von XML -Dateien enthält. Mein Verständnis ist, dass dies daran liegt, dass Office 2007 jetzt offene XML -Dateiformate verwendet. Gibt es eine Möglichkeit, dies zu verhindern, dass dies mit Symfony oder PHP passiert?

War es hilfreich?

Lösung 2

Symfony 1.3+ hat eine Option mime_type_guessers zum sfvalidatorfile Auf diese Weise können Sie Ihren eigenen Mime -Guesser -PHP -Callable definieren oder einen Build in Guesser verwenden. Das Aufrufen eines der 3 integrierten MIME-Gäste findet den richtigen Dateityp für DOCX und behält die DOCX-Dateierweiterung.

Hier ist der aktualisierte Code mit RatefromFileInfo:

$this->validatorSchema['file_name'] = new sfValidatorFile(array(
'required'   => true,
'path'       => sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_dir_file_sharing').DIRECTORY_SEPARATOR,
'mime_type_guessers' => array('guessFromFileinfo'),
'mime_types' => array('application/msword',
                    'application/vnd.ms-word',
                    'application/msword',
                    'application/msword; charset=binary')
), array(
    'invalid'    => 'Invalid file.',
    'required'   => 'Select a file to upload.',
    'mime_types' => 'The file must be a supported type.'
));

Andere Tipps

Das Problem ist Inhaltsniffing. Die neuen Office-Formate sind .zip-Dateien, und wenn das Upload beim Upload geschnüffelt ist, wird der Browser dies als ZIP-Datei identifiziert und den Header vom Typ Inhalt als solcher festgelegt. In ähnlicher Weise wird beim Download, es sei denn, Ihr Server legt den richtigen HTTP-Antwortheader vom inhaltlichen HTTP-Antwort fest, der Browser wird davon ausgegangen, dass es sich um eine ZIP-Datei handelt.

Es scheint ein zu sein Insekt In der Dateityp -Erkennung von Symfony. Eine Problemumgehung wird beschrieben.

Die vorgeschlagene Verwendung von mime_type_guessers Verwendet eine nicht existierende Funktion. Wenn Sie die Methode Sfvalidatorfile verwenden möchten, sollten Sie schreiben array(array('sfValidatorFile', 'guessFromFileinfo')). Die vorgeschlagene Lösung verwendet überhaupt keine MIME-Erkennung und führt zu Problemen mit dem klassischen Excel-Format auf meinem System.

Ich habe das Problem behoben, indem ich die SfvalidatorFile -Klasse erweiterte und die GetMimetype -Methode geändert habe.

Erstellen Sie eine neue msvalidatorfile.class.php -Datei in Ihrem Lib -Ordner:

<?php

class msValidatorFile extends sfValidatorFile
{
  protected function getMimeType($file, $fallback)
  {
    $arrayZips = array( "application/zip", 
                        "application/x-zip", 
                        "application/x-zip-compressed");
    $officeTypes = array(
        "application/vnd.ms-word.document.macroEnabled.12",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
        "application/vnd.openxmlformats-officedocument.wordprocessingml.template", 
        "application/vnd.ms-powerpoint.template.macroEnabled.12", 
        "application/vnd.openxmlformats-officedocument.presentationml.template", 
        "application/vnd.ms-powerpoint.addin.macroEnabled.12", 
        "application/vnd.ms-powerpoint.slideshow.macroEnabled.12", 
        "application/vnd.openxmlformats-officedocument.presentationml.slideshow", 
        "application/vnd.ms-powerpoint.presentation.macroEnabled.12", 
        "application/vnd.openxmlformats-officedocument.presentationml.presentation", 
        "application/vnd.ms-excel.addin.macroEnabled.12", 
        "application/vnd.ms-excel.sheet.binary.macroEnabled.12", 
        "application/vnd.ms-excel.sheet.macroEnabled.12", 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
        "application/vnd.ms-excel.template.macroEnabled.12", 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.template");

    foreach ($this->getOption('mime_type_guessers') as $method)
    {
      $type = call_user_func($method, $file);

      if (null !== $type && $type !== false)
      {
        if (in_array($type, $arrayZips) && in_array($fallback, $officeTypes))
        {
           return $fallback;
        }
        return strtolower($type);
      }
    }

    return strtolower($fallback);
  }
}

Verwenden Sie diesen neuen Validator in Ihrem Formularcode:

$this->validatorSchema['file'] = 
    new msValidatorFile(array('required' => false,
                              'path' => sfConfig::get('sf_upload_dir')
                        ));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top