2010-06-24 09:40:05 +01:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2011-02-09 19:04:59 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-06-24 09:40:05 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2011-01-29 14:45:10 +00:00
namespace Symfony\Component\HttpFoundation\File ;
use Symfony\Component\HttpFoundation\File\Exception\FileException ;
2011-03-17 23:07:53 +00:00
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException ;
2011-01-29 14:45:10 +00:00
2010-06-24 09:40:05 +01:00
/**
* A file uploaded through a form .
*
2011-03-17 23:07:53 +00:00
* @ author Bernhard Schussek < bernhard . schussek @ symfony . com >
* @ author Florian Eckerstorfer < florian @ eckerstorfer . org >
2010-06-24 09:40:05 +01:00
*/
class UploadedFile extends File
{
2011-01-03 14:47:20 +00:00
/**
2011-03-17 23:07:53 +00:00
* The original name of the uploaded file .
*
2011-01-03 14:47:20 +00:00
* @ var string
*/
2010-06-24 10:24:08 +01:00
protected $originalName ;
2011-01-03 14:47:20 +00:00
/**
2011-03-17 23:07:53 +00:00
* The mime type provided by the uploader .
*
2011-01-03 14:47:20 +00:00
* @ var string
*/
2010-06-24 10:24:08 +01:00
protected $mimeType ;
2011-01-03 14:47:20 +00:00
/**
2011-03-17 23:07:53 +00:00
* The file size provided by the uploader .
*
2011-01-03 14:47:20 +00:00
* @ var integer
*/
2010-06-24 10:24:08 +01:00
protected $size ;
2011-01-03 14:47:20 +00:00
/**
2011-03-17 23:07:53 +00:00
* The UPLOAD_ERR_XXX constant provided by the uploader .
*
2011-01-03 14:47:20 +00:00
* @ var integer
*/
2010-06-24 10:24:08 +01:00
protected $error ;
2011-01-03 14:47:20 +00:00
/**
2011-03-17 23:07:53 +00:00
* Whether the uploaded file has already been moved .
*
* @ var Boolean
2011-01-03 14:47:20 +00:00
*/
2011-03-17 23:07:53 +00:00
protected $moved ;
2010-06-24 10:24:08 +01:00
/**
2011-03-17 23:07:53 +00:00
* Accepts the information of the uploaded file as provided by the PHP global $_FILES .
*
* @ param string $path The full temporary path to the file
* @ param string $originalName The original file name
* @ param string $mimeType The type of the file as provided by PHP
* @ param integer $size The file size
* @ param integer $error The error constant of the upload ( one of PHP ' s UPLOAD_ERR_XXX constants )
* @ param Boolean $moved Whether the file has been moved from its original location
2010-06-24 10:24:08 +01:00
*
2011-03-17 23:07:53 +00:00
* @ throws FileException If file_uploads is disabled
* @ throws FileNotFoundException If the file does not exist
2010-06-24 10:24:08 +01:00
*/
2011-03-17 23:07:53 +00:00
public function __construct ( $path , $originalName , $mimeType , $size , $error , $moved = false )
2010-06-24 09:40:05 +01:00
{
2010-06-24 10:24:08 +01:00
if ( ! ini_get ( 'file_uploads' )) {
throw new FileException ( sprintf ( 'Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)' , get_cfg_var ( 'cfg_file_path' )));
}
2010-06-24 09:40:05 +01:00
2011-03-17 23:07:53 +00:00
if ( ! is_file ( $path )) {
throw new FileNotFoundException ( $path );
2010-09-09 15:07:12 +01:00
}
2010-06-24 09:40:05 +01:00
2011-03-17 23:07:53 +00:00
$this -> path = realpath ( $path );
$this -> originalName = $originalName ;
$this -> mimeType = $mimeType ? : 'application/octet-stream' ;
2010-06-24 10:24:08 +01:00
$this -> size = $size ;
2011-03-17 23:07:53 +00:00
$this -> error = $error ? : UPLOAD_ERR_OK ;
$this -> moved = ( Boolean ) $moved ;
2010-06-24 09:40:05 +01:00
}
2010-06-24 10:24:08 +01:00
/**
2011-01-03 14:47:20 +00:00
* @ inheritDoc
2010-06-24 10:24:08 +01:00
*/
public function getMimeType ()
2010-06-24 09:40:05 +01:00
{
2011-03-17 23:07:53 +00:00
return parent :: getMimeType () ? : $this -> mimeType ;
}
2010-06-24 10:24:08 +01:00
2011-03-17 23:07:53 +00:00
/**
* @ inheritDoc
*/
public function getSize ()
{
return null === $this -> size ? parent :: getSize () : $this -> size ;
2010-06-24 09:40:05 +01:00
}
2010-06-24 10:24:08 +01:00
/**
2011-03-17 23:07:53 +00:00
* Returns the absolute file name without dots .
2010-06-24 10:24:08 +01:00
*
2011-03-17 23:07:53 +00:00
* @ return string The file path
2010-06-24 10:24:08 +01:00
*/
2011-02-09 19:04:59 +00:00
public function getName ()
2010-06-24 09:40:05 +01:00
{
2011-03-17 23:07:53 +00:00
return $this -> moved ? parent :: getName () : $this -> originalName ;
2010-06-24 09:40:05 +01:00
}
2010-06-24 10:24:08 +01:00
/**
* Returns the upload error .
*
* If the upload was successful , the constant UPLOAD_ERR_OK is returned .
* Otherwise one of the other UPLOAD_ERR_XXX constants is returned .
*
2011-03-17 23:07:53 +00:00
* @ return integer The upload error
2010-06-24 10:24:08 +01:00
*/
public function getError ()
2010-06-24 09:40:05 +01:00
{
2010-06-24 10:24:08 +01:00
return $this -> error ;
2010-06-24 09:40:05 +01:00
}
2010-06-24 10:24:08 +01:00
/**
2011-01-03 14:47:20 +00:00
* @ inheritDoc
2010-06-24 10:24:08 +01:00
*/
2011-01-03 14:47:20 +00:00
protected function doMove ( $directory , $filename )
2010-06-24 09:40:05 +01:00
{
2011-03-17 23:07:53 +00:00
if ( $this -> moved ) {
return parent :: doMove ( $directory , $filename );
}
2011-01-03 14:47:20 +00:00
2011-03-17 23:07:53 +00:00
$newPath = $directory . DIRECTORY_SEPARATOR . $filename ;
2010-06-24 10:24:08 +01:00
2011-03-17 23:07:53 +00:00
if ( ! move_uploaded_file ( $this -> getPath (), $newPath )) {
throw new FileException ( sprintf ( 'Could not move file %s to %s' , $this -> getPath (), $newPath ));
2011-01-03 14:47:20 +00:00
}
2011-03-17 23:07:53 +00:00
$this -> moved = true ;
$this -> path = realpath ( $newPath );
2011-01-03 14:47:20 +00:00
}
/**
* @ inheritDoc
*/
2011-01-09 03:07:30 +00:00
public function move ( $directory , $name = null )
2011-01-03 14:47:20 +00:00
{
2011-03-17 23:07:53 +00:00
if ( $this -> moved ) {
return parent :: move ( $directory , $name );
}
$this -> doMove ( $directory , $this -> originalName );
if ( null !== $name ) {
$this -> rename ( $name );
2010-06-24 10:24:08 +01:00
}
2010-06-24 09:40:05 +01:00
}
2011-03-17 23:07:53 +00:00
}