Announcement

Collapse
No announcement yet.

PHP Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Help

    For you PHP experts out there...

    [code:1] # ----------------------------------- #
    # FUNCTION: upload
    # ARGS: $filename, $accept_type, $extension
    # DESCRIPTION: Checks and sets the raw
    # file data, checks if the content type is
    # acceptable (if $accept_type) is set, and
    # adds a default extension ($extention) if
    # there is no ".xxx" in the filename
    # ----------------------------------- #
    function upload($filename, $accept_type='', $extention='') {
    // get all the properties of the file
    $index = array("file", "name", "size", "type");
    for($i = 0; $i < 4; $i++) {
    $file_var = '$' . $filename . (($index[$i] != "file") ? "_" . $index[$i] : "");
    eval('global ' . $file_var . ';');
    eval('$this->file[$index[$i]] = ' . $file_var . ';');
    }

    if($this->file["file"] && $this->file["file"] != "none") {
    // test max size
    if($this->max_filesize && $this->file["size"] > $this->max_filesize) {
    $this->errors[1] = "Maximum file size exceeded. File may be no larger than " . $this->max_filesize/1000 . "KB (" . $this->max_filesize . " bytes).";
    return FALSE;
    }
    if(ereg("image", $this->file["type"])) {
    $image = getimagesize($this->file["file"]);
    $this->file["width"] = $image[0];
    $this->file["height"] = $image[1];

    // test max image size
    if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
    $this->errors[2] = "Maximum image size exceeded. Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels";
    return FALSE;
    }
    switch($image[2]) {
    case 1:
    $this->file["extention"] = ".gif";
    break;
    case 2:
    $this->file["extention"] = ".jpg";
    break;
    case 3:
    $this->file["extention"] = ".png";
    break;
    default:
    $this->file["extention"] = $extention;
    break;
    }
    } elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) {
    // add new mime types here
    switch($this->file["type"]) {
    case "text/plain":
    $this->file["extention"] = ".txt";
    break;
    default:
    break;
    }
    } else {
    $this->file["extention"] = $extention;
    }

    // check to see if the file is of type specified
    if($accept_type) {
    if(ereg($accept_type, $this->file["type"])) {
    $this->accepted = TRUE;
    } else {
    $this->accepted = FALSE;
    $this->errors[3] = "Only " . ereg_replace("\|", " or ", $accept_type) . " files may be uploaded";
    }
    } else {
    $this->accepted = TRUE;
    }
    } else {
    $this->accepted = FALSE;
    $this->errors[0] = "No file was uploaded";
    }
    return $this->accepted;
    } [/code:1]


    for some reason, this will not recognise JPG files as a valid file extention, even when passed image/jpeg and image/jpg in accept_type

    any clues?
    Ngreth Thergn

    Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
    Grandmaster Smith 250
    Master Tailor 200
    Ogres not dumb - we not lose entire city to froggies

  • #2
    I'm no expert...

    but I'll take a shot anywho:

    1) Is only jpg/jpeg not working? GIF and PNG are fine?

    2) there is a period in the extension you're checking for, is that correct?

    3) you have a variable named $extention, but shouldn't the attribute reference ($this->file["extention"]) be "extension"?

    Sorry it t'aint working...
    Mathir Thunderoak- Wood Elf Druid of Tunare // Taleen Bloodaxe - Barbarian Warrior of the Tribunal // Cendaar - Human Bard of Karana /// Ordo Ursi Prismatus - Lanys T'Vyl - EQ
    B3 t+ h+ r+ w+ s- g+ (It's a bear thing...)

    Comment


    • #3
      I recognize this code It's very similar to the file upload I use, including the spelling of the variable name "extention".

      My first question would be what is the output when you try to upload a jpg ("you may only upload XXX files")? This gives a clue as to what the code thinks are valid file types.

      Second, try adding an echo statement to see what image type and extension the code thinks you're trying to upload. All the file type magic is done via the getimagesize php function.

      I'm not seeing anything inherently wrong with the code, but it's 1:30am here and I'm a tad sleepy. Compare it with the fileupload class from David Tufts, which I think works.
      Zallarenya
      Coercer of the Underfoot
      Druzzil Ro Server

      Comment


      • #4
        Hehe I have no clue I just know it won't work. *giggle*

        Will try again and get you exactly error text...

        Tried to send a jpg and got:

        Only image/gif or image/jpeg or image/png files may be uploaded

        Could it only recognize jpeg not jpg? They are usually interchangable I thought?
        [75 Exemplar] Jenarie (Dark Elf) < Primal Brood > Test
        [65 Archon] Ariene (High Elf) Bristlebane (retired)

        Comment


        • #5
          A shot in the dark here, but try adding image/pjpeg into $accept_type
          Zallarenya
          Coercer of the Underfoot
          Druzzil Ro Server

          Comment


          • #6
            I had a whole long response that got eaten...

            but...

            the pjpeg fix worked
            Ngreth Thergn

            Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
            Grandmaster Smith 250
            Master Tailor 200
            Ogres not dumb - we not lose entire city to froggies

            Comment


            • #7
              /dance

              I have become better at Archery in the Dark! (24)
              Zallarenya
              Coercer of the Underfoot
              Druzzil Ro Server

              Comment


              • #8
                Next question - why'd that work and not when using just jpeg or jpg?
                Slimb Hcarvul
                Cazic-Thule

                Comment


                • #9
                  for some reason PHP is identifying the filetype as image/pjpeg

                  got me why it does that...

                  (I added a whol bunch of debuggin echo's and got that as a filetype when I tried to upload the file.)
                  Ngreth Thergn

                  Ngreth nice Ogre. Ngreth not eat you. Well.... Ngreth not eat you if you still wiggle!
                  Grandmaster Smith 250
                  Master Tailor 200
                  Ogres not dumb - we not lose entire city to froggies

                  Comment


                  • #10
                    http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?PJPEG

                    Maybe it really is a pjpeg
                    Canto Wolfheart -- 64th Wood Elf Druid of Tunare
                    Baking 200, Tailoring 245 (Trophy), Blacksmithing 191, Fletching 200, Brewing 200, Jewelcraft 200, Pottery 204, Fishing 192, Alchohol Tolerance 200, Begging 140

                    Comment


                    • #11
                      Just to mention:

                      getimagesize($filename)

                      on php.net:
                      http://www.php.net/manual/en/function.getimagesize.php

                      Is a great tool that returns an array including the image type of your file, ie:

                      [code:1]
                      &lt;?

                      // where $img is an uploaded file pointer from $_FILES array
                      $img_tmp = getimagesize&#40;$img&#91;'tmp_name'&#93;&#41;;

                      $img_x = $img_tmp&#91;0&#93;;
                      $img_y = $img_tmp&#91;1&#93;;
                      $img_type = $img_tmp&#91;2&#93;;
                      [/code:1]
                      $img_type will be one of the following (numerical value):

                      1 = GIF
                      2 = JPG
                      3 = PNG
                      4 = SWF
                      5 = PSD
                      6 = BMP
                      7 = TIFF(intel byte order)
                      8 = TIFF(motorola byte order)
                      9 = JPC
                      10 = JP2
                      11 = JPX
                      12 = JB2
                      13 = SWC
                      14 = IFF

                      I'm working with a large database engine for images through PHP right now, if you have further questions feel free to ask.

                      btw, sorry about the site move. Server h10009 was suffering from a serious database overload and needed some of the more popular sites to be shared amongst other servers. You should no longer see any slowdown on eqtraders' message board.

                      Sincerely,

                      -Matt
                      Matt Meier, RackNine Inc.
                      email: mmeier@racknine.com
                      web: http://www.racknine.com

                      Comment

                      Working...
                      X