...

View Full Version : help me on the upload class !!



PHPycho
03-11-2007, 05:40 AM
Hello forums
Now i am totally shifting towards OOP..
I had tried to make a upload class but it didnt worked .here is the code

uploader.class.php

<?php
class uploader
{
var $uploadDir;
var $thumbDir;
var $newFileName;
/*var $file_name;
var $file_type;
var $file_size;
var $file_temp_dir;*/
var $fileInfo = array();
var $maxFileSize;
var $allowTypes = array();
var $errMsg = "";
var $suMsg = "";
/***/
var $fileExt;

function getExtension()
{
$fileExt = explode(".",$this->$fileInfo['name']);
$this->fileExt = $fileExt[1];
}

function checkTypes()
{
if(!in_array($this->fileInfo['type'],$this->allowTypes))
{
return FALSE;
}
else
return TRUE;
}


function doUpload()
{
// Check the Size
/* If fileSize > maxSize */
if($this->fileInfo['size'] > $this->maxFileSize)
{
$this->errMsg .= "File Size is larger<br />";
}

/* If fileSize == 0 */
else if($this->fileInfo['size'] == 0)
{
$this->errMsg .= "No file uploaded <br />";
}

/* Check the Types */
else if(!$this->checkTypes())
{
$this->errMsg .= "Invalid file type !! <br />";
}

/* If everything goes fine then Upload */
else
{
//upload to destDir
echo "Final step";
$uploadPath = $this->uploadDir."/".$this->fileInfo['name'];
move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
//finally rename
$ext = $this->getExtension();
$newUploadPath = $this->uploadDir."/".$this->newFileName.".".ext;
if(rename($uploadPath,$newUploadPath))
{
$this->suMsg = "Sucessfully Uploaded & Renamed !!";
}

}
}

function deleteFile()
{
}

function renameFile()
{
}

}
?>

action.php


<?php
if(isset($_POST[] ...)
{
include "uploader.class.php";
$uploaderObj = new uploader();
//assign all the variable properties of uploader.class.php
$uploaderObj->doUpload();
}
?>
But nothing happens
What i want ?
1>Any techniques for checking how the class is working ie any debugging tips
2>Any changes in above code to make it efficient and effective Note: its for PHP4 ie any tips n modifications
3> when i try to echo $uploaderObj->getExtension(), it gives the follwing error
Fatal error: Cannot access empty property in C:\Program Files\xampp\htdocs\designtoko\libs\uploader.class.php on line 21
and why it is so

Thanks for reading my post.

Thanks in advance to all of you and awaiting for the results...

Fou-Lu
03-11-2007, 11:07 AM
Ok.

This:
$this->$fileInfo['name'])
Is not correct, it will attempt to find $fileInfo as a variable itself. Remember, $this now contains fileInfo as a property, and is accessed using $this->fileInfo. Any times you see it with the two dollar signs, and it is not using a local scope variable under that name, you must change it to the classes scope.

Second.
I don't see a constructor for your class. Offhand I cannot recall how picky php is when it requires a constructor, but normally you cannot create an instance of an object using new without the constructor. Since you did not recieve an error about access properties on a non-object, I would assume that php calls a default constructor. With that said, you could use the method in a static fasion: myClass::myFunc();

And lastly, I do not see how any data is being passed between your object and your calls. I just don't see how the files are being passed to the class, or is there more information being used in the action.php that defines that? Remember as well, that $_POST is superglobal and as such, can be accessed within your class itself, you can create an error handler in there if you would like. I'm just not seeing where the files is being passed in...

PHPycho
03-11-2007, 12:32 PM
Thanks all of You!!
Finally i had made the uploader class...its working..

<?php
//uploader.class.php
class uploader
{
var $uploadDir;
var $newFileName;
var $fileInfo = array();
var $maxFileSize;
var $allowTypes = array();
var $fileToDelete;
var $errMsg = "";
var $upMsg = "";
var $delMsg = "";

function getExtension()
{
$fileExt = explode(".",$this->fileInfo['name']);
return $this->fileExt = $fileExt[1];
}

function checkTypes()
{
if(!in_array($this->fileInfo['type'],$this->allowTypes))
{
return FALSE;
}
else
return TRUE;
}


function checkUpload()
{
// Check the Size
/* If fileSize > maxSize */
if($this->fileInfo['size'] > $this->maxFileSize)
{
//array_push($this->errMsg,"File Size is larger");
$this->errMsg = "File Size is larger <br />";
return FALSE;
}

/* If fileSize == 0 */
if($this->fileInfo['size'] == 0)
{
//array_push($this->errMsg,"No file uploaded");
$this->errMsg .= "No file uploaded <br />";
return FALSE;
}

/* Check the Types */
else if(!$this->checkTypes())
{
//array_push($this->errMsg,"Invalid file type !!");
$this->errMsg .= "Invalid file type !! <br />";
return FALSE;
}

/* If everything goes fine then Upload */
else
{
return TRUE;
}

}

function doUpload()
{
$uploadPath = $this->uploadDir."/".$this->fileInfo['name'];
move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
//finally rename
$ext = $this->getExtension();
$newUploadPath = $this->uploadDir."/".$this->newFileName.".".$ext;
if(@rename($uploadPath,$newUploadPath))
{
$this->upMsg = "Sucessfully Uploaded & Renamed !!";
}
else
{
$this->upMsg = "Unable to Rename !!";
}

}

function deleteFile()
{
if(file_exists($this->fileToDelete))
{
if(unlink($this->fileToDelete))
{
$this->delMsg = "File Successfully Deleted !!";
}
else
{
$this->delMsg = "Unable to Delete the file !!";
}
}
else
{
$this->delMsg = "Such file Doesnt exists !!";
}
return $this->delMsg;
}

}
?>

accessing part:

<?php
include "uploader.class.php";
$uploaderObj = new uploader();
$uploaderObj->uploadDir = "../uploads/images/";
$uploaderObj->fileInfo = $_FILES['client_img'];
$uploaderObj->maxFileSize = 1048576; //in bytes
$uploaderObj->allowTypes = array("image/jpeg","image/jpg");

//Check Upload and Insert the Image Contents in DB
if($uploaderObj->checkUpload())
{
$clientImagesObj->insert($clientID,$caption);
$uploaderObj->newFileName = $clientImagesObj->id;
//Finally Upload the file
$uploaderObj->doUpload();
echo $uploaderObj->upMsg;
}
else
{
echo $uploaderObj->errMsg;
}
?>

What i want?
1> any modifications that make the code effective like one i shoulnt use explode to find the extension in case image1.name.jpg
2> i am not getting the concatenated erros, is there any effective way of assigning and displaying erros
3>any suggestions

Thanks in advance to all of You !!

Fou-Lu
03-11-2007, 09:13 PM
Ok I see, gots it working, now your interested in some cleanup right?
I'll walk you through a couple of things I would recommend.
First, variable assignments. Now, I do know that this is exclusively used on a oop php 4 engine, but I'd still make a couple simple modifications to save you time in the future.
Write mutator methods for your properties. Anytime you see an $obj->property, you should write a getProperty() and setProperty() method for the class. Php Version 5 has an overloaded built in __get and __set, which easily simplifies this process. This will get you around the property scope (public private protected) without any concerns. Fortunatly, if you run a php4 class on a php5 engine, it will work as is, so this is really your choice.
For getting the extension, this is an appropriate method to use. The only recommendation I would give you is to check for the last period in the given string, instead of the first. There are different concepts you can use for this, but with what you currently have you can obtain the index using: count(arrLength) - 1 instead. This sould give you the last value of the array. Another option would be to use the string family functions, such as str_split and whatnots, and even pattern matching.
If you have it available, I would also use getimagesize() in conjunction with image_type_to_mime_type to compare what the image actually is. Just because the extension is .jpeg doesn't mean its not really a .php script.

The final thing I would recommend is the placement of your upload directory. I would place it above your directory root to stop users from directly accessing the uploaded files. This may seem silly as you need to write a script to access them, however it will help to add that tiny extra buffer to protect your website integrety should someone upload a php file masked as an image.
Hope that helps you out!

PHPycho
03-12-2007, 11:07 AM
One more Question?
If i would like to generate thumbnail what should i do?
1>make another class thumbNailer.class.php
2>or use with uploader.class.php

Suggest me to make the Thumbnail script for uploader.class.php
Thanks in advance

Fou-Lu
03-12-2007, 02:41 PM
If the thumbnail is a separate stored image based off the uploader, I'd put it in the uploader class. If its a dynamically created thumbnail, I'd create a new class for it.

PHPycho
03-13-2007, 10:39 AM
I have the following code

<?php
class uploader
{
var $uploadDir;
var $newFileName;
var $fileInfo = array();
var $maxFileSize;
var $allowTypes = array();
var $fileToDelete;
//var $errMsg = "";
var $errMsg = array();
var $upMsg = "";
var $delMsg = "";
var $randNo = "";

function getExtension()
{
$fileExt = substr(strrchr($this->fileInfo['name'],"."),1);
//$fileExt = explode(".",$this->fileInfo['name']);
return $this->fileExt = $fileExt;
}

function checkTypes()
{
if(!in_array($this->fileInfo['type'],$this->allowTypes))
{
return FALSE;
}
else
return TRUE;
}


function checkUpload()
{
// Check the Size
/* If fileSize > maxSize */
if($this->fileInfo['size'] > $this->maxFileSize)
{
//array_push($this->errMsg,"File Size is larger");
$this->errMsg[] = "File Size is larger <br />";
return FALSE;
}

/* If fileSize == 0 */
else if($this->fileInfo['size'] == 0)
{
//array_push($this->errMsg,"No file uploaded");
$this->errMsg[] = "No file uploaded <br />";
return FALSE;
}

/* Check the Types */
else if(!$this->checkTypes())
{
//array_push($this->errMsg,"Invalid file type !!");
$this->errMsg[] = "Invalid file type !! <br />";
return FALSE;
}

/* If everything goes fine then Upload */
else
{
return TRUE;
}

}

function doUpload()
{
$uploadPath = $this->uploadDir."/".$this->fileInfo['name'];
@move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
//finally rename
$ext = $this->getExtension();
$newUploadPath = $this->uploadDir."/".$this->newFileName.".".$ext;
if(@rename($uploadPath,$newUploadPath))
{
$this->upMsg = "Sucessfully Uploaded & Renamed !!";
}
else
{
$this->upMsg = "Unable to Rename !!";
}

}

function deleteFile()
{
if(file_exists($this->fileToDelete))
{
if(unlink($this->fileToDelete))
{
$this->delMsg = "File Successfully Deleted !!";
}
else
{
$this->delMsg = "Unable to Delete the file !!";
}
}
else
{
$this->delMsg = "Such file Doesnt exists !!";
}
return $this->delMsg;
}

function getErrors()
{
return $this->errMsg;
}

function genRandomNo()
{
//using uniqid() or microtime() for unique no generation
$randNo = substr(md5(uniqid(microtime())), 0, 8);
return $randNo;
}

}
?>

<?php
//including and creating object goes here...
$uploaderObj->uploadDir = "../uploads/images/";
$uploaderObj->fileInfo = $_FILES['client_img'];
$uploaderObj->maxFileSize = 1048576; //in bytes
$uploaderObj->allowTypes = array("image/jpeg","image/jpg");

//Check Upload and Insert the Image Contents in DB
if($uploaderObj->checkUpload())
{
$clientImagesObj->insert($clientID,$caption);
$uploaderObj->newFileName = $clientImagesObj->id;
//Finally Upload the file
$uploaderObj->doUpload();
echo $uploaderObj->upMsg;
}
else
{
print_r($uploaderObj->getErrors());
//echo $uploaderObj->getErrors();
}
?>

Problem with the code:
1> when i add .mp3 of very large size it gives only one error message but it should give two error messages ie it give error message "File is larger !!" but it should give "File is larger !!" & "Invalid file type !!". what is the problem with the code ?
Help !!
Thanks in advance

Fou-Lu
03-14-2007, 01:52 AM
You are using if/else with the returns. It finds the first error and returns a false boolean. To change it, you need to step through the possible error conditions setting the appropriate error messages, and return if the array according to its null value. False if it is not null, true if it is null.

PHPycho
03-14-2007, 05:41 AM
Thanks Mr. Fou-Lu
Can you help a bit with some codes Please ?
Thanks again !!

Fou-Lu
03-14-2007, 07:25 AM
What kind of codes? If they are related to this class, post it in the thread. Otherwise, you can send it to me via pm and I'll take a look at it.

PHPycho
03-14-2007, 07:51 AM
You are using if/else with the returns. It finds the first error and returns a false boolean. To change it, you need to step through the possible error conditions setting the appropriate error messages, and return if the array according to its null value. False if it is not null, true if it is null.

codes related to errors and for the above case



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum