gpt4 book ai didi

php - 简单的 AJAX 文件上传表单不起作用

转载 作者:行者123 更新时间:2023-12-02 19:49:42 25 4
gpt4 key购买 nike

我有一个有效的 Ajax 表单,现在我尝试向其中添加文件上传,但它不起作用。该文件未上传,也未在 $_FILES 数组中找到。代码非常简单,所以我不知道我可能做错了什么。我在这里给出了 HTML、AJAX 和 PHP 代码,但是由于 HTML 和 PHP 非常简单,我想我一定在 Ajax 功能中遗漏了一些东西,因为我对此没有太多经验。

这是 HTML:

<div id="leave_comment" class="leave_comment">
<form name="comment" id="comment" action="#" method="post" enctype="multipart/form-data">
<input type="hidden" name="rep_id" id="rep_id" value="something"/>
Name: <input type="text" name="c_name" id="c_name" onFocus="this.value=''; this.onfocus=null;"
value="Anonymous"/>
<br/>Comment:
<br/><textarea name="comment_box" id="comment_box" rows="5" cols="46" maxlength="2000" style="width:390px;"
onFocus="this.value=''; this.onfocus=null;">Type your comment here</textarea>

<p>Upload image (optional):
<br/><input type="hidden" name="size" value="2000000"><input type="file" name="photo">
<br/><input type="submit" class="submit" value="Add Comment"/>
</form>
</div>

这是 AJAX:

<script type="text/javascript">
$(function () {
$(".submit").click(function () {
var name = $("#c_name").val();
var comment = $("#comment_box").val();
var rep_id = $("#rep_id<").val();
var dataString = 'name=' + name + '&comment=' + comment + '&rep_id=' + rep_id;
if (name == '' || comment == '' || comment == 'Type your comment here') {
alert('Please Leave a Comment');
}
else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="../../images/loading_indicator.gif" />Loading Comment...');
$.ajax({
type:"POST",
url:"../../commentajax.php",
data:dataString,
cache:false,
success:function (html) {
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
$("#flash").hide();
$("#leave_comment").hide();
}
});
}
return false;
});
});
</script>

还有commentajax.php中的PHP:

if ($_POST) {
$name = mysql_real_escape_string($_POST['name']);
$comment = mysql_real_escape_string($_POST['comment']);
$rep_id = $_POST['rep_id'];
$now = date('Y-m-d h:i:s');
$ip = $_SERVER['REMOTE_ADDR'];

//handle photo upload
$target = "userphotos/";
$rand_string = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 6)), 0, 6);
$target = $target . $rand_string . "_" . basename($_FILES['photo']['name']);
$pic = $rand_string . "_" . $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'], $target);
if ($target == "userphotos/") {
$target = "";
}
if (pathinfo($pic, PATHINFO_EXTENSION) !== 'jpg' && pathinfo($pic, PATHINFO_EXTENSION) !== 'jpeg' && pathinfo($pic, PATHINFO_EXTENSION) !== 'gif' && pathinfo($pic, PATHINFO_EXTENSION) !== 'bmp' && pathinfo($pic, PATHINFO_EXTENSION) !== 'png' && pathinfo($pic, PATHINFO_EXTENSION) !== 'JPG' && pathinfo($pic, PATHINFO_EXTENSION) !== 'JPEG' && pathinfo($pic, PATHINFO_EXTENSION) !== 'GIF' && pathinfo($pic, PATHINFO_EXTENSION) !== 'BMP' && pathinfo($pic, PATHINFO_EXTENSION) !== 'PNG' && $pic !== "") {
$pic = "";
}

$resb = mysql_query("select * from blocked_ips where ip='$ip' ") or die(mysql_error());
$numb = mysql_num_rows($resb);
if ($numb == 0) {
mysql_query("insert into comments (report_id, author, ip, comment, photo) values ('$rep_id', '$name', '$ip', '$comment', '$pic') ") or die(mysql_error());
echo "<h5>Reply by " . $name . " on " . $now . "</h5><div id='comment'>" . nl2br($comment) . "</div>";
}
else {
echo "<p class='error'>The IP you are using is banned.";
}
}

最佳答案

您需要将文件数据作为原始数据(javascript 文件 API)传递,使用 flash 或使用 iframe,由于浏览器的工作方式,您不能只通过 ajax 发布文件输入。

http://www.anyexample.com/programming/php/php_ajax_example__asynchronous_file_upload.xml

关于php - 简单的 AJAX 文件上传表单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9454630/

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