jwmstudios Posted April 25, 2010 Share Posted April 25, 2010 i made a little uploader class, its not refined but it works, here is the class <?php /* This is a simple file uploader class created by me: Hugo Johnson website: www.jwmstudios.com email: hugoj@hotmail.com Its my first real shot at creating a php class, i am still a todler to this. */ Class Uploader { // Declaring my variables needed for this class, it makes sense. public $input_field_name = "userfile"; // default input field name public $file_size =300000; // default file size public $path_directory = ""; // location to send the file public $action_path_and_file = "uploader.php"; // default form action function form() { // This form is optional used if no form is provided, only good for one file echo <<<END <form enctype="multipart/form-data" method="post" action="$this->action_path_and_file "> <input name="$this->input_field_name" type="file" /><br /><input type="hidden" name="max_size" value="$this->file_size" /> <input type="submit" value="Upload" /> </form> END; } function file_field($field_name) { // optional for adding more than one file echo "<br /><input name=\"$field_name\" type=\"file\" /><br />"; } function upload($input_field_name) { // this will upload a file that coresponds to a field name of type file $filename = $_FILES[$input_field_name]['name']; // coming from the form $filesize = $_FILES[$input_field_name]['size']; // " " $temp = $_FILES[$input_field_name]['tmp_name']; // " " // move Uploaded File function places the file from its temp directory and puts it into the desired directory, $path = $this->path_directory.$filename; // you can type a path directory structure between the quotes eg. ../ or /filestorage etc. $uploadfile = move_uploaded_file($temp, $path); // moving file from temp location to new chosen path echo $uploadfile === TRUE ? '<br />File uploaded<br />' : '<br />Awaiting File Upload<br />'; } } ?> Save the file as uploader_bk.php Here is the test form: <?php // This is the test upload form include_once("uploader_bk.php"); // importing the Uploader class $myupload = new Uploader(); // declaring a new uploader object $myupload->file_size = 400000; // setting a different file size $myupload->path_directory = "myfiles/"; // You can mod the path directory at any time, leave blank if is on the same path as the script $myupload->action_path_and_file = $_SERVER['PHP_SELF']; // use $_SERVER['PHP_SELF'] if its the same script that is doing the processing $fieldname = array("joe", "Mary"); // This can apply to one file you may need to prefix variable names to add more fields; this could also be an include $max_fields = count($fieldname); // getting the maximum number of fields that you need echo "<br />"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Handling File Uploads</title> </head> <body> <form enctype="multipart/form-data" method="post" action="<?php echo $myupload->action_path_and_file; ?>"> <?php for ($i=0 ; $i < $max_fields; $i++) { $myupload->file_field($fieldname[$i]); // input field. $myupload->upload($fieldname[$i]); // The uploading process. } ?> <input type="submit" value="Send File" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/ Share on other sites More sharing options...
jwmstudios Posted April 26, 2010 Author Share Posted April 26, 2010 I also added a image re-sizer for those that are interested in resizing an image and sending it to a new path, it was a challenge creating this and i know its buggy thats why i seek a community such as this one to help me to find them and we can all share good code to make our work flow faster. Antway here is the class, call it uploader_bk.php <?php /* This is a simple file uploader class created by me: Hugo Johnson website: www.jwmstudios.com email: hugoj@hotmail.com Its my first real shot at creating a php class, i am still a todler to this. The Class called uploader is responsible for creating a file field and a single form, or many file fields (without a form) It then uploads the chosen file to a specified path, once you click on the browse button and submitt the form. The Sub-Class called Image is used to both upload the image to a chosen path as well as create a thumbnailed copy this thumbnail can be resized based of the new height parameter that is passed in by the user. Needs more work and validation but enjoy. */ Class Uploader { // Declaring my variables needed for this class, it makes sense. public $input_field_name = "userfile"; // default input field name public $file_size =300000; // default file size public $path_directory = ""; // location to send the file public $action_path_and_file = "uploader.php"; // default form action function form() { // This form is optional used if no form is provided, only good for one file echo <<<END <form enctype="multipart/form-data" method="post" action="$this->action_path_and_file "> <input name="$this->input_field_name" type="file" /><br /><input type="hidden" name="max_size" value="$this->file_size" /> <input type="submit" value="Upload" /> </form> END; } function file_field($field_name) { // optional for adding more than one file echo "<br /><input name=\"$field_name\" type=\"file\" /><br />"; } function upload($input_field_name) { // this will upload a file that coresponds to a field name of type file $filename = $_FILES[$input_field_name]['name']; // coming from the form $filesize = $_FILES[$input_field_name]['size']; // " " $temp = $_FILES[$input_field_name]['tmp_name']; // " " // move Uploaded File function places the file from its temp directory and puts it into the desired directory, $path = $this->path_directory.$filename; // you can type a path directory structure between the quotes eg. ../ or /filestorage etc. $uploadfile = move_uploaded_file($temp, $path); // moving file from temp location to new chosen path // echo $uploadfile === TRUE ? '<br />File uploaded<br />' : '<br />Awaiting File Upload<br />'; // echo "<br />".$this->path_directory.$filename."<br />"; if($uploadfile === TRUE) { echo '<br />Congrats file uploaded<br />'; // These are optional outputs for testing purpose return TRUE; } else { echo '<br />Awaiting File Upload<br />'; // These are optional outputs for testing purpose return FALSE; } } } Class Image extends Uploader{ // Declaring my varaibles public $newpath; // This will be the new location of the thumbnail image public $new_height; // This parameter will specify the size of thumbnail function fetch_filename($input_field_name){ // simple utilty for testing purpose return $filename = $_FILES[$input_field_name]['name']; } function resizeimage($x) { // begin image resizer x is the fieldname $filename = $_FILES[$x]['name']; // generating the file name from the field name // echo $this->path_directory.$filename; // echo "<br />new path:".$this->newpath.$filename; // Tsting to see if is correct extension. $ext = strrchr($filename, '.'); switch ($ext) { case ".jpg": $im = imagecreatefromjpeg($this->path_directory.$filename); break; case ".gif": $im = imagecreatefromgif($this->path_directory.$filename); break; case ".png": $im = imagecreatefrompng($this->path_directory.$filename); break; default: $im = imagecreatefromstring(file_get_contents($this->path_directory.$filename)); } $get_image = $im; // assigning get image to im variable // if($get_image === True) { echo "get image is true!";} if ($get_image === false) { die ('Unable to open image'); // scipt ends if stuff goes wrong } // Get original width and height $width = imagesx($get_image); $height = imagesy($get_image); // Set a new width, and calculate new height $new_width = $width * ($this->new_height/$height); // Resample the image $image_resized = imagecreatetruecolor($new_width, $this->new_height); imagecopyresampled($image_resized, $get_image,0 , 0,0 ,0 , $new_width, $this->new_height, $width, $height); //imagejpeg($image_resized_big, $path_thumb); imagejpeg($image_resized, $this->newpath.$filename); } // end of image resizer } ?> here is the test form: <?php // This is the test upload form include_once("uploader_bk.php"); // importing the Uploader class //============================ VARAIBLE DECLARATIONS =============================== $myupload = new Image(); // declaring a new uploader object $myupload->file_size = 400000; // setting a different file size $myupload->path_directory = "myfiles/"; // You can mod the path directory at any time, leave blank if is on the same path as the script $myupload->action_path_and_file = $_SERVER['PHP_SELF']; // use $_SERVER['PHP_SELF'] if its the same script that is doing the processing $fieldname = array("joe", "Mary"); // This can apply to one file you may need to prefix variable names to add more fields; this could also be an include $max_fields = count($fieldname); // getting the maximum number of fields that you need could be an include $myupload->newpath = $myupload->path_directory."thumbs/"; // The path for the thumbs directory $myupload->new_height = 100; // the new height parameter for the thumbnail image //================================================================================== ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Handling File Uploads</title> </head> <body> <form enctype="multipart/form-data" method="post" action="<?php echo $myupload->action_path_and_file; // path to the process script ?>"> <?php // Simple for loop that will repeat the steps based off the max_fields varaible. for ($i=0 ; $i < $max_fields; $i++) { $myupload->file_field($fieldname[$i]); // input field. $upload = $myupload->upload($fieldname[$i]); // file uploader if($upload === TRUE) { // Testing to see if upload variable returns a true value $myupload->resizeimage($fieldname[$i]); // image resizer } } ?> <input type="submit" value="Send File" /> </form> </body> </html> my next mission is to fuse it with a MySQL Class so that i can reference the files in a database and also a remover function that will remove my files from both the server and the database, some assistance / guidance will be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1048303 Share on other sites More sharing options...
trq Posted April 26, 2010 Share Posted April 26, 2010 Why would Image extend Uploader? They don't seem at all related. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1048307 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2010 Share Posted April 26, 2010 Your upload() method does not check if the form was submitted and so executes all the upload processing code when the form is requested, thereby generating a number of errors. The upload() method also does not test for any of the possible upload errors before blindly attempting to access any of the $_FILES data. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1048310 Share on other sites More sharing options...
jwmstudios Posted April 26, 2010 Author Share Posted April 26, 2010 i know, alot of bugs are there, but its a class that can be built on. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1048334 Share on other sites More sharing options...
jwmstudios Posted April 26, 2010 Author Share Posted April 26, 2010 "Why would Image extend Uploader? They don't seem at all related." its simple, uploader uploads the file and image manipulates the file uploaded, you can have a subclass called videos extending from uploader as well as pdf and other mime types etc. its unlimited ideas that can be spawned. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1048335 Share on other sites More sharing options...
trq Posted April 26, 2010 Share Posted April 26, 2010 "Why would Image extend Uploader? They don't seem at all related." its simple, uploader uploads the file and image manipulates the file uploaded, you can have a subclass called videos extending from uploader as well as pdf and other mime types etc. its unlimited ideas that can be spawned. My point however is that the objects aren't related, they should be used as separate objects. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1048341 Share on other sites More sharing options...
jwmstudios Posted May 31, 2010 Author Share Posted May 31, 2010 i understand now i should of separated the classes and let them interact. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1065901 Share on other sites More sharing options...
ignace Posted June 1, 2010 Share Posted June 1, 2010 "Why would Image extend Uploader? They don't seem at all related." its simple, uploader uploads the file and image manipulates the file uploaded, you can have a subclass called videos extending from uploader as well as pdf and other mime types etc. its unlimited ideas that can be spawned. From your description, Image looks like a plugin to your Uploader class. Also separate HTML from your PHP, your uploader only needs to know the field and if it's an array. Your Uploader package should contain these classes at a minimum: Uploader -- The main uploader class Uploader_Abstract -- Façade for the actual upload process (final), does not define the methods in the _Interface Uploader_Interface -- defines the methods used by the Façade Uploader_File -- Representation of a uploaded File, allows for easy operations on a file (eg hasExtension(array('jpg', 'jpeg', ..)), isValid(), ..) Uploader_Plugin_Image -- Takes in a Uploader_File verifies its an image and performs resizing Uploader_Plugin_Upload -- performs the actual uploading and is auto-appended by the Uploader class on the upload() call Uploader_Plugin_Abstract -- Used to add more plugins in the future (eg PDF, Video, ..) As you can see it contains a Uploader_Plugin_Upload class which performs the actual uploading, this is to allow uploads of the original file and a thumbnail. $upload->addPlugin(new Uploader_Plugin_Upload('images')) ->addPlugin(new Uploader_Plugin_Image('50%')) ->addPlugin(new Uploader_Plugin_Upload('images/thumbnails')) ->upload('file', TRUE); Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1066014 Share on other sites More sharing options...
jwmstudios Posted October 21, 2010 Author Share Posted October 21, 2010 Thanks for the tip, i see what you are saying, i am working on a new uploader. Quote Link to comment https://forums.phpfreaks.com/topic/199707-my-first-shot-at-writing-a-class/#findComment-1124813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.