How to upload images using PHP

Discussion in 'PHP/MySql' started by johnfrey, Aug 7, 2009.

Remove these ads by signing in
Remove these ads by signing in
Thread Status:
Not open for further replies.
  1. johnfrey New Member

    Member Since:
    Jul 17, 2009
    Message Count:
    3
    Likes Received:
    0
    Uploading a file is a basic requirement of most of the websites. In this post, I will explain in detail about how to upload an image using PHP.

    First of all, we will add HTML code to display the browse button to upload an image:
    PHP:

    <FORM ENCTYPE="multipart/form-data"  ACTION="_URL_" METHOD=POST>
    Upload this file: <INPUT NAME="userfile" TYPE="file">
    <INPUT TYPE="submit" VALUE="Send File"></FORM>
    This code will display a text area with a browse button to upload an image. Then I'll add PHP code for processing of file upload:

    PHP:
    if ($userfile_size >250000){$msg=$msg."Your  uploaded file size is more than 250KB so please reduce the file size and then  upload. Visit the help page to know how to reduce the file size.<BR>";$file_upload="false";}
    Now, we will check that only jpeg or gif files can be uploaded into our server:

    PHP:
    if (!($userfile_type =="image/pjpeg"  OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or  GIF. Other file types are not allowed<BR>";$file_upload="false";}
    Finally, running this script will add the file to the mentioned directory:

    PHP:
    if(move_uploaded_file ($userfile,  $add)){
    // do your coding here to give a thanks message or any other thing.}else{echo "Failed to upload file Contact Site admin to fix the problem";}
     
  2. Adam H Administrator

    Member Since:
    Jan 7, 2008
    Message Count:
    2,807
    Likes Received:
    162
    Occupation:
    Web Developer
    Location:
    Norfolk Uk
  3. Hanratty New Member

    Member Since:
    Sep 3, 2009
    Message Count:
    15
    Likes Received:
    0
    Code (text):
    [COLOR=#000000][COLOR=#0000bb]<?php

    [/COLOR][COLOR=#ff8000]// filename: upload.success.php

    [/COLOR][COLOR=#0000bb]?>[/COLOR]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "path for image or site link">

    <html lang="en">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
             
            <link rel="stylesheet" type="text/css" href="stylesheet.css">
             
            <title>Successful upload</title>
         
        </head>
         
        <body>
         
            <div id="Upload">
                <h1>File upload</h1>
                <p>Congratulations! Your file upload was successful</p>
            </div>
         
        </body>

    </html>[/COLOR]
  4. Lolito New Member

    Member Since:
    Jul 22, 2009
    Message Count:
    31
    Likes Received:
    0
    Nice and Thanks for info. But the most important is you should have folder in your web host for uploaded file or images.
Thread Status:
Not open for further replies.

Share This Page