我需要什么:
当用户上传文件时,该文件的所有内容都应写入另一个文件。
这是我的代码
$.validator.addMethod("custom_phone", function(value, element) {
return this.optional(element) || value.match(/^[0-9,\+-]+$/);
}, "Please enter a valid phone number.");
$("#query").validate(
{
rules: {
mobile:
{
required: true
},
email:
{
required: true,
email: true
},
name:
{
required: true
},
},
errorPlacement: function (error, element) {
if (element.parent().hasClass("input-append")){
error.insertAfter(element.parent());
}else{
error.insertAfter(element);
}
},
success: function(element) {
element.closest('.control-group').removeClass('error').addClass('success');
},
submitHandler: function(form) {
// $("#feedback").remove();
//$('#feedback_ajax').removeClass('dis-non');
var input =$("#query").serialize();
alert(input);
var myJsonString = JSON.stringify(input);
alert(myJsonString);
//console.log(myJsonString);
$.ajax({
url: "{{ DomainDetect() }}/ajax?for=formdata",
type: "POST",
crossDomain: true,
cache: false,
data: myJsonString,
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
},
error: function (responseData, textStatus, errorThrown) {
return false;
}
}).done(function(msg ) {
console.log(msg);
data = $.parseJSON(responseData);
console.log(data);
$('.8u').empty();
$('.row flush').append(msg);
});
}
});
<form id= "query" name ="query" class="regform" method="post" enctype="multipart/form-data">
<div class="row orgrw flush pdfrm">
<div class="3u"><label>Name</label></div>
<div class="6u">
<input class="text txtbx" type="text" name="name">
</div>
<div class="3u"><p class="alert alert_name">Required</p></div>
</div>
<div class="row orgrw flush pdfrm">
<div class="3u"><label>Email</label></div>
<div class="6u">
<input class="text txtbx" type="text" name="email">
</div>
<div class="3u"><p class="alert alert_name">Required</p></div>
</div>
<div class="row orgrw flush pdfrm">
<div class="3u"><label>Mobile No.</label></div>
<div class="6u">
<input class="text txtbx" type="text" name="mobile">
</div>
<div class="3u"><p class="alert alert_name">Required</p></div>
</div>
<div class="row orgrw flush pdfrm">
<div class="3u"><label>Upload resume</label></div>
<div class="6u">
<input class="" type="file" name="file">
</div>
<div class="3u not-mobile"> </div>
</div>
<div class="row orgrw flush pdfrm">
<div class="3u"><label style="line-height:1em">Any comments<br><span class="ft4">(optional)</span></label></div>
<div class="6u">
<textarea name="s_comment" rows="2" cols="10" style="min-height:6em; overflow:auto; padding:0.5em; font-size:16px; background-color:#f4f4f4; border-radius:0"></textarea>
</div>
<div class="3u not-mobile"> </div>
</div>
<div class="row orgrw flush pdfrm">
<div class="3u not-mobile"> </div>
<div class="6u aligncenter">
<input type="submit" class="attendnow cursr button" value="Submit">
</div>
<div class="3u not-mobile"> </div>
</div>
</form>
php代码:
not working:echo $_FILES["file"];
if(isset($_POST['file']))
{
$upload=$_POST['file'];
$file="uploaddocument.txt";
if(file_exists($file))
{
$current = file_get_contents($upload);
}
$sucess=file_put_contents($file, $current);
}
else
{
echo "fie not uploaded";
}
输出:
Array(
["name"] => Deepak
[email] => dpk.pandit1992@gmail.com
[mobile] => 919871077792
[s_comment] => "fesgdtfd"
)
Error.log
Undefined index: file
我面临的问题是我无法将上传文件数据的内容写入另一个文件中。
答案 0 :(得分:1)
查看此tutorial on file uploads with AJAX。请注意,只有较新的浏览器支持此功能,请在caniuse.com上查看details which browsers work。
旁注:如上所述:获得一个像PHPStorm或类似的体面的IDE,它将帮助您大大改善您的代码。