File: /home/undanet/www/PortalEmpleo/src/Utils/file_uploader.php
<?php
namespace PortalEmpleo\Utils;
use PortalEmpleo\Controller\UtilsController;
require __DIR__ . '/../../vendor/autoload.php';
//$fileOrigen = "Area"; //HAY QUE QUITARLO PARA QUE SEA EL QUE RECIBA POR PARAMETRO, ES PARA TESTING
//$fileDestino = "Area"; //HAY QUE QUITARLO PARA QUE SEA EL QUE RECIBA POR PARAMETRO, ES PARA TESTING
if(!defined('MAX_SIZE')) {
//const MAX_SIZE = 10485760;
define('MAX_SIZE', 10485760);
}
if(isset($fileOrigen) && isset($fileDestino))
{
try
{
$fileExtensionsAllowed = ['doc','docx','pdf']; //accept=".doc,.docx,application/msword,.pdf,application/pdf"
if(isset($permiteImagenes) && $permiteImagenes==true)
{
$fileExtensionsAllowed = ['doc','docx','pdf', 'jpg', 'jpeg', 'png', 'bmp', 'gif', 'zip']; //accept=".doc,.docx,application/msword,.pdf,application/pdf,.jpg,.jpeg,image/jpg,.png,image/png,.bmp,image/bmp,.gif,image/gif"
}
$fileName = $fileOrigen['name'];
$fileSize = $fileOrigen['size'];
$fileTmpName = $fileOrigen['tmp_name'];
$fileType = $fileOrigen['type'];
$farray = explode('.',$fileName);
$fileExtension = strtolower(end($farray));
if (! in_array($fileExtension,$fileExtensionsAllowed)) {
$error = "Error: This file extension is not allowed. Please upload .doc, .docx y .pdf files";
$retorno = false;
return;
}
if ($fileSize <= 0) {
$error = "Error: Forbidden file size";
$retorno = false;
return;
}
if ($fileSize > MAX_SIZE) {
$error = "Error: File exceeds maximum size (10MB)";
$retorno = false;
return;
}
if (!file_exists(dirname($fileDestino))) {
mkdir(dirname($fileDestino), 0777, true);
}
//echo 'subir archivo ' . $fileOrigen['tmp_name'] . ' a la carpeta ' . $fileDestino;
$retorno = move_uploaded_file($fileOrigen['tmp_name'], $fileDestino);
/*if($retorno == false)
{
echo 'retorno false<br>';
}*/
/*if($retorno)
{
echo 'Archivo subido con éxito:' . $retorno;
}*/
}
catch (Exception $ex)
{
$error = $exception->getMessage();
$retorno = false;
return;
}
}
?>