Uite aici clasa mea de file poate te inspiri din ea

.
Ideea este ca face cam tot (verificare,redimensionare, upload, stocare cale imagine/fisier in baza, verifica daca directorul de stocare exista etc);
[php]class files {
/*
The function will allow me to perform operations with files and archives.
Main Methods:
addFile -> Upload a file into a location
delFile -> Delete a file from a location
modFile -> Edit a file from a location
resFile -> Resize a file (available only for images)
*/
//Class variable declaration
var $filename; //<- file full name
var $filesize; //<- here i will load file size in kb
var $filetype; //<- here i will load extension
var $filebase; //<- file name without extension
var $filelocation; //<- were the file will be stored
var $fileallowed; //<- allowed file types
var $filetemp; //<- file name of temporary file
var $datamodule; //<- here i will store the database connection link
var $errorout; //<- here i will store the error management system
var $alert;
//Class constructor
function files() {
//Here i will initialise the engine variables (database and error management system)
$this->fileallowed = array(0 =>array("gif", "jpg", "jpeg", "png"),1=> array("zip", "rar"));
$this->datamodule = new dataserv();
}
//Internal methods
function errorOut($message) {
/*
This is an error function, you may replace the code with your config
*/
echo $message;
exit;
}
function inFileDetails($filename) {
//I will load class variables with needed details
if(strlen($filename) < 5) {
$this->errorOut('Please insert a valid file name.<br>');
}
$tmpFile = pathinfo($filename);
$this->filebase = $tmpFile['basename'];
$this->filetype = strtolower($tmpFile['extension']);
//Check the extension details
$tmpIndex = 0; $tmpFound = 0;
$tmpIndex = sizeof($this->fileallowed);
for($i=0; $i<$tmpIndex; $i++) {
foreach($this->fileallowed[$i] as $key=>$value) {
if(strcasecmp($this->filetype,$value) == 0) {
$tmpFound=1;
}
}
}
if($tmpFound == 0) {
$this->errorOut('The file extension is not allowed.<br>');
}
}
function inFileTarget($target) {
//I will check the target directory existance and permission
if(is_dir($target)) {
if(!is_readable($target)) {
$this->errorOut('The directory could not be open for reading.<br>');
}
if(!is_writable($target)) {
$this->errorOut('The directory could not be open for writting.<br>');
}
}
else {
$this->errorOut('The directory does not exists.<br>');
}
}
//Public methods
function inFileLimit ($filesize,$limitsize) {
if($filesize > $limitsize) {
$this->errorOut('The filesize exceed the maximum limit.<br>');
}
}
function sqlFile($tablename,$itemid,$column) {
/*
This function will upload in database the file details (location)
*/
$data = $this->filename;
//Getting table primary key
$sql="SHOW INDEX FROM ".$tablename;
$this->datamodule->engQuery($sql);
$row=mysql_fetch_array($this->datamodule->result,MYSQL_ASSOC);
$primaryKey=$row['Column_name'];
mysql_free_result($this->datamodule->result);
//Now performing the test <- if the record exists i will make update, otherwise i will make insert
$sql="select count($primaryKey) as itemNumber from $tablename where $primaryKey=$itemid";
$this->datamodule->engQuery($sql);
$row=mysql_fetch_array($this->datamodule->result,MYSQL_ASSOC);
$itemNumber = $row['itemNumber'];
mysql_free_result($this->datamodule->result);
if($itemNumber !=0){
$sql="update $tablename set $column='{$data}' where $primaryKey=$itemid";
$this->datamodule->engQuery($sql);
}
else{
$sql="insert into $tablename ($column) values ('{$data}')";
$this->datamodule->engQuery($sql);
}
}
function addFile($location,$filename,$filetemp) {
//Variable declaration
$this->filename = $filename;
$this->filelocation = $location;
$this->filetemp = $filetemp;
$tmpLocation="";
$tmpLocation=$this->filelocation.$this->filename;
$this->inFileTarget($this->filelocation);
$this->inFileDetails($this->filename);
//Verify if a other file has same name
if(file_exists($tmpLocation)) {
$this->errorOut('This file name is already used by another file');
}
//Uploading the file
if(is_uploaded_file($this->filetemp)) {
move_uploaded_file($this->filetemp, $tmpLocation);
chmod($tmpLocation, 0755);
}
}
function delFile($location,$filename) {
/*
The function will delete a file from specified location
*/
//Variable declaration
$this->filename = $filename;
$this->filelocation = $location;
$this->inFileTarget($this->filelocation);
$tmpLocation ='';
$tmpLocation = $location.$filename;
//Deleting the file
chmod($tmpLocation, 0755);
$fileDeleted = unlink($tmpLocation);
if(!$fileDeleted) {
$this->errorOut('Cannot delete the file.<br>');
}
}
function delFileNoError($location,$filename) {
/*
The function will delete a file from specified location
*/
//Variable declaration
$this->filename = $filename;
$this->filelocation = $location;
$this->inFileTarget($this->filelocation);
$tmpLocation ='';
$tmpLocation = $location.$filename;
//Deleting the file
chmod($tmpLocation, 0755);
$fileDeleted = @unlink($tmpLocation);
}
function modFile($location,$filename,$filetemp) {
/*
Editable function.
It will replace a file with a new one
*/
//Variable declaration
$this->filename = $filename;
$this->filelocation = $location;
$this->filetemp = $filetemp;
$tmpLocation="";
$tmpLocation=$this->filelocation.$this->filename;
$this->inFileTarget($this->filelocation);
$this->inFileDetails($this->filename);
//Updating the file
if(is_uploaded_file($this->filetemp)) {
move_uploaded_file($this->filetemp, $tmpLocation);
chmod($tmpLocation, 0755);
}
}
function resFile($width,$height,$proportion) {
/*
This function will resize an image and depending on the PHP version i will
also enable the gif resising, otherwise i will let the gif file as it is.
$proportion <- if is set to 1 this will force the resising process to maintain
the image scale
*/
$tmpLocation="";
$tmpLocation=$this->filelocation.$this->filename;
if(!in_array($this->filetype,$this->fileallowed[0])) {
$this->errorOut("Invalid extension for an image.<br>");
}
if(file_exists($tmpLocation)) {
if(strcasecmp($this->filetype,'jpg')==0 or strcasecmp($this->filetype,'jpeg')==0) {
$img=@imagecreatefromjpeg($tmpLocation);
}
elseif(strcasecmp($this->filetype,'png')==0) {
$img=@imagecreatefrompng($tmpLocation);
}
elseif(strcasecmp($this->filetype,'gif')==0) {
$img=@imagecreatefromgif($tmpLocation);
}
if ($img) {
# Get image size and scale ratio
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
$scale = min($width/$imgWidth, $height/$imgHeight);
if ($scale < 1) {
if($proportion == 1) {
$new_width = floor($scale*$imgWidth);
$new_height = floor($scale*$imgHeight);
}
else {
$new_width = $width;
$new_height = $height;
}
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
$bgc = imagecolorallocate($tmp_img, 255, 255, 255);
imagefilledrectangle($tmp_img, 0, 0, $new_width, $new_height, $bgc);
# Copy and resize old image into new image
$resized=imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $imgWidth, $imgHeight);
switch ($this->filetype) {
case 'jpg':
imagejpeg($tmp_img,$tmpLocation,80);
break;
case 'gif':
{
if (function_exists("imagegif")){
imagegif($tmp_img,$tmpLocation);
}
}
break;
case 'png':
imagepng($tmp_img,$tmpLocation);
break;
}
imagedestroy($img);
imagedestroy($tmp_img);
}
}
}
}
}[/php]
Orice problema se rezolva cu o grenada.