10 html javascript php jquery
我正在使用照片上传脚本,可以在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>
Run Code Online (Sandbox Code Playgroud)
这是将数据发送到的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;
});
});
Run Code Online (Sandbox Code Playgroud)
这是PHP文件,它重命名文件(图像)并将它们上传到uploads文件夹.
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
现在的问题是:
您可以textarea在表单中看到有一个和文件输入.我想插入textarea数据和上传到数据库的图像的名称.但是使用上面给出的PHP脚本重命名和上传图像.状态由我编写的自定义php脚本发布post-status.php.我想要的是我希望将重命名的文件名发送到post-status.php页面,以便我可以将其插入数据库.但由于这是我用于上传照片的外部脚本,我无法将其与我的脚本结合使用.请帮帮我们.您可以从上面给出的链接下载脚本.
首先,HTML代码中的表单存在一些问题,正如一些人已经提到的那样.其中一个问题是您需要使用multipart-form-dataenctype才能上传文件.如果没有,那么$_FILES数组将为空并且没有上传.表单标记中的
另一个runat="server"属性.这完全没必要,只在ASP.net中使用.
第三个也是最具政治性的一个$_SERVER['PHP_SELF']是易受XSS攻击的人!
那么,对你的问题.如果你检查了你正在使用的类的源代码,你会看到在onComplete成功上传文件后调用该函数.将$metas数组作为其第二个参数.此数组包含索引下的新文件名name.
或者,您可以使用该onSuccess方法在文件上载时插入文件名.