I have a class (taken from the Google code section) for uploading a
file but I do not know how to use / call it.
Here is the html form:
<form enctype="multipart/form-data" action="up.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
<input name="userfile" type="file" value="" size="40"/>
<input type="submit" name="UploadImage" value="go" />
</form>
here is the code for the class which I have not been able to test but
I assume it's OK.
<?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;
}
}
?>
Questions:
1. What is the exact procedure to call / use it?
2. I would also like to add a function to the class which adds certain
items to a MySql db. How can that be done? I know how to do the db bit
I just don't know how to create the function in the class file and
then call it. This is not as important as Q.1
3. I assume that 3 files are involved (1. upload.html which has the
upload form and is posted to file 2 which is upload.php which includes
and calls file 3 upload.class.php). Please can it be organised with
this kind of structure?
4. Finally, after file upload I would like the success or error
message to appear on upload.html not upload.php so some kind of
redirect will be necesary.
Thank you, look forward to hearing from somebody. |