2

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!";
   }
   ?>
Gibbs
  • 21,904
  • 13
  • 74
  • 138

1 Answers1

1

Give php write access to your directory you would like to move your uploaded file to. More information and different approaches were already made here.

Please check this article for what the proper permissions of an upload directory are to set up your top level upload directory.

Personally I like this approach which shows how to check if a certain upload directory already exists and if not how you create it using php. The advantage is that your httpd or php process is the owner of that created directory automatically (without applying manual changes to the server side directory structure) and you won't make the mistake changing the permissions on your upload directory to 0777 to get a quick and easy solution. This is a good way to organize the tree under your top level upload directory.

Community
  • 1
  • 1
tworabbits
  • 1,203
  • 12
  • 17