通过Ajax上传多个图像。我正在使用fd,.append()来发送图像,但是当我在另一个页面上进行恢复时,它会抛出错误:move_uploaded_file(upload_documents / 2017-10-04-04:41:39-aa - Copy - Copy.html):无法打开流:
中的参数无效所以我怎么能解决这个错误请帮助..
HTML代码: -
<input type="file" class="form-control" name="txtBusinessImageName[]" multiple id="txtDocuName" >
<label>Select Parent</label>
<select class="form-control" id="txtParentDocFolder">
<option value="0">--none--</option>
</select>
<button type="button" class="btn btn-success pull-right btnUploadDocClick" >Upload</button>
Ajax代码: -
<script>
$(".btnUploadDocClick").on("click",function(e){
var total_img=$("#txtDocuName").get(0).files.length;
if(total_img==0){
alert("Pleas select an image");
}
else{
alert(total_img);
for(var i=0; i<=total_img-1; i++){
var f_name=$("#txtDocuName").get(0).files.item(i).name;
var f_size=$("#txtDocuName").get(0).files.item(i).size;
console.log(f_name);
console.log(f_size);
}
var fd = new FormData();
var file_data = $("#txtDocuName")[0].files;
var selfolder=$("#txtParentDocFolder option:selected").val();
fd.append("file0", file_data.length+1);
for(var i = 0;i<file_data.length;i++){
fd.append("file"+(i+1), file_data[i]);
}
fd.append("parent_name", selfolder);
$.ajax({
url: 'upload_files_page.php',
data: fd,
contentType: false,
processData: false,
type: 'POST', async : true,
success: function(data){
//alert("Sucessfully Saved");
//fetch_product_image();
}
});
};
});
</script>
PHP代码: -
<?php
session_start();
if(isset($_SESSION['login'])){
include("root/db_connection.php");
$userID=str_replace("'","",$_SESSION['login']);
$parent_name=str_replace("'","",$_REQUEST['parent_name']);
$total=$_REQUEST['file0'];
for($i=1; $i<$total; $i++)
{
$ctrl_id="file".$i;
$file_name=$_FILES[$ctrl_id]['name'];
$tmp_file_name= $file_name;
$file_ext=pathinfo($tmp_file_name,PATHINFO_EXTENSION);
$a1_file=str_replace(".".$file_ext,"",$tmp_file_name);
$currentDate=date("Y-m-d-h:i:s");
$orignal_file_name=$currentDate."-".$a1_file;
$orignal_file_name=$orignal_file_name.".".$file_ext;
$insQ=$db->query("insert into user_document(file_name,file_tmp_name,folder_id,created_by,created_on) values('$orignal_file_name','$file_name',$parent_name,$userID,now())") or die("");
if($insQ==TRUE){
move_uploaded_file($_FILES[$ctrl_id]['tmp_name'],"upload_documents/".$orignal_file_name);
echo "Sucessfully Inserted !...";
}
else{
echo "0";
}
}
}
else{
echo "0";
}
?>
答案 0 :(得分:0)
使用此代码将图像插入本地目录..它适用于我。用于将图像路径插入到DB和映像到本地目录的简单且具有挑战性的代码。希望这对你有用。
$targetfolder = !empty(@$_FILES['file']['name']) ? $targetfolder . basename( @$_FILES['file']['name']) : false;
$file_name="your-domain/images/".@$_FILES['file']['name'];
if(move_uploaded_file(@$_FILES['file']['tmp_name'], $targetfolder))
{
Code for Insert the value here in DB as path= $file_name
}else
{
echo "<script type='text/javascript'>alert('Problem Uploading File!')</script>";
}