简体   繁体   English

使用Ajax和PHP将文件上传到DB

[英]File upload to DB using Ajax and PHP

Im looking for help here. 我在这里寻求帮助。 Cant figure it out where is problem here. 不能弄清楚这里哪里有问题。 Im trying to make multiple file upload to database using Ajax and PHP. 我试图使用Ajax和PHP将多个文件上传到数据库。 I am getting this error: 我收到此错误:

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

Can you please check code below: 您能否检查以下代码:

index.php: 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: 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;

and $response i get here is "true". 我到达这里的$ response是“ true”。 So data is inserted but BLOB is 0 bytes. 因此已插入数据,但BLOB为0字节。 No image. 没有图像。

Thanks! 谢谢!

Problem is in your upload.php file. 问题出在您的upload.php文件中。

This line: 这行:

$file = addslashes(file_get_contents($_FILES["files"]["name"][$i])); $ file =加号(file_get_contents($ _ FILES [“ files”] [“ name”] [$ i]));

You need change it like this: 您需要像这样更改它:

$file = addslashes(file_get_contents($_FILES["files"]["tmp_name"][$i])); $ file =加号(file_get_contents($ _ FILES [“ files”] [“ tmp_name”] [$ i]));

Then you upload file, file is saved in temporary directory and using random name, you can get it used $_FILES["files"]["tmp_name"] . 然后上传文件,文件保存在临时目录中,并使用随机名称,您可以使用$_FILES["files"]["tmp_name"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM