2010-06-24 09:40:05 +01:00
< ? php
2010-08-20 22:09:55 +01:00
namespace Symfony\Component\File ;
2010-06-24 09:40:05 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\File\Exception\FileException ;
2010-06-24 09:40:05 +01:00
/*
* This file is part of the symfony package .
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
/**
* A file uploaded through a form .
*
* @ author Bernhard Schussek < bernhard . schussek @ symfony - project . com >
* @ author Florian Eckerstorfer < florian @ eckerstorfer . org >
*/
class UploadedFile extends File
{
2010-06-24 10:24:08 +01:00
protected $originalName ;
protected $mimeType ;
protected $size ;
protected $error ;
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
* one of PHP ' s UPLOAD_XXX constants .
*/
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-06-24 10:24:08 +01:00
parent :: __construct ( $path );
2010-06-24 09:40:05 +01:00
2010-06-24 10:24:08 +01:00
if ( is_null ( $error )) {
$error = UPLOAD_ERR_OK ;
}
if ( is_null ( $mimeType )) {
$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
/**
* Returns the mime type of the file .
*
* The mime type is guessed using the functions finfo (), mime_content_type ()
* and the system binary " file " ( in this order ), depending on which of those
* is available on the current operating system .
*
* @ returns string The guessed mime type , e . g . " application/pdf "
*/
public function getMimeType ()
2010-06-24 09:40:05 +01:00
{
2010-06-24 10:24:08 +01:00
$mimeType = parent :: getMimeType ();
if ( is_null ( $mimeType )) {
$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
/**
* Moves the file to a new location .
*
* @ param string $newPath
*/
public function move ( $newPath )
2010-06-24 09:40:05 +01:00
{
2010-06-24 10:24:08 +01:00
if ( ! $this -> moved ) {
if ( ! move_uploaded_file ( $this -> getPath (), $newPath )) {
throw new FileException ( sprintf ( 'Could not move file %s to %s' , $this -> getPath (), $newPath ));
}
$this -> moved = true ;
} else {
parent :: move ( $newPath );
}
2010-06-24 09:40:05 +01:00
}
}