SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loading images problem

    everytime i upload my jpg images it gives me "Image Extension Unknown"

    here is my code...

    PHP Code:
    <?
    if ($_POST['mypost'])
    {
        
    $uploaddir ".";
        
    $imgfile_name $_POST['imgfile'];
        
    $pext getFileExtension($imgfile_name);
        
    $pext strtolower($pext);
        
    confirm($_POST['imgfile'].'-'.$pext);
        if ((
    $pext != "jpg")  && ($pext != "jpeg"))
        {
            print 
    "<h1>ERROR</h1>Image Extension Unknown.<br>";
            print 
    "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
            print 
    "The file you uploaded had the following extension: $pext</p>\n";
            
    unlink($imgfile);
            exit();
        }
        
    $imgsize GetImageSize($imgfile);
        if ((
    $imgsize[0] > 250) || ($imgsize[1] > 200)) 
        {
            
    $tmpimg tempnam("/tmp""MKUP");
            
    system("djpeg $imgfile >$tmpimg");
            
    system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
            
    unlink($tmpimg);
        }

        
    $final_filename str_replace(" ""_"$imgfile_name);
        
    $newfile $uploaddir "/$final_filename";
        
        if (
    is_uploaded_file($imgfile))
        {

           if (!
    copy($imgfile,"$newfile")) 
           {
              print 
    "Error Uploading File.";
              exit();
           } else
           {
               print 
    "Uploaded Successful";
           }
         } else
         {
             print 
    "Error Uploading File.";
         }

        
    unlink($imgfile);  
        print(
    "<img src=\"$final_filename\">");
    }
    ?>


    </head>
    <body bgcolor="#FFFFFF">

        <h2>Upload and Resize an Image</H2>

        <form action="<?=$SCRIPT_NAME?>" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="50000">

        <p>Upload Image: <input type="file" name="imgfile"><br>
        <font size="1">Click browse to upload a local file</font><br>
        <br>
        <input type="submit" value="Upload Image" name="mypost">
        </form>

    </body>
    </html>

    <?
        
    function getFileExtension($str) {
            
    $i strrpos($str,".");
            if (!
    $i) { return ""; }

            
    $l strlen($str) - $i;
            
    $ext substr($str,$i+1,$l);

            return 
    $ext;

        }
    ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,426
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Try
    PHP Code:
            print "<h1>ERROR</h1>$pext Image Extension Unknown.<br>"
    to see what the value is

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the $pext value is blank

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,426
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    image script problem

    Because this portion of code is inside the "if ($_POST['mypost'])", it is most likely a problem with the line
    PHP Code:
    $pext getFileExtension($imgfile_name); 
    Either the getFileExtension function is in a file that isn't being include-d/require-d, or there is a problem with the function's script. If so, please post it.

  5. #5
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the problem is none of the above. When you do file uploads the $_FILES array is used for all the file details not the $_POST array. If you have any details about the file in your $_POST array this is because enctype="multipart/form-data" is missing from your form tag.
    Last edited by bokehman; Aug 24, 2005 at 08:18.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,426
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    image upload problem

    Good spot by bokehman. The problem is because the code is using $_POST instead of $_FILES. The form tag does have - enctype="multipart/form-data" - but the value of $_POST['imgfile'] does not have an extension to get. Try
    PHP Code:
    print "<h1>ERROR</h1>$imgfile_name Image Extension Unknown.<br>"
    to see what the value is

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is your script working but with a few minor changes:
    PHP Code:
    <?php

    echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body bgcolor="#FFFFFF">'
    ;



    if (
    $_POST['mypost'])
    {
        
    $uploaddir ".";
        list(
    $orig_name$ext) = spliti('\.'$_FILES['imgfile']['name']);
        if(
    $_FILES['imgfile']['error']){
            if(
    $_FILES['imgfile']['error'] == '3' or '4') die('No file uploaded.</body></html>');
            if(
    $_FILES['imgfile']['error'] == '1' or '2') die('Maximum file size exceeded.</body></html>');
        }
        if(!
    is_uploaded_file($_FILES['imgfile']['tmp_name']))die('Can\'t modify a file that you didn\'t upload</body></html>');
        if(!(
    $imgsize GetImageSize($_FILES['imgfile']['tmp_name'])) or !eregi('^image/jpeg$'$imgsize['mime'])){
            print 
    "<h1>ERROR</h1>Image Extension Unknown.<br>";
            print 
    "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
            print 
    "The file you uploaded had the following extension: <b>.$ext</b></p></body></html>\n";
            
    unlink($_FILES['imgfile']['tmp_name']);
            exit();
        }
              
        if ((
    $imgsize[0] > 250) || ($imgsize[1] > 200))
        {
            
    $tmpimg tempnam("/tmp""MKUP");
            
    system("djpeg {$_FILES['imgfile']['tmp_name']} >$tmpimg");
            
    system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >{$_FILES['imgfile']['tmp_name']}");
            
    unlink($tmpimg);
        }

        
    $final_filename str_replace(" ""_"$_FILES['imgfile']['name']);
        
    $newfile $uploaddir "/" $final_filename;
        
        if (!
    copy($_FILES['imgfile']['tmp_name'],"$newfile")){
            print 
    "Error Uploading File.";
            exit();
        }else{
            print 
    "Upload Successful<br><br>";
        }
        
    unlink($_FILES['imgfile']['tmp_name']);  
        print(
    "<img src=\"$final_filename\">");
    }else{


    echo 
    '
    <body bgcolor="#FFFFFF">

        <h2>Upload and Resize an Image</H2>

        <form action="'
    .$SCRIPT_NAME.'" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="50000">

        <p>Upload Image: <input type="file" name="imgfile"><br>
        <font size="1">Click browse to upload a local file</font><br>
        <br>
        <input type="submit" value="Upload Image" name="mypost">
        </form>'
    ;

    }

    echo 
    '</body>
    </html>'
    ;


    ?>


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
  •