-1

I am trying to store uploaded file in MySQL database through PHP. When the query is executed, it returns true. But, when retrieving the file, the uploaded file is of 0 byte. Please help me with the issue. I have used the below code for the query execution.

<?php
$name = $email = $subject = $message = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = test_input($_POST['username']);
    $email = test_input($_POST['email']);
    $linkedinurl = test_input($_POST['linkedin']);
    $message = test_input($_POST['message']);
    $jobrole = test_input($_POST['jobrole']);
    $captcha = test_input($_POST['g-recaptcha-response']);
}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
}

if(isset($_POST['file'])) {
     $cv_file = file_get_contents($_FILES['file']);
}

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
}

if (!$captcha) {
    echo 'Please check the captcha form.';
    exit;
}

$secretKey = 'xxx';
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$captcha.'&remoteip='.$ip);
        $responseKeys = json_decode($response, true);
    if (intval($responseKeys['success']) !== 1) {
        echo '<h2>You are spammer !</h2>';
    } else {
        echo '';
    }

$servername = 'xxx';
$username = 'xxx';
$password = 'xxx';
$dbname = 'xxx';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die('Connection failed: '.$conn->connect_error);
}

$sql = "INSERT INTO career_details (name, email, linkedin_url,job_role, message, cv) 
VALUES ('$name', '$email', '$linkedinurl', '$jobrole', '$message','$cv_file')";

if ($conn->query($sql) === true) {
    echo 'New record created successfully';
} else {
    echo 'Error: '.$sql.'<br>'.$conn->error;
}

$conn->close();
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
John
  • 13
  • 5

1 Answers1

-1

You are mixing up $_POST and $_FILE. Update your code to the following:

if(isset($_FILE['file'])){
     $cv_file = file_get_contents($_FILES['file']);
}else{
    echo "file not uploaded";
} 
Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26