无法在密码识别程序中上传图像,出现错误的路径错误



我正在进行一个项目,试图使用jquery ajax+codeigniter上传图像。

不知道为什么我会遇到以下问题:

错误:上载路径似乎无效

下面的代码中有我遗漏的东西吗?

upload_product_image((是处理上传任务的方法

public function upload_product_image()
{
if(isset($_FILES["file"]["name"]))
{
$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|svg';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file'))
{
echo 'Error: '. $this->upload->display_errors();
echo '<hr>';
echo $config['upload_path'];
}
else
{
$arr_image = array('upload_data' => $this->upload->data());
print_r($arr_image);
}
}
}

以下代码是HTML代码

<div class="card mb-2">
<a class="card-link" data-toggle="collapse" href="#collapseone">
<div class="card-header">
Upload Product Image(One) <span class="float-right text-danger">Size(192X138)</span>
</div>
</a>
<div id="collapseone" class="collapse show" data-parent="#accordion">
<div class="card-body">
<div class="form-group row" >
<div class="col-sm-3 text-center" style="position:relative;">
<img id="product_img_one_img_preview" src="upload/default/coming_soon.svg" class="w-100 img-fluid img-thumbnail" />
<a href="#" class="btn btn-primary mt-2" onclick="$('#product_img_one_box').trigger('click'); return false;"><i class="fa fa-upload"></i> Upload file</a>
<div id="product_img_one_loader"></div>
</div>
<div class="col-sm-9 my-auto">
<input type="text" class="form-control" value="upload/default/coming_soon.svg" name="product_img_one" id="product_img_one" class="form-control" />
<input type="file" name="" class="form-control-file border hidden" id="product_img_one_box">
</div>
</div>
</div>
</div>

下面是我使用的jQuery代码

<script>
$(document).ready(function() {
$("#product_img_one_loader").hide();
$('#product_img_one_box').on('change', function(){
var fd = new FormData();
var files = $(this)[0].files[0];
fd.append('file', files);
console.log(fd);
$.ajax({
url: 'https://mywebsite.com/shop/admin/upload_product_image',
type: "post",
data: fd,
contentType: false,
processData: false,
beforeSend: function() {
$("#product_img_one_loader").html('<div class="lds-facebook" style="position:absolute;top: 35%;left: 40%;"><div></div><div></div><div></div></div>');
$("#product_img_one_loader").show();
},
success: function(response) {
$("#product_img_one_loader").html('');
// $("#product_img_one_loader").hide();
$("#product_img_one_loader").html(response);
// alert(response);
// if (response != 0) {
//
// } else {
//   alert('file not uploaded');
// }
},
complete: function(data) {
// $("#product_img_one_loader").html('');
// $("#product_img_one_loader").hide();
}
});
});
});

上传路径必须定义为绝对服务器路径,而不是相对路径。

尝试$config['upload_path'] = FCPATH.'upload/';而不是$config['upload_path'] = 'upload/';

这个小的更改将强制上传到upload目录中,该目录与前控制器(CI的主index.php文件(位于同一目录中。

您当前的代码正在查找与控制器(application/controllers/upload/(位于同一位置的upload目录,这不是您想要的。

此外,请检查目录及其内容是否由PHP进程拥有和可写(755是一个很好的起点(。。。一个简单的-ls -l将为您提供有关的所有信息

最新更新