Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
_classUpload.php
<?php
/*********************************************************************
* Criada por Vinícius
* op.vini@gmail.com ou www.isecretaria.net/vinicius
**********************************************************************/
class Upload{
var $maxSize = 8000; // 8 MB, tamanho máximo permitido
var $sobrescrever = 0; // 0 NÃO, 1 SIM, sobrescrever o arquivo se ele existir
var $allowBlank = 1; // 0 NÃO, 1 SIM, deixar campo em branco
var $blockExt = 0; // 0 NÃO, 1 SIM, bloquear extensões de arrExtBlocks
var $liberaExt = 0; // 0 NÃO, 1 SIM, liberar apenas extensões de arrExtPermit
var $arrExtPermits = array(); // array com extensões permitidas, liberaExt deve ser 1
var $arrExtBlocks = array(); // array com extensões que não são permitidas blockExt deve ser 1
var $fileName = "";
var $fileTmp = "";
var $fileSize = 0;
var $destino = "";
var $error = "";
protected $cfg;
protected $ext;
protected $arq;
protected $i;
function enviar(){
if( $this->fileName != "" )
{
$this->cfg = explode(".", $this->fileName);
$this->ext = $this->cfg[ count($this->cfg)-1 ];
$this->arq = str_replace(".".$this->ext, "", $this->fileName);
}
if( !$this->allowBlank && $this->fileName == "" ) $this->error .= "Upload Error 1: não foi passado nenhum arquivo.";
if( ($this->fileSize / 1024) > $this->maxSize ) $this->error .= "Upload Error 2: o arquivo é maior que o máximo permitido.";
if( $this->blockExt && in_array(strtolower($this->ext),$this->arrExtBlocks) && $this->fileName != "" )
$this->error .= "Upload Error 5: a extensão '".$this->ext."' não é permitida.";
if( $this->liberaExt && !in_array(strtolower($this->ext),$this->arrExtPermits) && $this->fileName != "" )
$this->error .= "Upload Error 6: a extensão '".$this->ext."' não é permitida.";
if( $this->fileName != "" && $this->destino != "" && !$this->sobrescrever)
{
if( file_exists( $this->destino.$this->fileName ) )
{
$this->i = 2;
while( file_exists($this->destino.$this->arq."(".$this->i.").".$this->ext) ) $this->i = $this->i + 1;
$this->fileName = $this->arq."(".$this->i.").".$this->ext;
}
}
else if( $this->destino == "" ) $this->error .= "Upload Error 3: não foi encontrado um destino.";
if( $this->error != "" ) return false;
else if( $this->fileName == "" && $this->allowBlank ) return true;
else
{
if( !move_uploaded_file( $this->fileTmp, $this->destino.$this->fileName ) )
{
$this->error .= "Upload Error 4: houve um erro no upload.".$this->fileTmp;
return false;
}
else return true;
}
}
}
?>
UTILIZANDO, neste exemplo só aceitará extensão MP3:
<?
include "_classUpload.php";
$upload = new Upload;
$upload->liberaExt = 1;
$upload->maxSize = 100000;
$upload->arrExtPermits = array('mp3');
$upload->destino = "/";
$upload->fileName = $_FILES['audio']['name'];
$upload->fileSize = $_FILES['audio']['size'];
$upload->fileTmp = $_FILES['audio']['tmp_name'];
if(!$upload->enviar()){
echo '{success: false, msg: "'.$upload->error.'"}';
exit;
}
?>
abraços
Carregando comentários...