Jaymin Sejpal Jaymin Sejpal - 5 months ago 8
PHP Question

: Call to undefined function upload()

I'm trying to upload an image and store it in a database, but only the image name is stored. The image itself is not uploading. I have created an upload image function which uploads the image and have a BLOB data type stored in database.


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";
}




}
}

?>

Answer

You're defining your function inside an if statement. Functions should not be defined conditionally. Move the upload() function to the top of the file.