0

I would like to ask for help with file uploading over AJAX under Ubuntu, I have separate apache, mysql and php servers installed (not a lamp package bundle) and I want to add a file upload option to the form to be sent without page refreshing.

I have tried different approaches and so far had the best results with code posted by fellow stackoverflow user- Aitazaz Khan (posted as a response to question asked here file is not uploading in ajax php mysql).

To avoid the necessity of checking that post just to see the code I am reposting the code here, will post my problem below the code (code is the one posted originally by Vicky and Aitazaz Khan's script):

Form

<form name="multiform" id="multiform" action="process.php" method="POST" enctype="multipart/form-data">
           name : <input type="text" name="name" id="name"/>
           </br>
           message : <input type="text" name="message" id="message" />
           </br>
           Image : <input type="file" name="file" id="file" />
</form>
           <input  type="button" id="multi-post" value="Run Code"></input>
           <div id="multi-msg"></div>

Script

<script type="text/javascript">
$(document).ready(function(){
$("#multiform").submit(function(e)
{
    var formObj = $(this);
    var formURL = formObj.attr("action");

if(window.FormData !== undefined)  
    {
        var formData = new FormData(this);
        $.ajax({
            url: formURL,
            type: 'POST',
            data:  formData,
            mimeType:"multipart/form-data",
            contentType: false,
            cache: false,
            processData:false,
            success: function(data, textStatus, jqXHR)
            {
                    $("#multi-msg").html('<pre><code>'+data+'</code></pre>');
            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                $("#multi-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
            }           
       });
        e.preventDefault();
        e.unbind();
   }
});
$("#multi-post").click(function()
    {
    //sending form from here
    $("#multiform").submit();
});

});

</script>

And PHP

<?php
  mysql_connect("localhost","root","");
  mysql_select_db("ajaxdatabase");

  $name=$_POST["name"];
  $message=$_POST["message"];
  //storing file in filename variable
    $fileName = $_FILES['file']['name'];
    //destination dir
    $to="image/".$fileName;

    move_uploaded_file($_FILES['file']['tmp_name'],$to);

  $query=mysql_query("INSERT INTO common(name,message,destination) values('$name','$message','$to') ");

  if($query){
    echo "Your comment has been sent";
  }
  else{
    echo "Error in sending your comment";
  }

?>

The problem is: after uploading a file I get the "Your comment has been sent" message and in the database I can see that all fields (including file's location) have been saved but physically the file is not saved.

My question is: do this has something to do with files/directories permissions? From what Vicky posted seems the code should be working correctly.

Posting on stackoverflow as in my opinion it has more to do with programming than with the Ubuntu system itself.

I would appreciate any help.

Best regards.

Community
  • 1
  • 1
  • Check the php error logs on your linux box; probably it's due to the folder access level for the web user in you linux box. *Plus: it's recommended not to use `mysql_query` and use `mysqli_query` instead* – Javad Sep 12 '14 at 18:01
  • Also, check your php.ini settings for post_max_size and upload_max_filesize. Even if your file is bigger than upload_max_filesize, no errors or warnings show. – Devin H. Sep 12 '14 at 18:21
  • A little error checking with `is_uploaded_file()` {http://php.net/manual/en/function.is-uploaded-file.php} might get you pointed in the right place. – DevlshOne Sep 12 '14 at 18:22
  • Thank you for the hints but as I supposed and as Javad mentioned it had to do with folder permissions. In Ubuntu the folder you are writing to needs rwx permissions for others or 770 if you change the folder's owner to www-data:www-data. If you write the files in /var/www/html/[yourwebpage]/image solution with www-data as the folder owner seems to be more secure as the files won't be accessible by end user. In case of giving read write execute permissions to "others" the file tree can be browsed by the user with internet browser and each of the files can be individually downloaded. –  Sep 14 '14 at 15:30

0 Answers0