Thread: ftp upload ware

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    110
    Rep Power
    13

    ftp upload ware


    I have a working php ftp upload modual that works perfectlly for uploading '.txt' files. I have been trying to get this to upload pictures instead, but I can not seem to get it to work properly. What happens is I get the picture icon but with no data inside the picture.

    This is the script that will work properly for txt files, does anybody now how to modifiy this to upload 'gif', 'jpeg', 'png'? Thanks in advance for anybody that could help me out. This modual is open source, please feel free to use.

    This is broken into two pages for clarity.

    //html for that links to the php upload page

    <html>
    <head>
    <title>Administration - upload new files</title>
    </head>
    <body>
    <h1>Upload new news files</h1>
    <form enctype="multipart/form-data" action="upload.php" method=post>
    <input type="hidden" name="MAX_FILE_SIZE" value="10000">
    Upload this file:
    <input name="userfile" type="file">
    <input type="submit" value="Send File">
    </form>
    </body>
    </html>


    //php upload page

    <html>
    <head>
    <title>Uploading...</title>
    </head>
    <body>
    <h1>Uploading file...</h1>
    <?
    // $userfile is where file went on webserver
    // $userfile_name is original file name
    // $userfile_size is size in bytes
    // $userfile_type is mime type e.g. image/gif text/plain

    echo "Variables are:<br>";
    echo "userfile: ".$userfile."<br>userfile_name: ".$userfile_name."<br> userfile_size: ".$userfile_size."<br>userfile_type: ".$userfile_type."<br>End variables<br><br>";


    if ($userfile=="none")
    {
    echo "Problem: no file uploaded";
    exit;
    }

    if ($userfile_size==0)
    {
    echo "Problem: uploaded file is zero length";
    exit;
    }

    if ($userfile_type != "text/plain") //'image/gif' doesnt work correctly
    {
    echo "Problem: file is not plain text";
    exit;
    }

    $upfile = "images/".$userfile_name;

    if ( !copy($userfile, $upfile))
    {
    echo "Problem: Could not move file into directory";
    exit;
    }


    echo "File uploaded successfully<br><br>";
    $fp = fopen($upfile, "r");
    $contents = fread ($fp, filesize ($upfile));
    fclose ($fp);

    $contents = strip_tags($contents);
    $fp = fopen($upfile, "w");
    fwrite($fp, $contents);
    fclose($fp);

    echo "Preview of uploaded file contents:<br><hr>";
    echo $contents;
    echo "<br><hr>";

    ?>
    </body>
    </html>
  2. #2
  3. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    Maybe I can help without confusing the issue further...

    1) The code checks the filetype explicitly, and it has to be "text/plain" or else the script stops. I would do something like this:
    PHP Code:
    $typesOK = array("text/plain""image/gif""image/jpeg""image/png");
    if (! 
    in_array($userfile_type$typesOK))
    {
        echo 
    "Problem: file is not of acceptable type.";
        exit;

    2) Your script reads in the data from the uploaded file and dumps it to the screen. You can't display an image just by dumping the data to a web page like that. The script copies the image to a place where it's accessible from the web, I think; all you have to do is put an image tag with the proper URL to display the image.

    3) Don't use this check for an empty file upload:
    PHP Code:
    if ($userfile=="none")
    {
        echo 
    "Problem: no file uploaded";
        exit;

    Beginning with (I think) version 4.2, when no file is uploaded the variable is no longer set to "none", instead it's just left blank. A better way is to always check the size; if $userfile_size is zero, then no file or an empty file was uploaded.

    hth

    /nick
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    110
    Rep Power
    13
    That worked out perfectlly, thank you so much...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    110
    Rep Power
    13
    I have one other question about it though. Do you have any idea why 'image/gif' will upload, but 'image/jpg' and 'image/png' does not work?

    $typesOK = array("image/jpeg", "image/png", "image/gif");
    if (! in_array($userfile_type, $typesOK))
    {
    echo "Problem: file is not of acceptable type.";
    exit;
    }

    I am getting this error for jpeg's and png's, but works for gif's. Thanks in advance...
  8. #5
  9. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    I don't have an answer there.. what's the output of your debugging bit when you upload those types of files? When I wrote a script like this I took all the file types I wanted to accept, uploaded them and copied what PHP said the mime type was...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    110
    Rep Power
    13
    For some reason the output file types for jpg and png are screwy:

    Jpeg output file type prints: 'image/pjpeg'
    Png output file type prints: 'image/x-png'

    Mabe these are not exceptable MIME types?
  12. #7
  13. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    Hmm... on closer inspection of my own script I see that I have these listed as acceptable mime types:
    image/jpeg
    image/pjpeg
    image/gif

    well, I am not sure why there are seemingly 2 mime types for jpeg. One of those is probably not correct and shouldn't be there. If I find anything else I'll post it here.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    110
    Rep Power
    13
    sounds good, thanks for all the help.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Boston
    Posts
    1
    Rep Power
    0
    When investigating validating uploading images, I found this list to certify against and it seems to work. I am not familiar with all the types, but you might find this list helpful.

    $cert1 = "image/pjpeg"; //Jpeg type 1
    $cert2 = "image/jpeg"; //Jpeg type 2
    $cert3 = "image/gif"; //Gif type
    $cert4 = "image/ief"; //Ief type
    $cert5 = "image/png"; //Png type
    $cert6 = "image/tiff"; //Tiff type
    $cert7 = "image/bmp"; //Bmp Type
    $cert8 = "image/vnd.wap.wbmp"; //Wbmp type
    $cert9 = "image/x-cmu-raster"; //Ras type
    $cert10 = "image/x-x-portable-anymap"; //Pnm type
    $cert11 = "image/x-portable-bitmap"; //Pbm type
    $cert12 = "image/x-portable-graymap"; //Pgm type
    $cert13 = "image/x-portable-pixmap"; //Ppm type
    $cert14 = "image/x-rgb"; //Rgb type
    $cert15 = "image/x-xbitmap"; //Xbm type
    $cert16 = "image/x-xpixmap"; //Xpm type
    $cert17 = "image/x-xwindowdump"; //Xwd type

IMN logo majestic logo threadwatch logo seochat tools logo