using a PHP class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richflash
Forum Newbie
Posts: 2
Joined: Thu Oct 12, 2006 7:01 pm

using a PHP class

Post by richflash »

Hello, I amnew here.

I have a PHP file upload class (which I got from the Google coding website), copied below.

How can I activate this (make it work) from an HTML form?

The form code is:

Code: Select all

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
    <input name="userfile" type="file" value="" size="40"/> &nbsp;<input type="submit" name="submit" 

value="go" />

and the class (upload.class.php) is:

<?php
/**
* Base Upload Class
*
* Upload Class Used for Uploading all kinds of
* Files, can be extended to fit specific needs
*/
class Upload extends File {
	/**
	* @var string
	*/
	var $type;
	/**
	* @var string
	*/
	var $tmp_name;
	/**
	* @var int
	*/
	var $error;
	/**
	* @var int
	*/
	var $size;
	

	/**
	* Class Constructor
	*
	* Creates a upload object from form data
	*
	* @param mixed
	* @return NULL
	*/
	function Upload($data, $directory='/home/richsnap/public_html/ecozenhtml/images/'){
		$this->filename = $data['name'];
		$this->type = $data['type'];
		$this->tmp_name = $data['tmp_name'];
		$this->error = $data['error'];
		$this->size = $data['size'];
		$this->directory = $directory;
	}

	/**
	* Checks the FILE data for and errors, overloads the Form CheckData
	* $acceptedTypes = array of acceptable file types
	* $max_size = int size of file in bytes
	* <code>
	* <?php
	* define(MAX_SIZE,1500000);
	* $image_types = array('image/jpeg','image/gif','image/png','image/pjpeg');
	* ?>
	* </code>
	* @param array, int in bytes
	* @return array
	*/
	function CheckData($acceptedTypes, $max_size){
		// Make sure that we have something to check first
		if ($this->filename != ''){
			Debug('CheckData(), Checking File data for errors.');
			// Check to see if the dirstory is writable
			if (!is_writable($this->directory)){
				// Try to make it writable
				$path = '';
				foreach( split('/', $this->directory) as $data ){
					$path .= $data . '/';
					if ( !file_exists($path) ){
						umask(000);
						mkdir($path,0775);
					}
				}
			}
			if (!is_writable($this->directory))
				$error = 'Directory is not writable, please check with the systems administrator';
		
			// Check file types
			if (!in_array($this->type,$acceptedTypes))
				$error = 'This type of file is not permitted, Please try again';
			else if ($this->size == 0)
				$error = 'The filesize is zero, Please try again';
			else if ($this->size > $max_size)
				$error = 'The filesize is too large, ' . $max_size . ' bytes is filesize limit, Please try again';
		}
		
		// Return the Errors
		return $error;
	}

	/**
	* Upload File
	*
	* Moves the file from its temp location and puts it into the directory that is set
	*
	* @return BOOL true/false if the item was uploaded successfully
	*/
	function UploadFile(){
		// Format the filename to make sure it is directory safe
		$this->FormatFilename();
		
		// Upload the file
		if ( move_uploaded_file($this->tmp_name, $this->directory . $this->filename) ) {
			Debug('UploadFile(), File moved successfully to: "' . $this->directory . $this->filename . '"');
			// Make sure it is in the right spot
			if ( file_exists($this->directory . $this->filename) ){ 
				// Change the permissions so the file is not locked
				chmod($this->directory . $this->filename, 0775);
				// Return if the file is uploaded successfully
				if (is_writable($this->directory . $this->filename)){
					Debug('UploadFile(), Is Writable! "' . $this->directory . $this->filename . '"');
					return true;
				}else{
					Debug('UploadFile(), Is Not Writable! "' . $this->directory . $this->filename . '"');
				}
			}
		}

		// There must have been some issue
		return false;
	}
	

}
?>
Thank you, all help appreciated.

richflash
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Use

Code: Select all

tags in your forum posts.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Don't they provide documentation for it?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Looks simple enough:

Code: Select all

<?php

$upload = new Upload($_FILES['userfile'], '/path/to/saved/dir');
$upload->UploadFile();

?>
richflash
Forum Newbie
Posts: 2
Joined: Thu Oct 12, 2006 7:01 pm

calling a PHP class seple

Post by richflash »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks very much for your input -- I agree it looks simple enough. Armed wiht your assistance this is what I did:

1. default.html 

[syntax="html"]<div id="UploadForm" style="display:;">
<table><tr><td>
<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
    <input name="userfile" type="file" value="" size="40"/> &nbsp;<input type="submit" name="UploadImage" 

value="go" />
</form>
</td></tr></table>
</div>
2. upload.php[/syntax]

Code: Select all

<?php
include('upload.class.php');
$upload = new Upload($_FILES['userfile'], '/home/richsnap/public_html/images/');
$upload->UploadFile();

?>
Result:
Fatal error: Cannot instantiate non-existent class: upload in /home/richsnap/public_html/ecozenhtml/again/upload.class.php on line 2

I tried putting the php coed in section2 above into the upload.class.php form and posted the form directy to upload.class.php and that produced the same result?

I am sure I am being silly here, but what am I doing wrong? any further insight will be greatly appreciated.

Thank you


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply