wrathican Posted October 9, 2008 Share Posted October 9, 2008 Hi people. I'm kinda new to OOP but have been using php for about 3 years. I'm trying to create a file upload class. It works. It uploads and renames files selected by the user, it can even handle multiple file uploads. The problem i'm having is that when i use it, it creates two of each file. i append a random generation of characters at the end of the name to prevent any overwriting of files and the class uploads two of the same files with a different series of chars. this is my class: <?php ############################################# ############# File Upload Class ############# ############################################# ############################################# # Purpose: # To manage the upload of a file. # Can be used to upload any file to a server # and can also manage the processing of an # image to be able to rezise it. This class # will make use of the GD Library functions. # If GD is not enabled this script will # die. # # # Files that require this will also require # the database.php class if they intend on # storing the file imformation into a # database. # # # This will also need to require the page.php # class because it uses the generatePassword # function to append a random series of # letters and numbers to the end of # a filename. # ############################################# ## First attempt at a class. Hope i dont fuck it up. class UploadFile { // General File attributes var $file_src_name; # Original name of file on clients machine var $file_src_type; # Mime type of file being uploaded var $file_tmp_name; # Location and name of the temporary file var $file_src_error; # Error code given from $_FILES var $file_src_size; # Size in bytes of the file // Extra File Attributes var $file_dst_path; # Path where the file is to be moved var $file_dst_pathname; # array containing all the full names e.g: some/path/somefile.ext // Image specific attributes var $image_src_x; # Width of original image var $image_dst_x; # Width of destination image var $image_src_y; # Height of original image var $image_dst_y; # Height of Destination image var $image_src_type; # File type of orginal image # Set the General File Attributes # Upload the file(s) function uploadFiles ($array, $path) { # $array will be $_FILES. If $array is a multidimensional # array, each GFA will be an array. # $path will be destination filepath for file // GFA $this->file_src_name = $array['name']; $this->file_src_type = $array['type']; $this->file_tmp_name = $array['tmp_name']; $this->file_src_error = $array['error']; $this->file_src_size = $array['size']; //EFA $this->file_dst_path = $path; $this->file_dst_pathname = array(); // loop through for($i = 0; $i < count($this->file_src_name); $i++) { // this is where the magic happens if($this->file_src_name[$i] != '') { // behold! a file to upload! //get ext $ext = substr(strrchr($this->file_src_name[$i], '.'), 0); // get basename $basename = basename($this->file_src_name[$i], $ext); //create random series of chars $rand = '-' . generatePassword(8,1); // create fullname $this->file_dst_pathname[] = $this->file_dst_path . $basename . $rand . $ext; // upload the file move_uploaded_file($this->file_tmp_name[$i], $this->file_dst_pathname[$i]); } // if } // for } // function function delete ($imagePath) { for($i = 0; $i < count($imagePath); $i++) { $delete = unlink($imagePath[$i]); if($delete) { $error = 0; // ??? }else{ $error = 2; // ??? } // if } // for } // function //image specific functions function image_thumb ($image, $thumbWidth = 100, $newWidth = 700, $keepOrig = true) { // $image will be an array of file locations } // function } // class ?> This is how i am using it: <?php $f->uploadFiles($_FILES['image'], 'images/uploads/images/'); $foobar = $f->file_dst_pathname; echo '<pre>'; print_r($foobar); die('</pre>'); ?> the print_r is just for debugging. Help would be appreciated. Thanks Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/ Share on other sites More sharing options...
wrathican Posted October 10, 2008 Author Share Posted October 10, 2008 this was odd. The problem solved it's self Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-661806 Share on other sites More sharing options...
keeB Posted October 10, 2008 Share Posted October 10, 2008 This line is peculiar for($i = 0; $i < count($this->file_src_name); $i++) { If I am reading it properly, you're getting the count() of $this->file_src_name .. why would it ever be anything other than 1? I bet you if you figure that out the problem will really go away. Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-661887 Share on other sites More sharing options...
wrathican Posted October 10, 2008 Author Share Posted October 10, 2008 Thanks for replying, well the class is meant to handle multiple file uploads, which it does. and $this->file_src_name is the name of an array i use to access the names (file locations) of all the files that were uploaded. it is used for my image_thumb method in the same class. is it a bad idea to do this? im not all that savvy with OOP and it's my first (full) class and would appreciate advice Thanks Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-661911 Share on other sites More sharing options...
nadeemshafi9 Posted October 10, 2008 Share Posted October 10, 2008 u would recomend an ftp and ftp site functions to chmod the files and directories after ftping them be warned the paths are full in these functions Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-661914 Share on other sites More sharing options...
keeB Posted October 11, 2008 Share Posted October 11, 2008 FTP is not secure so I do not recommend that. im not all that savvy with OOP and it's my first (full) class and would appreciate advice I would break it in to 2 (or more) Classes. One would just represent a single file, while the other would represent the Collection of files. If you have any specific questions let me know Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-662451 Share on other sites More sharing options...
JasonLewis Posted October 11, 2008 Share Posted October 11, 2008 I would also recommend you use PHP5 OOP. Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-662454 Share on other sites More sharing options...
wrathican Posted October 11, 2008 Author Share Posted October 11, 2008 is the way i have written it not php5 oop style? if not why? telling me this only helps me improve my abilities. KeeB: i see what you mean about having a class that represents a single file, rather than a bunch, since one file is classed as an object. but wouldn't it be les productive to have two classes? wouldn't it be easier to a single class that can handle multiple files? Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-662676 Share on other sites More sharing options...
JasonLewis Posted October 11, 2008 Share Posted October 11, 2008 Check out this OOP tutorial on the main site. It's good. Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-662678 Share on other sites More sharing options...
corbin Posted October 12, 2008 Share Posted October 12, 2008 is the way i have written it not php5 oop style? if not why? telling me this only helps me improve my abilities. KeeB: i see what you mean about having a class that represents a single file, rather than a bunch, since one file is classed as an object. but wouldn't it be les productive to have two classes? wouldn't it be easier to a single class that can handle multiple files? Well if you want to look at it like that, wouldn't it be easier/faster to just do it procedurally? ;p Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-663203 Share on other sites More sharing options...
wrathican Posted October 12, 2008 Author Share Posted October 12, 2008 well that's kinda what I'm trying to get my head around. Ive read that tutorial on the main site, a bunch of times. I understand why use oop and 'how' to use it. I'm the type of person that has to do something before they fully understand it, and I don't mean by following the examples shown in the tutorial. That's why I'm trying it out in a way that I can relate to. This is my first time putting it into practise and I want to be able to do it properly. Not code procedurally inside a class. That's why I'm here. To ask for your help in making me a better programmer. So your advice on how I should do it is more than welcome. Now that was a pain in the ass to write out on a phone Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-663231 Share on other sites More sharing options...
keeB Posted October 12, 2008 Share Posted October 12, 2008 What you'll notice, is that as you program more your style will develop naturally. There are a billion different ways to do things. The proper way to do what you're asking would be to break it up in to multiple classes. Encapsulation is about delegating responsibility and keeping things relatively simple. Only by creating a combination of simple objects should you achieve complexity. Reason being, if any part of your requirements change, the changes should be isolated to a small portion of your code base. This phase is called Maintenance. Extending your application should also not impact the rest of your code base. Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-663413 Share on other sites More sharing options...
wrathican Posted October 16, 2008 Author Share Posted October 16, 2008 Well, I reformatted my upload class to just deal with a singular file: this is what i have come up with: <?php ############################################# ############# File Upload Class ############# ############################################# ############################################# # Purpose: # To manage the upload of a file. # Can be used to upload any file to a server # and can also manage the processing of an # image to be able to rezise it. This class # will make use of the GD Library functions. # If GD is not enabled this script will # die. # # # Files that require this will also require # the database.php class if they intend on # storing the file imformation into a # database. # # # This will also need to require the page.php # class because it uses the generatePassword # function to append a random series of # letters and numbers to the end of # a filename. # ############################################# ## First attempt at a class. Hope i dont fuck it up. - well ya did ## Second attempt - Including more delegation. class UploadFile { // General File attributes private var $file_src_name; # Original name of file on clients machine private var $file_src_type; # Mime type of file being uploaded private var $file_tmp_name; # Location and name of the temporary file private var $file_src_error; # Error code given from $_FILES private var $file_src_size; # Size in bytes of the file // Extra File Attributes private var $file_dst_path; # Destination path. E.g: /uploads/images/ public var $file_dst_pathname; # Full destination - to be accessed outside of the class. E.g: /uploads/images/filename.ext public function processUpload($file) { // this is the function that calls the other functions to upload a file $this->file_src_name = $file['name']; $this->file_src_type = $file['type']; $this->file_tmp_name = $file['tmp_name']; $this->file_src_error = $file['error']; $this->file_src_size = $file['size']; $this->reName(); $this->moveFile(); } // function private function reName() { // function to rename a file - this function // makes use of the generatePassword function // written in the page class. // ------------------------------------------ \\ // get extension - extension includes '.' $fileName = $this->file_src_name; $ext = substr(strrchr($fileName, '.'), 0); // get basename $basename = basename($fileName, $ext); // replace spaces $basename = ereg_replace(" ", "_", $basename); //strip ext to lower $ext = strtolower($ext); //create random series of chars $rand = '-' . generatePassword(8,1); /*// if file is image add sizes into name --- IGNORE if( 7 == 9 ) { // find image sizes $x = imagesx($fileName); $y = imagesy($fileName); $basename .= $x . 'x' . $y; }*/ // create fullname $this->file_dst_pathname = $this->file_dst_path . $basename . $rand . $ext; } // function private function moveFile () { // function to move the file from $tmp to $dir // $dir is the returned name from reName() $tmp = $this->file_tmp_name; $dir = $this->file_dst_pathname; $move = move_uploaded_file($tmp, $dir); } } // class //image specific functions class Images extends UploadFile { function image_thumb ($image, $thumbWidth = 200, $maxWidth = 700, $keepOrig = true) { // $image will be an array of file locations $this->image_thumb_path = $this->file_dst_path . 'thumb/'; $this->image_thumb_pathname = array(); for ($i = 0; $i < count($image); $i++) { // loop through each image $file = pathinfo($image[$i]); $basename = $file['basename']; $sizes = getimagesize($image[$i]); //ratio $ratio = $sizes[0]/$sizes[1]; //LS or PO if($sizes[0] > $sizes[1]){ //pic is landscape $newWidth = $thumbWidth; $newHeight = round($thumbWidth/$ratio); }else{ //pic is portrait $newWidth = round($thumbWidth*$ratio); $newHeight = $thumbWidth; } #die($image[$i]); //create true colour image $srcImg = imagecreatetruecolor($newWidth, $newHeight); if($ext == 'jpg' || $ext == 'jpeg') { $tmpImg = imagecreatefromjpeg($image[$i]); }elseif($ext == 'png') { $tmpImg = imagecreatefrompng($image[$i]); }elseif($ext == 'gif') { $tmpImg = imagecreatefromgif($image[$i]); } //create destination image filename //dest filename $dstFile = $this->image_thumb_path . 'thumb-' . $basename; imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $newWidth, $newHeight, $sizes[0], $sizes[1]); //save image if($ext == 'jpg' || $ext == 'jpeg') { imagejpeg($srcImg, $dstFile, 100); }elseif($ext == 'png') { imagepng($scrImg, $dstFile, 100); }elseif($ext == 'gif') { imagegif($srcImg, $dstFile, 100); } // destrony tmp + src images imagedestroy($srcImg); imagedestroy($tmpImg); echo "<br /><br /><br /><br /><br /><br />"; echo $basename . '<br />' . $sizes[0] . '<br />' . $sizes[1] . '<br />' . $ratio . '<br />' . $newWidth . '<br />' . $newHeight . '<br />' . $maxWidth . '<br />' . $dstFile; echo '<pre>'; print_r($image); echo('</pre>'); if(file_exists($image[$i])){ echo 'image is there'; } } // for die(); } // function } // class function createThumb($result, $picWidth, $imgDir) { $poo = array(); if ($result == false) { //file could not be uploaded $poo[0] = false; $poo[1] = 1; return $poo; }else{ //file was uploaded. create thumbnail //pathinfo $file = pathinfo($result); //find the .ext $ext = $file['extension']; //strip .ext to lower $ext = strtolower($ext); //get image sizes $sizes = getimagesize($result); //ratio $ratio = $sizes[0]/$sizes[1]; //LS or PO if($sizes[0] > $sizes[1]){ //pic is landscape $newWidth = $picWidth; $newHeight = round($picWidth/$ratio); }else{ //pic is portrait $newWidth = round($picWidth*$ratio); $newHeight = $picWidth; } //create true colour image $srcImg = imagecreatetruecolor($newWidth, $newHeight); if($ext == 'jpg' || $ext == 'jpeg') { $tmpImg = imagecreatefromjpeg($result); }elseif($ext == 'png') { $tmpImg = imagecreatefrompng($result); }elseif($ext == 'gif') { $tmpImg = imagecreatefromgif($result); }else{ $poo[0] = false; $poo[1] = 2; return $poo; } //create destination image filename //get filename $name = $file['basename']; //dest filename $dstFile = $imgDir . $name; imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $newWidth, $newHeight, $sizes[0], $sizes[1]); //save image if($ext == 'jpg' || $ext == 'jpeg') { imagejpeg($srcImg, $dstFile, 100); }elseif($ext == 'png') { imagepng($scrImg, $dstFile, 100); }elseif($ext == 'gif') { imagegif($srcImg, $dstFile, 100); }else{ $poo[0] = false; $poo[1] = 3; return $poo;; } $killSrc = imagedestroy($srcImg); $killTmp = imagedestroy($tmpImg); if($killSrc != false && $killTmp != false){ return $dstFile; }else{ $poo[0] = false; $poo[1] = 5; return $poo; } } } function copyFile($oldFile, $dstDir) { //find out basename of file $fileName = pathinfo($oldFile); $fileName = $fileName['basename']; //create new dstFile $newFile = $dstDir . $fileName; //copy file to tmp dir $result = copy($oldFile, $newFile); //check copy if ($result) { return $newFile; }else{ return false; } } ?> it's not been tested as of yet, but it should theoratically work comments? Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-667135 Share on other sites More sharing options...
nadeemshafi9 Posted October 17, 2008 Share Posted October 17, 2008 What you'll notice, is that as you program more your style will develop naturally. There are a billion different ways to do things. The proper way to do what you're asking would be to break it up in to multiple classes. Encapsulation is about delegating responsibility and keeping things relatively simple. Only by creating a combination of simple objects should you achieve complexity. Reason being, if any part of your requirements change, the changes should be isolated to a small portion of your code base. This phase is called Maintenance. Extending your application should also not impact the rest of your code base. nicely said Link to comment https://forums.phpfreaks.com/topic/127706-file-upload-class/#findComment-667919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.