SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Quality Image Upload Script?

    Hi,

    I am trying to find a quality image upload script. I found one however it bundles all the errors together

    http://www.w3schools.com/php/php_file_upload.asp

    Is there a standard, reliable image upload script for PHP?

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try the script from this page: http://www.coursesweb.net/php-mysql/...dio-gallery_s2
    Checks the type (extension) and size of the files, width and height for images, before upload.
    It works for images, and audio files. Data like: Title, Description, File_path can be stored in mysql table.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Cheers dude, it comes across as being a bit OTT. Is there a simpler script which can be done on one page?

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,394
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You could either write one yourself which is not hard, modify one to your requirements or check out hotscripts.

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll check out hotscripts.

    I thought there would just be a standard, off the shelf upload script which is widely used.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try this script, it is very simple:
    Code:
    <?php
    // Simple PHP Upload Script:  http://www.coursesweb.net/php-mysql/
    
    $uploadpath = 'upload/';      // directory to store the uploaded files
    $max_size = 2000;          // maximum file size, in KiloBytes
    $alwidth = 900;            // maximum allowed width, in pixeli
    $alheight = 800;           // maximum allowed height, in pixeli
    $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        // allowed extensions
    
    if(isset($_FILES['fileup'])) {
      $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
      $type = end(explode('.', strtolower($_FILES['fileup']['name'])));
      list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
      $err = '';
    
      // Checks if the file has allowed type, size, width and height (for images)
      if(!in_array($type, $allowtype)) $err .= 'The file <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
      if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB';
      if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x 
      '. $alheight;
    
      // If no errors, upload the image, else, output the errors
      if($err == '') {
        if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
          echo 'File:<b> '. basename( $_FILES['fileup']['name']). '</b> succesfully uploaded:';
          echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
          echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
          if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
          echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';
        }
        else echo '<b>Unable to upload the file.</b>';
      }
      else echo $err;
    }
    ?> 
    <div style="margin:1em auto; width:333px; text-align:center;">
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
      Upload File: <input type="file" name="fileup" /><br/>
      <input type="submit" name='submit' value="Upload" /> 
     </form>
    </div>
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Brilliant thanks, I got this working straight away.

    It uploads the image but how do I associate an image to a user. Lets say a company registers and then uploads their company logo how do I associate it to that company?

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,394
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Add an extra line to the form asking for the company name and if you are using a database add the image location to the company name; as I pressume the company will have an address or something. Otherwise you could rename the image to the name of the company; although this could cause problems later.

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What I need to do is to add the link that is created to a column under the company. Haven't got a clue how to do that though!

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,394
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    This is where you want to start getting organised before you go to far.
    Do you have a form to create the company details? If so the image upload form can be added to that so all the details are added in one go. If you start doing it with two different forms things start to get messy. If the company details are already in the database you need to have some way to link this image to the company and so you will need the company name or ID to add to the form from the database..

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am building up together. So the form is new with both membership registration and image upload.

  12. #12
    SitePoint Member
    Join Date
    Jul 2012
    Location
    Leicester
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres a nice Upload class I wrote which makes it nice and easy to handle uploads.

    <?php



    /*



    Simply Websites UK

    Upload Class



    This makes uploading files much easier and handles security issues too.



    $upload = new Upload($_FILE[uploaded_file]);



    if($upload->isSafeFile()){



    $upload->process();



    echo $upload->filename;



    }





    */



    class Upload {



    private $file;



    public $error;



    public $image_extensions = array("gif","jpg","png","bmp","jpeg");



    public $safe_extensions = array("gif","jpg","jpeg","png","bmp","zip","doc","txt","pdf","xls","exe","wmv","avi","rar","tar");



    public $filename;



    public function __construct($file){

    $this->file = $file;

    }



    public function getFileExtension(){

    return strtolower(substr(strrchr($this->file[name],'.'),1));

    }



    public function isImage(){

    if( in_array($this->getFileExtension(),$this->image_extensions) ) return true;

    }



    public function isSafeFile(){

    if( in_array($this->getFileExtension(),$this->safe_extensions) ) return true;

    }



    public function process($dir=DIR_UPLOAD){



    if(!$this->filename){

    if(file_exists($dir.$this->file[name])){

    $this->filename = date('d-m-y_h-i-s',time()).'_'.$this->file[name];

    }

    else

    {

    $this->filename = $this->file[name];

    }

    }



    $move = move_uploaded_file($this->file[tmp_name],$dir.$this->filename);



    if($move){

    $log = new Log('upload');

    $log->write('File Uploaded: '.$this->filename);



    return true;

    }

    else

    {

    return false;

    $this->error = 'Error Uploading File';

    }





    }



    public function getSize(){

    if($this->file){

    return $this->file['size'];

    }

    }



    public function getOriginalFilename(){

    return $this->file['name'];

    }



    }



    ?>
    I just realised there are some lines in this that record logs. You can just remove these.

  13. #13
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,394
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You will pressumably have a form already so if you are going to use the code from MarPlo I would put your current form code into the form part of his code.
    You will then need to put your database code into the part of code starting if(isset($_FILES['fileup'])) { - Note the data from the form will only be uploaded if there is an image and it is a valid file type.
    Add the $uploadpath variable to your database insert code and it should work.

    There are some things I would change but it should get you started - Note the images will be saved into a folder called uploads that you will need to create and CHMOD to 755 or 777 depending on your server setup.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •