www.webdeveloper.com
Results 1 to 7 of 7

Thread: Only allowing Jpg's to be uploaded !!

  1. #1
    Join Date
    May 2004
    Posts
    288

    Only allowing Jpg's to be uploaded !!

    What is the code to allow jpg to be uploaded through my site.

    Ive used this tutorial in learning how to up load files but it does include this in it.

    http://www.phpeasystep.com/phptu/1.html

  2. #2
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    First, in your form, you should use the "accept" with a comma-separated list of the MIME types that you want to accept for upload.[php]<input type="file" name="upfile" accept="mime1,mime2, etc">. In your case, the MIME type would be: "image/jpeg".

    You shouldn't rely on that only, however. You should check on the received MIME types as you post-process the uploaded file(s). For more on that, see: http://www.webdeveloper.com/forum/showthread.php?t=101466
    Last edited by NightShift58; 02-13-2007 at 06:03 AM.

  3. #3
    Join Date
    May 2004
    Posts
    288
    how do u add that to this code then?

    PHP Code:
    <?php
    //set where you want to store files
    //in this example we keep file in folder upload
    //$HTTP_POST_FILES['ufile']['name']; = upload file name
    //for example upload file name cartoon.gif . $path will be upload/cartoon.gif
    $path"upload/".$HTTP_POST_FILES['ufile']['name'];
    if(
    $ufile !=none)
    {
    if(
    copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
    {
    echo 
    "Successful<BR/>";

    //$HTTP_POST_FILES['ufile']['name'] = file name
    //$HTTP_POST_FILES['ufile']['size'] = file size
    //$HTTP_POST_FILES['ufile']['type'] = type of file
    echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
    echo 
    "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
    echo 
    "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
    echo 
    "<img src=\"$path\" width=\"150\" height=\"150\">";
    }
    else
    {
    echo 
    "Error";
    }
    }
    ?>

  4. #4
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Before you do the copy() part, you need to ask:
    PHP Code:
    if ($HTTP_POST_FILES['ufile']['type'] == "image/jpeg") {
      
    copy(...)
    } else {
      echo 
    "You tried to sneak one in...";


  5. #5
    Join Date
    May 2004
    Posts
    288
    is it better to upload the image to the server or to the database?

    or is there much difference

  6. #6
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Personally, I leave them on the server. I'm more flexible that way. but it's a personal choice.

  7. #7
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    To check the mime type of an image use getimagesize() which checks the file headers. If you are extra paranoid you could even use imagecreatefromjpeg() to see whether the image can be opened. Do this before considering moving the image.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles