我在这里寻求帮助。无法弄清楚问题出在哪里。我试图使用Ajax和PHP将多个文件上传到数据库。我收到了这个错误:
Warning: file_get_contents(iamge.png): failed to open stream: No such
file or directory in /var/www/html/includes/forms/addProductSteps/BTR/upload.php on line 12
请检查以下代码:
的index.php:
<form id="fourthStepForm" class="" action="" method="post" enctype="multipart/form-data">
<input type="file" multiple id="imagesToUpload" name="files[]" value="">
<div class="col-xs-3 col-md-3"><input type="submit" onclick="fourthStep();return false" name="submit" value="Finalize" class="btn btn-primary btn-block btn-md" tabindex="5"></div>
</form>
function fourthStep(){
var formData = new FormData();
var ins = document.getElementById('imagesToUpload').files.length;
for (var x = 0; x < ins; x++) {
formData.append("files[]",
document.getElementById('imagesToUpload').files[x]);
}
$.ajax({
url: 'includes/forms/addProductSteps/BTR/upload.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: formData,
type: 'post',
success: function (response) {
alert(response);
},
error: function (response) {
alert(response);
}
});
}
upload.php的:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once'../../../connections_multi.php';
$p_id = 1001; //for testing purpose
$no_files = count($_FILES["files"]['name']);
for ($i = 0; $i < $no_files; $i++) {
if ($_FILES["files"]["error"][$i] > 0) {
echo "Error: " . $_FILES["files"]["error"][$i] . "<br>";
} else {
$file = addslashes(file_get_contents($_FILES["files"]["name"][$i]));
if (!($stmt = $db->prepare("INSERT INTO products.battery_lobs ( p_id,img) VALUES (?,?)"))) {
$response = $stmt->error;
}
if (!$stmt->bind_param("sb", $p_id, $file )) {
$response = $stmt->error;
}
if (!$stmt->execute()) {
$response = $stmt->error;
}else{
$response = "true";
}
}
}
echo $response;
和我在这里得到的回应是&#34; true&#34;。因此插入数据但BLOB为0字节。没有形象。
谢谢!
答案 0 :(得分:1)
问题出在upload.php文件中。
这一行:
$ file = addslashes($ _ FILES [&#34; files&#34;] [&#34; name&#34;] [$ i]));
你需要改变它:
$ file = addslashes($ _ FILES [&#34; files&#34;] [&#34; tmp_name&#34;] [$ i]));
然后你上传文件,文件保存在临时目录中并使用随机名称,你可以使用$_FILES["files"]["tmp_name"]
。