Results 1 to 3 of 3

Thread: Uploading Image Problem

  1. #1
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Uploading Image Problem

    I am trying to upload jpeg image but it always giving error (You must upload a jpeg, gif, or bmp) even that image is jpg. but if i upload gif or png file than it will working file.

    please see below code for inserting image:

    PHP Code:
    <form action="upload.php" method="post" enctype="multipart/form-data">
                    <
    ul>
            <
    li>
                <
    label for="name">Your Name:</label>
                <
    input type="text" size="40" id="fname" name="fname" />
            </
    li>
                        

                         <
    li>
                <
    label for="email">Mobile:</label>
                <
    input type="text" size="40" id="mobile" name="mobile" />
            </
    li>
    <
    li>
                <
    label>Upload a file:</label>
                

                                        
                        <
    input type="hidden" name="MAX_FILE_SIZE" value="100000" />
                        <
    input name="image" type="file" class="uploader" id="image" value="Upload" />
                      </
    li>
                            
                            <
    label for="message">Description:</label>
                <
    textarea cols="50" rows="5" id="description" name="description"></textarea>
            </
    li>
            </
    ul>
            <
    p>
            
            <
    button type="submit" class="right">Submit</button>
            <
    button type="reset" class="action">Reset</button>
           
        </
    p>
                    </
    form
    and for Upload.php

    PHP Code:
    <?php
    // Start a session for error reporting
    session_start();
    $userloginname$_SESSION['login'];
    // Call our connection file
    require("includes/conn.php");

    // Check to see if the type of file uploaded is a valid image type
    function is_valid_type($file)
    {
        
    // This is an array that holds all the valid image MIME types
        
    $valid_types = array("image/jpg""image/jpeg""image/bmp""image/gif");

        if (
    in_array($file['type'], $valid_types))
            return 
    1;
        return 
    0;
    }

    // Just a short function that prints out the contents of an array in a manner that's easy to read
    // I used this function during debugging but it serves no purpose at run time for this example
    function showContents($array)
    {
        echo 
    "<pre>";
        
    print_r($array);
        echo 
    "</pre>";
    }

    // Set some constants

    // This variable is the path to the image folder where all the images are going to be stored
    // Note that there is a trailing forward slash
    $TARGET_PATH "images/";

    // Get our POSTed variables
    $fname $_POST['fname'];
    $description $_POST['description'];
    $mobile $_POST['mobile'];
    $image $_FILES['image'];




    // Sanitize our inputs
    $name mysql_real_escape_string($name);
    $description mysql_real_escape_string($description);
    $mobile mysql_real_escape_string($mobile);
    $image['name'] = mysql_real_escape_string($image['name']);

    // Build our target path full string.  This is where the file will be moved do
    // i.e.  images/picture.jpg
    $TARGET_PATH .= $image['name'];

    // Make sure all the fields from the form have inputs
    if ( $fname == "" || $description == "" || $mobile=="" )
    {
        
    $_SESSION['error'] = "All fields are required";
        
    header("Location: insert.php");
        exit;
    }

    // Check to make sure that our file is actually an image
    // You check the file type instead of the extension because the extension can easily be faked
    if (!is_valid_type($image))
    {
        
    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
        
    header("Location: insert.php");
        exit;
    }

    // Here we check to see if a file with that name already exists
    // You could get past filename problems by appending a timestamp to the filename and then continuing
    if (file_exists($TARGET_PATH))
    {
        
    $_SESSION['error'] = "A file with that name already exists";
        
    header("Location: insert.php");
        exit;
    }

    // Lets attempt to move the file from its temporary directory to its new home
    if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
    {
        
    // NOTE: This is where a lot of people make mistakes.
        // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
        
    $sql "insert into people (name, description, mobile, filename, username) values ('$fname', '$description', '$mobile', '" $image['name'] . "', '$userloginname')";
        
    $result mysql_query($sql) or die ("Could not insert data into DB: " mysql_error());
        
    header("Location: images.php");
        exit;
    }
    else
    {
        
    // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
        // Make sure you chmod the directory to be writeable
        
    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
        
    header("Location: insert.php");
        exit;
    }
    ?>
    Last edited by salman1karim; 11-13-2013 at 10:39 AM.

  2. #2
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    if i upload the image less than 70KB than its working fine and i checked in php configration file by default max file upload size is 2M. even after than i cant upload more than 70KB and i remove the file size limit in insert.html
    HTML Code:
    <!--<input type="hidden" name="MAX_FILE_SIZE" value="100000" />-->

  3. #3
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    I resolved the issue i just change the value from 100000 to 700000 and now its working fine

Similar Threads

  1. Problem Uploading Lightbox V2.03a
    By ArtfulWhiz in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 10-22-2011, 04:06 PM
  2. Problem in uploading .xml file
    By newphpcoder in forum PHP
    Replies: 0
    Last Post: 10-21-2011, 02:36 AM
  3. image uploading
    By hosdank in forum MySQL and other databases
    Replies: 4
    Last Post: 08-24-2008, 02:34 PM
  4. having problem uploading file
    By himalayanjava in forum ASP
    Replies: 1
    Last Post: 01-19-2008, 04:29 PM
  5. Uploading Image
    By annies44 in forum HTML
    Replies: 1
    Last Post: 01-29-2006, 08:15 AM

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
  •