gpt4 book ai didi

php - 通过PHP以blob格式上传到Mysql的文件有0字节

转载 作者:行者123 更新时间:2023-11-29 17:46:21 24 4
gpt4 key购买 nike

我正在尝试通过 PHP 将上传的文件存储在 MySQL 数据库中。执行查询时,返回true。但是,在检索文件时,上传的文件为0字节。请帮我解决这个问题。我使用以下代码来执行查询。

<?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();

最佳答案

您混淆了 $_POST 和 $_FILE。将您的代码更新为以下内容:

if(isset($_FILE['file'])){
$cv_file = file_get_contents($_FILES['file']);
}else{
echo "file not uploaded";
}

关于php - 通过PHP以blob格式上传到Mysql的文件有0字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49790876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com