Problem with uploader

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_nov
Forum Newbie
Posts: 1
Joined: Sat Jul 25, 2009 3:06 am

Problem with uploader

Post by php_nov »

ok so basically I have an image uploader in my page and it is giving me troubles :banghead: :banghead: :banghead:

When I first got the script working it had the problem of submitting the same image to mysql whenever the user hit refresh. I fixed this problem with a header that automatically redirected the page after the image was uploaded but now my errors don't show up. this is the upload script that is giving me the headache

Code: Select all

 
 
<?php
 
 
 
if($_FILES['userfile']['size'] > 1) {
 
// set some variables for the maximum parameters
    $max_size = 700000;
    $max_height = 800;
    $max_width = 800;
 
 
// get information on the file that was uploaded
    // get filesize
        $info = getimagesize($_FILES['userfile']['tmp_name']);
 
// filter through limitations set earlier 
    //check file-size (in bytes):   
    if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) {        
        $error_size = 1;
        echo "<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> you";
    die("<BR><BR>Error: Upload file size too large: (<b>" . $_FILES['userfile']['size'] . "</b>). Must not exceed ".$max_size."kb.");
       unset ($_FILES);
 
        }
        
    //check the extension.
     $array = explode(".", $_FILES['userfile']['name']); 
     $nr    = count($array); 
     $ext  = $array[$nr-1];
     if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg")) {
     $error_ext = 1;
     die("<BR><BR>Error: file extension un-recognized. Be sure your image follows the correct extension (.JPG or .PNG)");
        unset ($_FILES);
     }
    //CHECK TYPE: (what the browser sent)
    if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png")) {
    $error_extension = 1;
    die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
       unset ($_FILES);
    }
    
    //DOUBLE CHECK TYPE: if image MIME type from GD getimagesize() -In case it was a FAKE!                          
    if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) {
    $error_extension2 = 1;
    die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
       unset ($_FILES);
    }
    
    //check file size (length & width)
    if(($info[0] > $max_width) || ($info[1] >$max_height)) {
    $error_height = 1;
   die("<BR><BR>Error: Image size error (<b>" . $info[0] . "</b> x <b>" . $info[1] . "</b>). Must not exceed ". $max_height . " x ". $max_width .".");
   unset ($_FILES);
    }
    
    //check if a file was loaded
    if ($_FILES['userfile'] == NULL) {
        echo "You didn't upload anything";
        die("Your upload was empty");
 
    }
 
 
As you can see the first line of code looks for a file submission and, because of my header, this will never evaluate to true. Once the file is processed the page is automatically redirected so the user will never see the error codes. The images still show up fine assuming they pass all the requirements in the brackets. I have tried a lot of methods and none has worked. i either get the refresh issue without the header or I get the errors not displaying issue with the header so i
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problem with uploader

Post by jackpf »

Sorry....what do you want to do exactly?
Post Reply