http://symfonysymplifyd.blogspot.com.es/search/?q=file
Entiendo todo bien salvo una parte que intentaré explicar de la mejor forma que me sea posible.
El ejemplo de subida de este tuto consiste en:
1 Bundle llamado File
2 Modelos (clases) dentro de este Bundle: Document.php y
UploadFileMover.php
El código de Document.php sería este:
Código:
Y el de "UploadFileMover" este:<?php namespace File\FileBundle\Models; /** * Description of Document * * @author Manoj */ use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; class Document { private $file; private $subDir; private $filePersistencePath; /** @var string */ protected static $uploadDirectory = '%kernel.root_dir%/../uploads'; static public function setUploadDirectory($dir) { self::$uploadDirectory = $dir; } static public function getUploadDirectory() { if (self::$uploadDirectory === null) { throw new \RuntimeException("Trying to access upload directory for profile files"); } return self::$uploadDirectory; } public function setSubDirectory($dir) { $this->subDir = $dir; } public function getSubDirectory() { if ($this->subDir === null) { throw new \RuntimeException("Trying to access sub directory for profile files"); } return $this->subDir; } public function setFile(File $file) { $this->file = $file; } public function getFile() { return new File(self::getUploadDirectory() . "/" . $this->filePersistencePath); } public function getOriginalFileName() { return $this->file->getClientOriginalName(); } public function getFilePersistencePath() { return $this->filePersistencePath; } public function processFile() { if (! ($this->file instanceof UploadedFile) ) { return false; } $uploadFileMover = new UploadFileMover(); $this->filePersistencePath = $uploadFileMover->moveUploadedFile($this->file, self::getUploadDirectory(),$this->subDir); } }
Código:
<?php namespace File\FileBundle\Models; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Description of UploadFileMover * * @author Manoj */ class UploadFileMover { public function moveUploadedFile(UploadedFile $file, $uploadBasePath,$relativePath) { $originalName = $file->getFilename(); // use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test. // $relativePath = date('Y-m', filemtime($file->getPath())); $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName; $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName; $ext = $file->getExtension(); $i=1; while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) { if ($ext) { $prev = $i == 1 ? "" : $i; $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath); } else { $targetFilePath = $targetFilePath . $i++; } } $targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath; if (!is_dir($targetDir)) { $ret = mkdir($targetDir, umask(), true); if (!$ret) { throw new \RuntimeException("Could not create target directory to move temporary file into."); } } $file->move($targetDir, basename($targetFilePath)); return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath); } } ?>
1 Controller "UploadController.php" que tiene este código:
Código:
Mi principal duda reside en la llamada desde Document.php al método "moveUploadedFile", ya que este método es de la clase UploadFileMover que no veo que haya sido instanciada ni llamada mediante USE ni de ninguna otra manera, ¿cómo es posible que esto funcione así?.namespace File\FileBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\File\UploadedFile; use File\FileBundle\Models\Document; class UploadController extends Controller { public function uploadAction(Request $request) { if ($request->getMethod() == 'POST') { $image = $request->files->get('img'); $status = 'success'; $uploadedURL=''; $message=''; if (($image instanceof UploadedFile) && ($image->getError() == '0')) { if (($image->getSize() < 2000000000)) { $originalName = $image->getClientOriginalName(); $name_array = explode('.', $originalName); $file_type = $name_array[sizeof($name_array) - 1]; $valid_filetypes = array('jpg', 'jpeg', 'bmp', 'png'); if (in_array(strtolower($file_type), $valid_filetypes)) { //Start Uploading File $document = new Document(); $document->setFile($image); $document->setSubDirectory('uploads'); $document->processFile(); $uploadedURL=$uploadedURL = $document->getUploadDirectory() . DIRECTORY_SEPARATOR . $document->getSubDirectory() . DIRECTORY_SEPARATOR . $image->getBasename(); } else { $status = 'failed'; $message = 'Invalid File Type'; } } else { $status = 'failed'; $message = 'Size exceeds limit'; } } else { $status = 'failed'; $message = 'File Error'; } return $this->render('FileFileBundle:Default:index.html.twig',array('status'=>$status,'message'=>$message,'uploadedURL'=>$uploadedURL)); } else { return $this->render('FileFileBundle:Default:index.html.twig'); } } }
Gracias de antemano.