note 113405 added to function.is-uploaded-file
| From: | Anonymous | Date: | Mon, 07 Oct 2013 23:08:09 +0000 |
| Subject: | note 113405 added to function.is-uploaded-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196487@lists.php.net to get a copy of this message | ||
Here is my code for file handler, i hope it help to all:
First a class to handler file upload:
<?php
define('UPLOAD_PATH', 'upload/');
define('MAXIMUM_FILESIZE', '10485760'); //10 MB
class FileHandler
{
private $file_types = array('xls', 'xlsx');
private $files = null;
private $filename_sanitized = null;
private $filename_original = null;
public function __construct($files)
{
$this->files = $files;
}
public function setFileTypes($fileTypes = array())
{
$this->file_types = $fileTypes;
return $this;
}
public function setFileNameOriginal($filename)
{
$this->filename_original = $filename;
}
public function fileNameOriginal()
{
return $this->filename_original;
}
public function sanitize($cursor = 0)
{
$this->setFileNameOriginal($this->files['name'][$cursor]);
$safe_filename = preg_replace(
array("/\s+/", "/[^-\.\w]+/"),
array("_", ""),
trim($this->fileNameOriginal()));
$this->filename_sanitized = md5($safe_filename.time()).$safe_filename;
return $this;
}
public function fileSize($cursor = 0)
{
return $this->files['size'][$cursor];
}
public function extensionValid()
{
$fileTypes = implode('|', $this->file_types);
$rEFileTypes = "/^\.($fileTypes){1}$/i";
if(!preg_match($rEFileTypes, strrchr($this->filename_sanitized, '.')))
throw new Exception('No se pudo encontrar el tipo de archivo apropiado');
return $this;
}
public function isUploadedFile($cursor)
{
if(!is_uploaded_file($this->files['tmp_name'][$cursor]))
{
throw new Exception("No se obtuvo la carga del archivo");
}
}
public function saveUploadedFile($cursor)
{
if(!move_uploaded_file
($this->files['tmp_name'][$cursor],UPLOAD_PATH.$this->filename_sanitized))
throw new Exception("No se consiguió guardar el archivo");
}
public function fileNameSanitized()
{
return $this->filename_sanitized;
}
public function uploadFile($cursor = 0)
{
$this->isUploadedFile($cursor);
if ($this->sanitize($cursor)->fileSize($cursor) <= MAXIMUM_FILESIZE)
{
$this->extensionValid()->saveUploadedFile($cursor);
}
else
{
throw new Exception("El archivo es demasiado grande.");
}
return $name;
}
}
?>
Next a part of code to use the class
<?php
//form is submited and detect that
if ($form_submited == 1)
{
try
{
//i assume de input file is:
/*
<input id="<?php echo EXCEL_FILE;?>[]" name="<?php
echo EXCEL_FILE;?>[]" type="file"/>
where EXCEL_FILE is the constant:
define('EXCEL_FILE', 'excel_file');
*/
$file = new FileHandler($_FILES['excel_file']);
$inputFileName = $file->uploadFile()->fileNameSanitized(); // File to read
...
}
catch(Exception $e)
{
die('Error cargando archivo "'.($file->fileNameOriginal()).'":
'.$e->getMessage());
}
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 190.48.120.184
----
Manual Page -- http://php.net/manual/en/function.is-uploaded-file.php
Edit -- https://master.php.net/note/edit/113405
Del: integrated -- https://master.php.net/note/delete/113405/integrated
Del: useless -- https://master.php.net/note/delete/113405/useless
Del: bad code -- https://master.php.net/note/delete/113405/bad+code
Del: spam -- https://master.php.net/note/delete/113405/spam
Del: non-english -- https://master.php.net/note/delete/113405/non-english
Del: in docs -- https://master.php.net/note/delete/113405/in+docs
Del: other reasons-- https://master.php.net/note/delete/113405
Reject -- https://master.php.net/note/reject/113405
Search -- https://master.php.net/manage/user-notes.php