Jump to content

help me on the upload class !!


PHPycho

Recommended Posts

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...

Link to comment
Share on other sites

Try this for your uploader class:

 

<?php
class uploader
{
var $uploadDir;
var $thumbDir;
var $newFileName;
var $fileInfo = array();
var $maxFileSize = 1024; // Max filesize of 1024 bytes (1 MB)
var $allowTypes = array();
var $errMsg = array();
var $suMsg = "";
var $fileExt;
var $uploaded = false;
var $fileLocation;

function uploader($file) {
	$this->fileInfo = $file;
}

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

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

function doUpload($file) {
	if (!$this->fileInfo['tmp_name'])
		$this->errMsg[] = "No file uploaded";
	if ($this->fileInfo['size'] > $this->maxFileSize)
		$this->errMsg[] = "File size is too large";
	if (!$this->checkTypes())
		$this->errMsg[] = "Invalid file type";
	if (!$this->errMsg) {
		$filename = $this->newFileName ? $this->newFileName : $this->fileInfo['name'];
		$this->fileLocation = $uploadPath = $this->uploadDir . "/" . $filename;
		move_uploaded_file($this->fileInfo['tmp_name'], $uploadPath);
		$this->uploaded = true;
	}

}

function deleteFile() {
	if ($this->uploaded)
		return unlink($this->fileLocation);
}

function renameFile($newFilename) {
	if ($this->uploaded)
		return rename($this->fileLocation, $this->uploadDir . "/" . $newFilename);
	else $this->newFileName = $newFilename;
	return true;
}
}
?>

 

Try this for your action file:

<?php
if (isset($_FILES['filename'])) {
    $uploaderObj = new uploader($_FILES['filename']);
    $uploaderObj->doUpload();
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.