调用未定义的函数 upload()

Call to undefined function upload()

我正在尝试上传图像并将其存储在数据库中,但只存储了图像名称。图片本身没有上传。我创建了一个上传图片功能,它上传图片并在数据库中存储了一个 BLOB 数据类型。

<?php
include_once 'db.php';

if (isset($_FILES['productimage']))
{ 
    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';
    }

    function upload() 
    {
        include "db.php";
        $maxsize = 10000000; //set to approx 10 MB

        //check associated error code
        if($_FILES['productimage']['error']==UPLOAD_ERR_OK)
        {

            //check whether file is uploaded with HTTP POST
            if(is_uploaded_file($_FILES['productimage']['tmp_name']))
            {    

                //checks size of uploaded image on server side
                if( $_FILES['productimage']['size'] < $maxsize)
                {  

                    //checks whether uploaded file is of image type
                    if(strpos(mime_content_type($_FILES['productimage']['tmp_name']),"image")===0) 
                    {
                        $finfo = finfo_open(FILEINFO_MIME_TYPE);
                        if(strpos(finfo_file($finfo, $_FILES['productimage']['tmp_name']),"image")===0) 
                        {    

                            // prepare the image for insertion
                            $imgData =addslashes (file_get_contents($_FILES['productimage']['tmp_name']));
                        }
                        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['productimage']['name'].' is '.$_FILES['userfile']['size'].
                            ' bytes</div><hr />';
                    }
                }
                else
                    $msg="File not uploaded successfully.";

            }
            else {
                $msg= file_upload_error_message($_FILES['productimage']['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';
            }
        }


        $productname=$_POST['productname'];
        $productdescription=$_POST['productdescription'];
        $stock=$_POST['stock'];
        $price=$_POST['price'];
        $sku=$_POST['sku'];
        $status=$_POST['status'];
        $query = "INSERT into `addproduct` (p_name,p_desc,p_stock, p_price, p_sku,p_imagename,p_image,p_status) VALUES ('$productname','$productdescription','$stock', '$price','$sku','{$_FILES['productimage']['name']}','{$imgData}','$status')";
        ECHO $query;
        $result = mysql_query($query);
        if($result)
        {
            echo "Added Image succesfully";
        }
    }
}

?>

您正在 if 语句中定义您的函数。不应有条件地定义函数。将 upload() 函数移动到文件顶部。