I read few SO posts regarding file upload and just used them to understand how it works. But unfortunately I am unable to get it working:
code
<html>
<head>
<script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="lib/jquery-validate.min.js"></script>
</head>
<script>
$(
function()
{
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'upload.php',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function(s,e)
{
console.log('Completed'+" "+s+" "+e);
},
error: function(xhr,status,error)
{
console.log('Error '+xhr+" "+status+" "+error);
},
data: formData,
cache: false,
contentType: false,
processData: false
});
});
})
</script>
<body>
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
</body>
This is the error I am getting:
<b>Warning</b>: move_uploaded_file(/new 1.txt): failed to open stream:
Permission denied in <b>C:\Users\gopir\Server\Apache24\htdocs\front-
page\upload.php</b> on line <b>6</b><br /><br />
<b>Warning</b>: move_uploaded_file(): Unable to move 'C:\Users\gopir
\AppData\Local\Temp\php306E.tmp' to '/new 1.txt' in <b>C:\Users\gopir
\Server\Apache24\htdocs\front-page\upload.php</b> on line <b>6</b><br />
There was an error uploading the file, please try again!
I understand that new 1.txt permission is causing the probelm.
My doubts are
How does user know about these permissions when he/she uploads the file? Do they need to change permissions if they need to upload? Or did i understand wrongly?
I don't understand about xhr function here. I googled it. But i don't understand it and the need of it here.
Please clear my doubts. If this question doesn't meet SO quality, let me know. I 'll edit/delete it.
Thanks
Forget to upload php
<?php
$folder = "/";
$path = $folder . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>