我使用的是照片上传脚本,它可以在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