gpt4 book ai didi

javascript - 将文件名插入数据库

转载 作者:太空狗 更新时间:2023-10-29 13:12:17 25 4
gpt4 key购买 nike

我正在使用可以在 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>

这是将数据发送到 post-status.php 的 Jquery:

//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 和文件输入。我想插入上传到数据库的图像的文本区域数据和名称。但是图像正在重命名并使用上面给出的 PHP 脚本上传。状态由我在 post-status.php 中编写的自定义 php 脚本发布。我想要的是将重命名的文件名发送到 post-status.php 页面,以便我可以将其插入数据库。但是由于这是我用来上传照片的外部脚本,所以我无法将它与我的脚本结合起来。请帮帮我。您可以从上面给出的链接下载脚本。

最佳答案

首先,您的 HTML 代码中的表单存在一些问题,正如一些人已经提到的那样。其中一个问题是您需要使用 multipart-form-data enctype 才能上传文件。如果不是,则 $_FILES 数组将为空且未上传任何内容。
表单标记中的另一件事是 runat="server" 属性。这是完全没有必要的,只在 ASP.net 中使用。
第三个也是最关键的一个是 $_SERVER['PHP_SELF']vulnerable for XSS attacks !

那么,对于你的问题。如果您检查了您正在使用的类的源代码,您会看到 onComplete 函数在文件上传成功后被调用。使用 $metas 数组作为它的第二个参数。此数组包含新文件名,位于索引 name 下。
或者,您可以使用 onSuccess 方法,在上传文件时插入文件名。

关于javascript - 将文件名插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com