我使用的是照片上傳腳本,它可以在https://github.com/CreativeDream/php-uploader插入文件名到數據庫
這個發現是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的將數據發送到post-status.php
:
//Get Image Value and Assign it to class 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+";";
}
var media = $(".mediafile").val(output);
});
// STATUS UPDATE
$(function() {
$("#submit").click(function() {
var textcontent = $(".newstatuscontent").val();
if(media == ''){
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: {content:textcontent},
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;
?>
現在的問題:
你可以看到存在的形式textarea
和文件輸入。我想插入上傳到數據庫的圖像的textarea數據和名稱。但是圖像正在被重命名,並且使用上面給出的PHP腳本進行上傳。狀態由我在post-status.php
中編寫的自定義php腳本發佈。我想要的是我想將重命名的文件名發送到post-status.php
頁面,以便我可以將其插入到數據庫中。但是因爲這是一個外部腳本,我用它來渲染照片,所以我無法將它與腳本結合起來。請幫助我們。你可以從上面給出的鏈接下載腳本。
我認爲你的代碼沒有完成,在修復真正的問題之前,你可能會考慮幾個問題來解決,首先你需要添加multipart/form-data,如下面的回答,也是這一行「if(media =='') 「這不是jQuery ...在你的代碼上工作並使其成功上傳文件,然後考慮解決名稱問題。 –
到目前爲止你輸出了什麼?在知道發佈的內容後,使用file和textarea的問題就來了。你嘗試調試它們嗎? – Rahi
你所做的一切都是錯誤的。您應該通過輸入上傳和發送數據來完成1個請求。使用「jquery form」插件,它將您的表單與Ajax包裝進行包裝,無需額外編碼,只需重新開發serverside部分即可。 – num8er