158 lines
3.6 KiB
PHP
158 lines
3.6 KiB
PHP
<?php
|
|
|
|
class fileUploader
|
|
{
|
|
const GET_NAME = 'qqfile';
|
|
|
|
private $asAllowedExtensions;
|
|
private $iSizeLimit;
|
|
private $sFolderPath;
|
|
|
|
function __construct($sFolderPath, $asAllowedExtensions = array()/*, $sizeLimit = 10485760*/)
|
|
{
|
|
$this->sFolderPath = $sFolderPath;
|
|
$this->asAllowedExtensions = array_map("mb_strtolower", $asAllowedExtensions);
|
|
//$this->iSizeLimit = $sizeLimit;
|
|
//$this->checkServerSettings();
|
|
$this->iSizeLimit = Databap::getMaxSize();
|
|
|
|
/*
|
|
if(isset($_GET[self::GET_NAME]))
|
|
{
|
|
//$this->file = new qqUploadedFileXhr();
|
|
}
|
|
elseif(isset($_FILES[self::GET_NAME])) {
|
|
$this->file = new qqUploadedFileForm();
|
|
}
|
|
else
|
|
{
|
|
$this->file = false;
|
|
}
|
|
*/
|
|
}
|
|
|
|
/*
|
|
private function checkServerSettings()
|
|
{
|
|
$postSize = $this->toBytes(ini_get('post_max_size'));
|
|
$uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
|
|
|
|
|
|
if($postSize < $this->iSizeLimit || $uploadSize < $this->iSizeLimit){
|
|
$size = max(1, $this->iSizeLimit / 1024 / 1024) . 'M';
|
|
die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
|
|
}
|
|
}
|
|
|
|
private function toBytes($str)
|
|
{
|
|
$val = trim($str);
|
|
$last = mb_strtolower($str[mb_strlen($str)-1]);
|
|
switch($last) {
|
|
case 'g': $val *= 1024;
|
|
case 'm': $val *= 1024;
|
|
case 'k': $val *= 1024;
|
|
}
|
|
return $val;
|
|
}
|
|
*/
|
|
|
|
function save($sFileName)
|
|
{
|
|
$oInput = fopen("php://input", "r");
|
|
$sTemp = tmpfile();
|
|
$iRealSize = stream_copy_to_stream($oInput, $sTemp);
|
|
fclose($oInput);
|
|
|
|
if($iRealSize != $this->getSize())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
$sPath = $this->sFolderPath.$sFileName;
|
|
$oTarget = fopen($sPath, "w");
|
|
fseek($sTemp, 0, SEEK_SET);
|
|
stream_copy_to_stream($sTemp, $oTarget);
|
|
fclose($oTarget);
|
|
|
|
return true;
|
|
}
|
|
|
|
function getName()
|
|
{
|
|
return isset($_GET[self::GET_NAME])?$_GET[self::GET_NAME]:false;
|
|
}
|
|
|
|
function getSize()
|
|
{
|
|
if(isset($_SERVER["CONTENT_LENGTH"]))
|
|
{
|
|
return (int)$_SERVER["CONTENT_LENGTH"];
|
|
}
|
|
else
|
|
{
|
|
throw new Exception('Getting content length is not supported.');
|
|
}
|
|
}
|
|
|
|
//Returns array('success'=>true) or array('error'=>'error message')
|
|
function handleUpload(/*$sUploadDir, $replaceOldFile = false*/)
|
|
{
|
|
if(!is_writable($this->sFolderPath))
|
|
{
|
|
return array('error' => "Erreur serveur. Le dossier est protégé en écriture");
|
|
}
|
|
if(!$this->getName())
|
|
{
|
|
return array('error' => 'Aucun fichier téléchargé');
|
|
}
|
|
|
|
$iSize = $this->getSize();
|
|
if($iSize == 0)
|
|
{
|
|
return array('error' => 'Fichier vide');
|
|
}
|
|
if($iSize > $this->iSizeLimit)
|
|
{
|
|
return array('error' => 'Fichier trop volumineux');
|
|
}
|
|
|
|
$asPathInfo = pathinfo($this->getName());
|
|
$sExt = mb_strtolower($asPathInfo['extension']);
|
|
$sExt = ($sExt=='jpg')?'jpeg':$sExt;
|
|
if($this->asAllowedExtensions && !in_array($sExt, $this->asAllowedExtensions))
|
|
{
|
|
$sAuthorizedExts = implode(', ', $this->asAllowedExtensions);
|
|
return array('error' => 'Le fichier a une extension invalide, les extensions autorisées sont : '.$sAuthorizedExts);
|
|
}
|
|
|
|
//
|
|
|
|
|
|
//$sFileName = $asPathInfo['filename'];
|
|
$sFileName = md5(uniqid());
|
|
/*
|
|
if(!$replaceOldFile){
|
|
// don't overwrite previous files that were uploaded
|
|
//while (file_exists($sUploadDir . $sFileName . '.' . $sExt)) {
|
|
// $sFileName .= rand(10, 99);
|
|
//}
|
|
if(file_exists($sUploadDir . $sFileName . '.' . $sExt))
|
|
{
|
|
$sFileName .= date(Databap::DATE_COMPACT_FORMAT);
|
|
}
|
|
}
|
|
*/
|
|
|
|
if($this->save($sFileName.'.'.$sExt))
|
|
{
|
|
return array('success'=>true, 'file_name'=>$sFileName.'.'.$sExt);
|
|
}
|
|
else
|
|
{
|
|
return array('error'=> 'Erreur server. Impossible de sauvegarder le fichier.');
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|