我正在尝试通过ajax上传图片,以便在单击保存按钮之前在其特定块中查看上传的图像。搜索网络我找到了以下代码,我试了一下。它工作正常,但是当我上传图像时没有上传。
HTML
<form id="overflow_imgupload" method="post" enctype="multipart/form-data" action="">
<input type="hidden" name="position" value="" class="position"/>
<input type="file" name="imgupload" id="imgupload" value="Upload Image" class="span12"/>
<a id="btn_imgupload" onclick="return ajaxFileUpload();">Upload</a>
<a id="closeimg" >X</a>
</form>
“&GT;
function ajaxFileUpload(){
$("#loading")
.ajaxStart(function(){
$(this).show();
})
.ajaxComplete(function(){
$(this).hide();
});
$.ajaxFileUpload({
url:'<?php echo site_url('doajaxfileupload') ?>',
secureuri:false,
fileElementId:'imgupload',
dataType: 'html',
success: function (data, status){
console.log(data);
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert(data.msg);
}
}
},
error: function (data, status, e){
alert(e);
}
})
return false;
}
<?php
class Doajaxfileupload extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
//$this->load->library('upload');
}
function index(){
$error = "";
$msg = "";
$fileElementName = 'imgupload';
if(!empty($_FILES[$fileElementName]['error']))
{
switch($_FILES[$fileElementName]['error'])
{
case '1':
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'No file was uploaded.';
break;
case '6':
$error = 'Missing a temporary folder';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'No error code avaiable';
}
}elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none')
{
$error = 'No file was uploaded..';
}else
{ //$msg .= $_FILES[$fileElementName]['name'];
$config['allowed_types'] = 'jpg|png|jpeg|bmp|gif';
$config['upload_path'] = './uploaded_files/';
$config['max_width'] = '720';
$config['max_height'] = '240';
$config['min_width'] = '325';
$config['min_height'] = '240';
$this->load->library('upload',$config);
if ($this->upload->do_upload($_FILES[$fileElementName]['name'])) {
$msg .= "Upload success!";
} else {
$msg .= "UPoloadfailes";
}
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
}
}
?>
每次警报UPoloadfailes.why ??
欢迎任何帮助/建议。谢谢。
答案 0 :(得分:0)
我不知道为什么,但是当我使用move_uploaded_file时,它可以工作。
在doajaxfileupload.php else部分
$extension = end(explode('.', $_FILES[$fileElementName]['name']));
$uploaddir = './uploaded_files/temp/';
$file = $uploaddir .'default_'.rand(1,999).'_'.time().'.'.$extension;
if (move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $file)) {
$msg .= $this->input->post('position').','.$file;
} else {
$msg .= "error";
}
有人可以建议我为什么CI上传不起作用以及为什么这个PHP move_uploaded_file有效?