Jump to content


Photo

Image Upload (SOLVED)


  • Please log in to reply
11 replies to this topic

#1 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 29 September 2006 - 11:33 PM

I need an image upload script that checks if a file uploaded is infact an image and not a script.
Also, I'd like to be able to define allowed file types easily using (define("FILE_TYPES", "gif, jpeg, jpg, png");)

I know this is a rather straight forward and "give me the code" question.
However, I have tried making one for awhile now and finally gave in to asking for one.

Also, is it possible to do checks before you move the file? (Ex. check width, height, and filesize)
If no, is there a way to delete the file if a check fails?

#2 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 September 2006 - 02:01 AM

bump ^^
Anyone? Please.

#3 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 30 September 2006 - 03:38 AM

Have you looked at the tutorial here at phpfreaks?

http://www.phpfreaks...orials/85/0.php

It is tutorial on a file upload class with file extension validation, getting sizes and many other things.

Jeremy

#4 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 September 2006 - 01:29 PM

I tried that and I cant even get the file extention to verify...
I made sure .gif was in the ext array and it returns false every time, I removed the ext array validation and started getting file already exists error...

I can't get that class to work properly...

#5 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 September 2006 - 08:52 PM

bump ^^
Anyone? I need this resolved asap if possible, kinda stuck on my project until this part is finished...

I wouldnt be surprised if the answer is in the comments on the bottom of the tutorial's page but when I click on them it refreshes the page but does not display the comments...

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 September 2006 - 09:00 PM

use the getimagesize() function to check width and height. It also checks the filetype so that even something named wombat.jpg will fail if it isn't a real jpg file (and similarly for any image type you want).

http://fundisom.com/...load_and_resize

is lixlpixel's script that works 'straight out of the box'.

That should get you moving in the right direction.
Legend has it that reading the manual never killed anyone.
My site

#7 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 September 2006 - 09:12 PM

Thing is I'm working with functions and classes already. If someone can help me determine the reason I am receiving errors on the class that was given to me above, I'd have everything working.
Thanks

Also, I don't want a complex script that makes a thumbnail. I am only working with images/gif's of the size 100x100px

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 September 2006 - 10:34 PM

OK, so in fact you don't want help with the things you asked about. You want someone to explain why the code we can't see doesn't work, right?
Legend has it that reading the manual never killed anyone.
My site

#9 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 September 2006 - 10:53 PM

The code you cant see is the code from the tutorial that was given in the link above.  :)

http://www.phpfreaks...orials/85/0.php

Anyway, here is my code that calls the class

Basicly I have the form element -> process.php -> class_upload.php

Form POST's to process.php
process.php calls function addBanner()
function addBanner:
  /**
   * addBanner - Uploads the file and executes the MySQL Query to update the database
   */
  function addBanner() {
    global $upload_files, $database, $session, $form;
		
    $upload_class                =new Upload_Files;
    $upload_class->temp_file_name=trim($_FILES['upload']['tmp_name']);
    $upload_class->file_name     =trim(strtolower($_FILES['upload']['name']));
    $upload_class->upload_dir    =IMG_PATH;
    $upload_class->upload_log_dir="" . IMG_PATH . "/upload_logs/";
    $upload_class->max_file_size =MAX_IMG_SIZE;
    $upload_class->banned_array  =array("");
    $upload_class->ext_array     =array(".gif",".jpg",".jpeg",".png");
    $valid_ext                   =$upload_class->validate_extension();
    $valid_size                  =$upload_class->validate_size();
    $valid_user                  =$upload_class->validate_user();
    $max_size                    =$upload_class->get_max_size();
    $file_size                   =$upload_class->get_file_size();
    $file_exists                 =$upload_class->existing_file();
    $field                       ="userfile"; //Use field name for username
    if ($valid_ext) {
      $form->setError($field, "* Invalid file type");
      $retval=1;
    } elseif(!$valid_size) {
      $form->setError($field, "* Invalid file size");
      $retval=1;
    } elseif(!$valid_user) {
      $form->setError($field, "* Banned from uploading");
      $retval=1;
    } elseif($file_exists) {
      $form->setError($field, "* File exists");
      $retval=1;
    } else {
      $upload_file=$upload_class->upload_file_with_validation();
      if (!$upload_file) {
        $form->setError($field, "* Error during upload");
        $retval=2;
      } else {
        $retval=0;
      }
    }
    /* Upload Successful */
    if ($retval == 0) {
      if ($database->addBanner($cid, $uid, $newfile)) {
        return 0; //Query Successfil
        $_SESSION['reguname']  =$_POST['file'];
        $_SESSION['regsuccess']=true;
        header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]");
      } else {
        return 2; //Error with query
        $_SESSION['reguname']  =$_POST['user'];
        $_SESSION['regsuccess']=false;
        header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]");
      }
    }
    /* Error found with form */
    else if($retval == 1) {
      $_SESSION['value_array']=$_POST;
      $_SESSION['error_array']=$form->getErrorArray();
      header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]");
    }
    /* Upload attempt failed */
    else if($retval == 2) {
      $_SESSION['reguname']  =$_POST['user'];
      $_SESSION['regsuccess']=false;
      header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]");
    }
  }


#10 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 30 September 2006 - 11:03 PM

Tell me exactly what you will use it for, and I will give a quick example! I have a bunch of ready made examples, so give me an idea of what one would best help you! What mean is, tell me what you need it to support, (ie: multi upload, restrict size, restrict type, ...)

me!

#11 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 30 September 2006 - 11:12 PM

Tell me exactly what you will use it for, and I will give a quick example! I have a bunch of ready made examples, so give me an idea of what one would best help you! What mean is, tell me what you need it to support, (ie: multi upload, restrict size, restrict type, ...)

me!


Ok, lets see how I can best describe this.
I am creating a BannerExchange that uses 100x100 images, but I want these variables to be easily changed in a config file.
I want to upload images, again would be nice to have it as an easy access array in a config file with extensions allowed.
I need it to verify that it is an image and not a script (Security measure)
I need it to check that the size is less than a defined size in config. (51200bytes - 50kb/ default)
I need it to then, after the checks, copy the temp file to a permanent location with a name I can easily specify.
(ex. $id_$cid_$uid.$ext - BannerID_ChannelID_UserID.Extension)
Last but not least I need to be able to implement it into my system.
Form -> process.php -> funtion addBanner

#12 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 01 October 2006 - 01:54 AM

Ok, finally managed to debug each function 1 by 1 and fixed the errors.
Thanks for the referral to that tutorial/class!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users