PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 07, 2020 11:55 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Image Upload Class
PostPosted: Wed Sep 14, 2005 9:45 am 
Offline
DevNet Resident
User avatar

Joined: Wed Sep 25, 2002 7:47 pm
Posts: 1708
This is a small class to upload images on a *nix server. Enjoy

Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
$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.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 11:08 am 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Syntax: [ Download ] [ Hide ]
$image_path_result = $fileEMan->('name_of_image_on_form');

$fileEMan-> should be $fileMan->

Also, isn't $HTTP_POST_FILES a global already?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 11:20 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 11:39 am 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 10:51 pm 
Offline
DevNet Resident
User avatar

Joined: Wed Sep 25, 2002 7:47 pm
Posts: 1708
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 11:28 am 
Offline
Forum Contributor
User avatar

Joined: Thu Aug 18, 2005 8:30 pm
Posts: 317
Location: Toronto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 12:24 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 7:58 am 
Offline
DevNet Resident
User avatar

Joined: Wed Sep 25, 2002 7:47 pm
Posts: 1708


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 8:04 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
could always use ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group