ajax external url php file

ajax external url php file

Hello jQuery,

They need to upload huge files to ftp, because of the limitation of the plan,

I have partially written and copy code to make this work.

to use an external php fix this problem.

code snippet 1: jQuery/javascript:
  1. <script>
  2. $(document).ready(function(){
  3. $('#IDFormField_fileupload_0').change(function(){
  4.     var file = this.files[0];
  5.     name = file.name;
  6.     size = file.size;
  7.     type = file.type;
  8.     //your validation
  9. });
  10. $('#IDFormField_fileupload_0').change(function(){
  11.     var formData = new FormData($('form')[0]);
  12.         $.ajax({
  13.         url: 'http://www.externalurl.com/5.php', //server script to process data
  14.         crossDomain: true,
  15.         type: 'POST',
  16.         xhr: function() { // custom xhr
  17.             var myXhr = $.ajaxSettings.xhr();
  18.             if(myXhr.upload){ // check if upload property exists
  19.                 myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
  20.             }
  21.             return myXhr;
  22.         },
  23.         //Ajax events
  24.         success: function(data) {
  25.        if (data.successMessage){//
  26.         //set progress to 100%
  27.         $("body").find('progress').attr({value:100,max:100});
  28.        }
  29.        if (data.errorMessage){
  30.         console.log("error uploading file");
  31.        }
  32.       },
  33.        data: formData,
  34.        cache: false,
  35.        contentType: false,
  36.        processData: false
  37.     });
  38. });

  39. });
  40. function progressHandlingFunction(e){
  41.     if(e.lengthComputable){
  42.         $('progress').attr({value:e.loaded,max:e.total});
  43.     }
  44. }

  45. </script>




code snippet number 2 php code:
  1. <?php 

  2. $ftp_server = "xxx";
  3. $ftp_username = "xxx";
  4. $ftp_password = "xxx!";
  5. //setup of connection
  6. $conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
  7. //login
  8. if(@ftp_login($conn_id, $ftp_username, $ftp_password))
  9.   {
  10.   echo "conectd as $ftp_username@$ftp_server\n";
  11. }
  12. else {
  13.   echo "could not connect as $ftp_username\n";
  14. }

  15. if (!empty($_FILES)) {
  16. $file = $_FILES["IDFormField_fileupload_0"]["name"];
  17. $remote_file_path = "/".$file;

  18. ftp_put($conn_id, $remote_file_path, $_FILES["IDFormField_fileupload_0"]["tmp_name"], FTP_ASCII);
  19. }
  20. ftp_close($conn_id);
  21. echo "\n\nconnection closed";

  22. ?>


    I have tested this outside of the his environment works perfectly.
      when tested in his environment getting the following errors:
        Failed to load resource: Origin http://www.website.com is not allowed by Access-Control-Allow-Origin. http://www.externalurl.com/5.php
          XMLHttpRequest cannot load http://www.externalurl.com/5.php. Origin http://www.website.com is not allowed by Access-Control-Allow-Origin