数据库我使用的是照片上传脚本,可以在https://github.com/CreativeDream/php-uploader插入文件名与AJAX
找到我想要的文件名作为一个逗号分隔值插入到数据库中。但由于这个上传器使用拖放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文件中解压缩它。
*为逗号分隔值* .... gooosh,为什么呢? – Hackerman
abc.jpg,bcd.jpg,123.jpg像这样......这样我才能提取它以后使用.. –
这些应该存储在另一个表....文件ID,文件名,MIME类型,存储路径,创建日期等 – Hackerman