Image Upload Class

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Image Upload Class

Post by infolock »

This is a small class to upload images on a *nix server. Enjoy

Code: Select all

<?php
/**
 * @Author : Jonathon Hibbard
 *
 * Provides File Management Services (Upload, Copy, Create, MkDIR, etc.)
 */
class File_Upload_Manager {
  /**
   * Uploads the file and returns the result
   *
   * @param string $img_name  //name of image
   * @param string $path      //path we want to use for uploading
   * @param string $postFile  //name of image field in form
   * @return $pic             //image information
   */
  function upload_image($path = NULL, $postFile = NULL) {
    global $HTTP_POST_FILES;
    $img_name =& basename($HTTP_POST_FILES[$postFile]['name']);
    $max_size=1000000;
    if(isset($HTTP_POST_FILES[$postFile]['name']) && !empty($HTTP_POST_FILES[$postFile]['name'])) {
      $file_check = $path.$HTTP_POST_FILES[$postFile]['name'];
      if(file_exists($file_check)) {
        $temp_filename = basename($file_check,'.jpg');
        $temp_filename .= date("YmdHis").'.jpg';
        $new_path = $path.$img_name.'/'.$temp_filename;
        rename($file_check,$new_path);
      }
    }
    if(!is_dir($path.$img_name))
      mkdir($path.$img_name,0775);
    $path = $path.$img_name;
    if(is_uploaded_file($HTTP_POST_FILES[$postFile]['tmp_name'])) {
      if($HTTP_POST_FILES[$postFile]['size'] > $max_size)
        die($this->jsAlert("The file '.$HTTP_POST_FILES[$postFile]['name'].' is too big.  It must be smaller than 1mb.  Upload Failed!"));
      $this->copyImage(&$postFile, &$path);
    }
    if($HTTP_POST_FILES[$postFile]['name'])
      $pic = $path.$img_name.'/'.basename($HTTP_POST_FILES[$postFile]['name']);
    else
      $pic = 'No Picture';
    return $pic;
  }
  /**
   * Copies the Image and removes any images remaining that may have the same name...
   *
   * @param string $field_name //form's field name of image
   * @param string $path       //Path to directory
   */
  function copyImage($field_name = NULL,$path = NULL) {
    global $HTTP_POST_FILES;
    if($HTTP_POST_FILES[$field_name]['type']=="image/jpeg" || $HTTP_POST_FILES[$field_name]['type']=="image/pjpeg" || $HTTP_POST_FILES[$field_name]['type']=="image/jpg" || $HTTP_POST_FILES[$field_name]['type']=="image/gif") {
      if(file_exists($path.$HTTP_POST_FILES[$field_name]['name']))	{
        unlink($path.$HTTP_POST_FILES[$field_name]['name']);
      }
      $res = copy($HTTP_POST_FILES[$field_name]['tmp_name'], $path.$HTTP_POST_FILES[$field_name]['name']);
      if(!$res)
        die($this->jsAlert("Upload Failed on line ".__LINE__."!"));
    } else
      die($this->jsAlert($field_name.' : Wrong File Type.  The image must be a JPEG'));
  }
  function jsAlert($message) {
    echo '<script language="javascript">
          alert("'.$message.'");
          </script>';
  }
}
?>
Example Usage

Code: Select all

$fileMan = new File_Upload_Manager();
$image_path_result = $fileMan->('name_of_image_on_form');
Last edited by infolock on Wed Sep 14, 2005 10:51 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$image_path_result = $fileEMan->('name_of_image_on_form');
$fileEMan-> should be $fileMan->

Also, isn't $HTTP_POST_FILES a global already?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_FILES is a superglobal, $HTTP_POST_FILES is not, but it is a copy of $_FILES (provided long vars ini setting is on ;))
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Never used $HTTP_POST_FILES before :roll:
Thanks for the correction
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

feyd hit the head on the nail. i chose to use $HTTP_POST_FILES for backward compatabiliy reasons. We have some servers that have globals turned on, and some turned off. some running php 3 and the others 4. $_FILES is a better method to the depreciated $HTTP_POST_FILES, but again it's for simplistic and compatability sakes.

btw, thanks for the heads up
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

what sort of changes would you need to make to this script to make it run on a windows server...
i know windows servers aren't very hip or whatnot, but it is what my company has...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

provided you are running at least PHP 4, it should be fine.. I don't see anything in the code that would stop it from working (basically) with a Windows server, provided file uploading is turned on and the directories required for it are set up correctly.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

the only thing that could cause any problems is using forward slashes in the path for mkdir.

linux uses forward slashes, windows uses backslashes. so extra care must be taken in this instance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could always use DIRECTORY_SEPARATOR ;)
Post Reply