2017-08-04 5 views
0

私はPHP用のCKFinder 3.4で作業しています。すでにサーバーに存在するファイルをアップロードするときに自動的に名前を変更する形式を変更する方法を探しています。標準フォーマットは、file.extからfile(1).extに名前を変更することです。しかし、私はそれがよりを更新しますので、ソースファイルを変更しないようしたいと思いますプラグインを使用してCKFinderの自動リネーム形式を変更することはできますか?

/core/connector/php/vendor/cksource/ckfinder/src/CKSource/CKFinder/Filesystem/File/File.php

:私はfile-1.ext.

で簡単に十分ckfinderのソースに機能を変更することが可能であるにこれを変更する必要があります難しい

最適なのは、プラグインを使用して問題を解決したいのですが、BEFORE_COMMAND_FILE_UPLOADイベントには名前が変更された(自動)ファイル名が含まれていないようです。

同様の問題に取り組み、解決策を見つけた人は誰ですか?

答えて

0

私は、説明した方法でファイルの名前を変更する小さなコネクタプラグインを用意しました。残念なことに、私は小さなハックなしにこの結果を達成できませんでした。クローズをバインドしてプライベートオブジェクトプロパティにアクセスしました。

CKFinder 3 PHP Connectorプラグインの詳細については、plugins docsをご覧ください。

はここでプラグインだ、私はあなたがそれが役に立つことを願っています:

<?php 

namespace CKSource\CKFinder\Plugin\CustomAutorename; 

use CKSource\CKFinder\CKFinder; 
use CKSource\CKFinder\Event\BeforeCommandEvent; 
use CKSource\CKFinder\Event\CKFinderEvent; 
use CKSource\CKFinder\Filesystem\Folder\WorkingFolder; 
use CKSource\CKFinder\Plugin\PluginInterface; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 

class CustomAutorename implements PluginInterface, EventSubscriberInterface 
{ 
    protected $app; 

    public function setContainer(CKFinder $app) 
    { 
     $this->app = $app; 
    } 

    public function getDefaultConfig() 
    { 
     return []; 
    } 

    public function onBeforeUpload(BeforeCommandEvent $event) 
    { 
     $request = $event->getRequest(); 

     /** @var UploadedFile $uploadedFile */ 
     $uploadedFile = $request->files->get('upload'); 

     /** @var WorkingFolder $workingFolder */ 
     $workingFolder = $this->app['working_folder']; 

     if ($uploadedFile) { 
      $uploadedFileName = $uploadedFile->getClientOriginalName(); 
      if (!$workingFolder->containsFile($uploadedFileName)) { 
       // File with this name doesn't exist, nothing to do here. 
       return; 
      } 

      $basename = pathinfo($uploadedFileName, PATHINFO_FILENAME); 
      $extension = pathinfo($uploadedFileName, PATHINFO_EXTENSION); 

      $i = 0; 

      // Increment the counter until there's no file named like this in current folder. 
      while (true) { 
       $i++; 
       $uploadedFileName = "{$basename}-{$i}.{$extension}"; 

       if (!$workingFolder->containsFile($uploadedFileName)) { 
        break; 
       } 
      } 

      // And here's the hack to make a private property accessible to set a new file name. 
      $setOriginalName = function (UploadedFile $file, $newFileName) { 
       $file->originalName = $newFileName; 
      }; 

      $setOriginalName = \Closure::bind($setOriginalName, null, $uploadedFile); 

      $setOriginalName($uploadedFile, $uploadedFileName); 
     } 
    } 

    public static function getSubscribedEvents() 
    { 
     return [CKFinderEvent::BEFORE_COMMAND_FILE_UPLOAD => 'onBeforeUpload']; 
    } 
} 
+0

はどうもありがとうございました、私が探していたまさにでしたこと。 –

関連する問題