problem adding image type to script

Discussion in 'PHP' started by ycpc55, Apr 8, 2012.

  1. #1
    hi
    I'm having a problem adding the only allow file types to this script i tried everything i can think of with no luck anyone have any idea how i can add this to the script? thanks...

    Upload Script:
    
    $MaxSize = "600000";
    if (isset($_POST['btn_update'])){
    function createRandomPassword() {
    $chars = "abcde!@#%^fghijkmnoABCDEFGHIJKpqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWZ!@#%^&";
    srand((double)microtime()*10000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 19) {
    $num = rand() % 60;
    $tmp = substr($chars, $num, 1);
    $pass = $pass . $tmp;
    $i++;
    }
    return $pass;
    }
    if ($_FILES['aMyUploads0']['size'] > $MaxSize || $_FILES['aMyUploads1']['size'] > $MaxSize || $_FILES['aMyUploads2']['size'] > $MaxSize)
    {
    echo "ERROR: File too big!";
    exit;
    }
    $aMyUploads = array();
    $password = createRandomPassword();
    foreach($_FILES as $aFile)
    {
    $newLocation = 'uploads/'.$password .$aFile["name"];
    if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation)))
    {
    $aMyUploads[] = $newLocation;
    } 
    else 
    {
    $aMyUploads[] = '';
    }
    }
    $connection = mysql_connect("localhost", "?????", "??????");
    mysql_select_db("??????", $connection);
    $insert = "INSERT INTO ]photos (image1, image2, image3) VALUES 
    (
    ' ".$aMyUploads[0]." ',
    ' ".$aMyUploads[1]." ', 
    ' ".$aMyUploads[2]." '
    )";
    $add_member = mysql_query($insert) or die(mysql_error()); 
    }
    PHP:
    Code Im Trying To Add:
    $allowed_filetypes = array(".jpg", ".gif", ".jpeg", ".png");
    
    $ext = substr($newLocation, strpos($newLocation,'.'), strlen($newLocation)-1);
    if(!in_array($ext,$allowed_filetypes))
    die('The file you attempted to upload is not allowed.');
    
    PHP:
     
    ycpc55, Apr 8, 2012 IP
  2. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #2
    Try this:

    
    $MaxSize = "600000";
    if (isset($_POST['btn_update'])){
    function createRandomPassword() {
    $chars = "abcde!@#%^fghijkmnoABCDEFGHIJKpqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWZ!@#%^&";
    srand((double)microtime()*10000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 19) {
    $num = rand() % 60;
    $tmp = substr($chars, $num, 1);
    $pass = $pass . $tmp;
    $i++;
    }
    return $pass;
    }
    if ($_FILES['aMyUploads0']['size'] > $MaxSize || $_FILES['aMyUploads1']['size'] > $MaxSize || $_FILES['aMyUploads2']['size'] > $MaxSize)
    {
    echo "ERROR: File too big!";
    exit;
    }
    $allowed_filetypes = array(".jpg", ".gif", ".jpeg", ".png");
    
    $ext = substr($newLocation, strpos($newLocation,'.'), strlen($newLocation)-1);
    if(!in_array($ext,$allowed_filetypes))
    {
    die('The file you attempted to upload is not allowed.');
    }
    
    $aMyUploads = array();
    $password = createRandomPassword();
    foreach($_FILES as $aFile)
    {
    $newLocation = 'uploads/'.$password .$aFile["name"];
    if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation)))
    {
    $aMyUploads[] = $newLocation;
    } 
    else 
    {
    $aMyUploads[] = '';
    }
    }
    $connection = mysql_connect("localhost", "?????", "??????");
    mysql_select_db("??????", $connection);
    $insert = "INSERT INTO ]photos (image1, image2, image3) VALUES 
    (
    ' ".$aMyUploads[0]." ',
    ' ".$aMyUploads[1]." ', 
    ' ".$aMyUploads[2]." '
    )";
    $add_member = mysql_query($insert) or die(mysql_error()); 
    
    PHP:
     
    geforce, Apr 10, 2012 IP
  3. chris1230

    chris1230 Member

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #3
    thanks for the information
     
    chris1230, Apr 10, 2012 IP