Hi,

I try to make a simple page to upload an image in Database. This is a test page in my website at this moment.

The name of the table to store the images is image and the columns are: id, userid, name, image

My problem is when I click the submit button, the page show me this error:

Fatal error: Call to undefined function finfo_open() in /home/rwxypejp/public_html/diary/imgupload/addimage.php on line 53

Can anyone help me to solve this error please?

This is the code I use for this page:

<?php 
    require("../headerloggedin.php"); 
?>
        <div id="content">
            <h3>Please Choose a File and click Submit</h3>

            <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <input type="hidden" name="idofuser" value="<?php echo $userid; ?>" />
                <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
                <input name="userfile" type="file" />
                <br />
                <input type="submit" value="Submit" />
            </form>

            <?php
                // check if a file was submitted
                if(!isset($_FILES['userfile']))
                {
                    echo '<p>Please select a file</p>';
                }
                else
                {
                    try 
                    {
                        $msg= upload();  //this will upload your image
                        echo $msg;  //Message showing success or failure.
                    }
                    catch(Exception $e) 
                    {
                        echo $e->getMessage();
                        echo 'Sorry, could not upload file';
                    }
                }

                // the upload function

                function upload() 
                {
                    //require "../dbconnection/connection.php";
                    $maxsize = 10000000; //set to approx 10 MB

                    //check associated error code
                    if($_FILES['userfile']['error']==UPLOAD_ERR_OK) 
                    {
                        //check whether file is uploaded with HTTP POST
                        if(is_uploaded_file($_FILES['userfile']['tmp_name'])) 
                        {    
                            //checks size of uploaded image on server side
                            if( $_FILES['userfile']['size'] < $maxsize) 
                            {  
                                //checks whether uploaded file is of image type
                                //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
                                $finfo = finfo_open(FILEINFO_MIME_TYPE);
                                if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) 
                                {    
                                    // prepare the image for insertion
                                    $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

                                    //get the userid
                                    $userid = $_POST["idofuser"];

                                    // our sql query
                                    $sql = "INSERT INTO image(image, name, userid) VALUES ('{$imgData}','{$_FILES['userfile']['name']}','{$userid}');";

                                    // insert the image
                                    mysql_query($sql) or die("Error in Query: " . mysql_error());
                                    $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>';
                                }
                                else
                                $msg="<p>Uploaded file is not an image.</p>";
                            }
                            else 
                            {
                                // if the file is not less than the maximum allowed, print an error
                                $msg='<div>File exceeds the Maximum File limit</div>
                                <div>Maximum File limit is '.$maxsize.' bytes</div>
                                <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div><hr />';
                            }
                        }
                        else
                            $msg="File not uploaded successfully.";

                    }
                    else 
                    {
                        $msg= file_upload_error_message($_FILES['userfile']['error']);
                    }
                    return $msg;
                }
                // Function to return error message based on error code

                function file_upload_error_message($error_code) 
                {
                    switch ($error_code) 
                    {
                        case UPLOAD_ERR_INI_SIZE:
                            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';

                        case UPLOAD_ERR_FORM_SIZE:
                            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';

                        case UPLOAD_ERR_PARTIAL:
                            return 'The uploaded file was only partially uploaded';

                        case UPLOAD_ERR_NO_FILE:
                            return 'No file was uploaded';

                        case UPLOAD_ERR_NO_TMP_DIR:
                            return 'Missing a temporary folder';

                        case UPLOAD_ERR_CANT_WRITE:
                            return 'Failed to write file to disk';

                        case UPLOAD_ERR_EXTENSION:
                            return 'File upload stopped by extension';

                        default:
                            return 'Unknown upload error';
                    }
                }
            ?>

            <br />   
            <a id="buttonlink" href="../indexloggedin.php">Click here</a> to go in homepage.
        </div>
<?php require("../footerloggedin.php"); ?>

Thanks for your help!!

Fatal error: Call to undefined function finfo_open() in /home/rwxypejp/public_html/diary/imgupload/addimage.php on line 53

finfo_open() was introduced in PHP 5.3.0, do you have the right version?

Hi pritaeas,

From the server side, the php version is 5.3.21

Is the fileinfo extension enabled in the php.ini file?

I try to find php.ini but I didn't find it? Where can I find this file?

I can't guess what system you are using.

Just use another functions in uploading images.

Just use this.

<input type="file" name="file"/>

if($_FILES['file']['error']!=UPLOAD_ERR_OK)
{

}
else
{
    $uploaddir = "your_directory";  // this is your directory
    $file = $uploaddir . basename($_FILES["file"]["name"]); 
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $file))
    {
        $location = $_FILES["file"]["name"]; //this file will be saved into your database.
    }
}

It's very simple and easy to use.

hi ruin3936,

regard to your code for file upload, the word file and name used in line 10, 11 and 13, I change it to directory name or leave it same?

Apart from this regarding to line 9, I need to set a permission for this file?

Thanks.

marifard

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.