數據庫我使用的是照片上傳腳本,可以在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