我正在使用照片上传脚本,可在https://github.com/CreativeDream/php-uploader
找到我想将文件名作为逗号分隔值插入数据库。但由于此上传器使用拖放div来上传文件,因此它不会保留<input type="file" name="files[]" id="filer_input2" multiple="multiple">
中的值。
HTML:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server">
<textarea name="status" class="textarea newstatuscontent" placeholder="What are you thinking?"></textarea>
<div class="media"><input type="file" name="files[]" id="filer_input2" multiple="multiple"></div>
<input type="submit" name="post" value="Post" class="post-btn" id="submit" />
</form>
Jquery和Ajax:
$(function() {
$("#submit").click(function() {
var textcontent = $(".newstatuscontent").val();
var mediafile = $(".mediafile").val();
console.log('media files: ' + mediafile);
var dataString = 'content='+ textcontent;
if(mediafile == ''){
if(textcontent == ''){
$('.cap_status').html("Status cannot be empty. Please write something.").addClass('cap_status_error').fadeIn(500).delay(3000).fadeOut(500);
}
}else{
$.ajax({
type: "POST",
url: "post-status.php",
data: dataString,
cache: true,
success: function(html){
$("#shownewstatus").after(html);
$(".newstatuscontent").val('');
}
});
}
return false;
});
});
PHP图片上传文件:
<?php
include('class.uploader.php');
$uploader = new Uploader();
$data = $uploader->upload($_FILES['files'], array(
'limit' => 10, //Maximum Limit of files. {null, Number}
'maxSize' => 10, //Maximum Size of files {null, Number(in MB's)}
'extensions' => null, //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))}
'required' => false, //Minimum one file is required for upload {Boolean}
'uploadDir' => '../uploads/', //Upload directory {String}
'title' => array('{{random}}{{.extension}}', 32), //New file name {null, String, Array} *please read documentation in README.md
'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)}
'replace' => false, //Replace the file if it already exists {Boolean}
'perms' => null, //Uploaded file permisions {null, Number}
'onCheck' => null, //A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback
'onError' => null, //A callback function name to be called if an error occured (must return an array) | ($errors, $file) | Callback
'onSuccess' => null, //A callback function name to be called if all files were successfully uploaded | ($files, $metas) | Callback
'onUpload' => null, //A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback
'onComplete' => null, //A callback function name to be called when upload is complete | ($file) | Callback
'onRemove' => null //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback
));
if($data['isComplete']){
$files = $data['data'];
echo json_encode($files['metas'][0]['name']);
}
if($data['hasErrors']){
$errors = $data['errors'];
echo json_encode($errors);
}
exit;
?>
在控制台中,我得到的值为UNDEFINED
。请查看脚本并帮助我解决问题。我想在var mediafile = $(".mediafile").val();
中获取文件名,以便我可以用逗号分隔值将其传输到我的上传脚本。还请告诉我在通过ajax将数据传输到php文件之前是否应将值作为逗号分隔值进行内爆,否则我可以在以后的php文件中将其内爆。
答案 0 :(得分:1)
Bellow是一个工作示例,我向您展示了如何获取文件名。
我做的第一件事是听看文件输入是否发生变化。当它这样做时,它将获取与输入相关联的文件并循环遍历它们。我将名称连接到一个由分号分隔的字符串,并将.mediafile
的值设置为连接的文件名。
$('#filer_input2').change(function(){
var files = $(this)[0].files;
var output = "";
for (var i = 0; i < files.length; i++) {
console.log(files[i].name);
output += files[i].name+";";
}
$(".mediafile").val(output);
});