嗨,我是 PHP 新手:我曾尝试使用随机生成的 nos 上传图像文件。然后我想在视图 php 中返回上传的文件名并在 jquery 函数中设置。这里是代码上传文件。
// controller file this file only when i click upload calling
// controller file
function uploadfileview(){
$targeturl = getcwd()."/uploads/";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$checkexisting = getcwd()."/uploads/".$_FILES['userfile']['name'][0];
$random_number = rand(0,1000000);
//checking the file is existing or not
if(file_exists($checkexisting)) {
if(move_uploaded_file($_FILES['userfile']['tmp_name'][0], $targeturl.$random_number.'-'.$_FILES['userfile']['name'][0])){
echo json_encode(array(
'files' => $random_number.'-'.$_FILES['userfile']['name'][0],
'post' => $_POST,
'fileurl' => getcwd()."/uploads/".$random_number.'-'.$_FILES['userfile']['name'][0]
));
$bb = $random_number.'-'.$_FILES['userfile']['name'][0];
}
}else{
if(move_uploaded_file($_FILES['userfile']['tmp_name'][0], $targeturl.$random_number.'-'.$_FILES['userfile']['name'][0])){
echo json_encode(array(
'files' => $random_number.'-'.$_FILES['userfile']['name'][0],
'post' => $_POST,
'fileurl' => getcwd()."/uploads/".$random_number.'-'.$_FILES['userfile']['name'][0]
));
}
$data['testing'] = $random_number.'-'.$_FILES['userfile']['name'][0];
return $this->$data('reimbursement');
}
// exit;
}
}
这是ajax上传form.function。
// for uploaded the file name
jQuery(function(){
var button = $('#uploader-button'), interval;
new AjaxUpload( button, {
action: baseUrl + "expensereimbursement/uploadfileview",
name: 'userfile[]',
multiple: true,
onSubmit : function(file , ext){
// Allow only images. You should add security check on the server-side.
if (ext && /^(jpg|png|jpeg|pdf)$/.test(ext)){
} else {
// extension is not allowed
$('#bq-reimbursement-form .error-ajax').text('Error: Invalid File Format').css('color','red').show().fadeOut(5000);
// cancel upload
return false;
}
// change button text, when user selects file
button.text('Uploading');
// If you want to allow uploading only 1 file at time,
// you can disable upload button
this.disable();
// Uploding -> Uploading. -> Uploading...
interval = window.setInterval(function(){
var text = button.text();
if (text.length < 13){
button.text(text + '.');
} else {
button.text('Uploading');
}
}, 200);
},
onComplete: function(file, response){
button.text('Upload');
window.clearInterval(interval);
// enable upload button
this.enable();
var obj = $.parseJSON(response);
if(obj.error){
$('#bq-reimbursement-form .error-ajax').text('Error: File Already Exist!').css('color','red').show().fadeOut(5000);
}else {
var url = "https://docs.google.com/viewer?url=" + obj.fileurl;
var html = '<li><input type="text" name="imageName[]" value="'+file +'" class="display-type" ><a class="filenames" target="_blank" href="'+url+'">'+file +'</a><span class="close-button display-type">x</span></li>';
$('#upload-file-bq .files').append(html);
}
}
});
});
var html = '<li><input type="text" name="imageName[]" value="'+file +'" class="display-type" ><a class="filenames" target="_blank" href="'+url+'">'+file +'</a><span class="close-button display-type">x</span></li>';
// file = my uploaded file name to be return from controller.
这里文件只有我想返回上传的文件名,任何人都可以帮助我。这是否可能在 jquery 中获取值。