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-01-29 14:45:10 +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 ;
2010-06-24 09:40:05 +01:00
/**
* A file uploaded through a form .
*
2011-03-06 11:40:06 +00:00
* @ author Bernhard Schussek < bernhard . schussek @ symfony . com >
2010-06-24 09:40:05 +01:00
* @ author Florian Eckerstorfer < florian @ eckerstorfer . org >
*/
class UploadedFile extends File
{
2011-01-03 14:47:20 +00:00
/**
* The original name of the uploaded file
* @ var string
*/
2010-06-24 10:24:08 +01:00
protected $originalName ;
2011-01-03 14:47:20 +00:00
/**
* The mime type provided by the uploader
* @ var string
*/
2010-06-24 10:24:08 +01:00
protected $mimeType ;
2011-01-03 14:47:20 +00:00
/**
* The file size provided by the uploader
* @ var integer
*/
2010-06-24 10:24:08 +01:00
protected $size ;
2011-01-03 14:47:20 +00:00
/**
* The UPLOAD_ERR_XXX constant provided by the uploader
* @ var integer
*/
2010-06-24 10:24:08 +01:00
protected $error ;
2011-01-03 14:47:20 +00:00
/**
* Whether the uploaded file has already been moved
* @ var boolean
*/
2010-06-24 10:24:08 +01:00
protected $moved = false ;
/**
* Accepts the information of the uploaded file as provided by the PHP
* global $_FILES .
*
* @ param string $tmpName The full temporary path to the file
* @ param string $name The original file name
* @ param string $type The type of the file as provided by PHP
* @ param integer $size The file size
* @ param string $error The error constant of the upload . Should be
2011-01-03 14:47:20 +00:00
* one of PHP ' s UPLOAD_ERR_XXX constants .
2010-06-24 10:24:08 +01:00
*/
public function __construct ( $path , $originalName , $mimeType , $size , $error )
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
2010-09-09 15:07:12 +01:00
if ( is_file ( $path )) {
$this -> path = realpath ( $path );
}
2010-06-24 09:40:05 +01:00
2010-11-18 00:13:06 +00:00
if ( null === $error ) {
2010-06-24 10:24:08 +01:00
$error = UPLOAD_ERR_OK ;
}
2010-11-18 00:13:06 +00:00
if ( null === $mimeType ) {
2010-06-24 10:24:08 +01:00
$mimeType = 'application/octet-stream' ;
}
$this -> originalName = ( string ) $originalName ;
$this -> mimeType = $mimeType ;
$this -> size = $size ;
$this -> error = $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
*/
public function getMimeType ()
2010-06-24 09:40:05 +01:00
{
2010-06-24 10:24:08 +01:00
$mimeType = parent :: getMimeType ();
2010-11-18 00:13:06 +00:00
if ( null === $mimeType ) {
2010-06-24 10:24:08 +01:00
$mimeType = $this -> mimeType ;
}
return $mimeType ;
2010-06-24 09:40:05 +01:00
}
2010-06-24 10:24:08 +01:00
/**
* Returns the original file name including its extension .
*
* @ returns string The file name
*/
public function getOriginalName ()
2010-06-24 09:40:05 +01:00
{
2010-06-24 10:24:08 +01:00
return $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 .
*
* @ returns integer The upload error
*/
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
{
2010-06-24 10:24:08 +01:00
if ( ! $this -> moved ) {
2011-01-03 14:47:20 +00:00
$newPath = $directory . DIRECTORY_SEPARATOR . $filename ;
2010-06-24 10:24:08 +01:00
if ( ! move_uploaded_file ( $this -> getPath (), $newPath )) {
throw new FileException ( sprintf ( 'Could not move file %s to %s' , $this -> getPath (), $newPath ));
}
$this -> moved = true ;
2010-10-21 21:15:17 +01:00
$this -> path = realpath ( $newPath );
2010-06-24 10:24:08 +01:00
} else {
2011-01-03 14:47:20 +00:00
parent :: doMove ( $directory , $filename );
}
}
/**
* @ inheritDoc
*/
2011-01-09 03:07:30 +00:00
public function move ( $directory , $name = null )
2011-01-03 14:47:20 +00:00
{
if ( ! $this -> moved ) {
$this -> doMove ( $directory , $this -> originalName );
2011-01-09 03:07:30 +00:00
if ( null !== $name ) {
$this -> rename ( $name );
}
2011-01-03 14:47:20 +00:00
} else {
2011-01-09 03:07:30 +00:00
parent :: move ( $directory , $name );
2010-06-24 10:24:08 +01:00
}
2010-06-24 09:40:05 +01:00
}
}