Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. uploading file via post

uploading file via post

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phpcomagentic-aidata-structureshelp
9 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Why the below code doesn't upload file to http://xyz.com/uvw/upload_data.php[^] automatically ? I get error = uploaded file is either missing, empty or was not uploaded via the POST method But am sure uploading file is not missing or empty as i have echo it and i think i am uploading via post method ?

    function datapost($URLServer,$postdata)
    {

    $agent = "Mozilla/5.0";			 
    $cURL\_Session = curl\_init();
    curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer);
    curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent);
    curl\_setopt($cURL\_Session, CURLOPT\_POST, 1);
    curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata);
    curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1);
    curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1);
    $result = curl\_exec($cURL\_Session);
     
    return $result;
    

    }

    $postdata = array();
    $postdata\['Id'\] = '12340'; 
    $postdata\['profileName'\] = 'tsunami';
        $postdata\['file'\] = file\_get\_contents('http://www.abc.co.uk/def/ghi.csv');
    
    
    $postdata\['submit'\] = urlencode('submit');
    
    
    $source= datapost("http://xyz.com/uvw/upload\_data.php",$postdata);
    

    Any suggestion devs ??

    N 1 Reply Last reply
    0
    • L Lost User

      Why the below code doesn't upload file to http://xyz.com/uvw/upload_data.php[^] automatically ? I get error = uploaded file is either missing, empty or was not uploaded via the POST method But am sure uploading file is not missing or empty as i have echo it and i think i am uploading via post method ?

      function datapost($URLServer,$postdata)
      {

      $agent = "Mozilla/5.0";			 
      $cURL\_Session = curl\_init();
      curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer);
      curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent);
      curl\_setopt($cURL\_Session, CURLOPT\_POST, 1);
      curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata);
      curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1);
      curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1);
      $result = curl\_exec($cURL\_Session);
       
      return $result;
      

      }

      $postdata = array();
      $postdata\['Id'\] = '12340'; 
      $postdata\['profileName'\] = 'tsunami';
          $postdata\['file'\] = file\_get\_contents('http://www.abc.co.uk/def/ghi.csv');
      
      
      $postdata\['submit'\] = urlencode('submit');
      
      
      $source= datapost("http://xyz.com/uvw/upload\_data.php",$postdata);
      

      Any suggestion devs ??

      N Offline
      N Offline
      Niall Barr
      wrote on last edited by
      #2

      Hi, You need to set CURLOPT_HTTPGET to false explicitly - the PHP documentation doesn't make this clear. curl_setopt($cURL_Session, CURLOPT_HTTPGET, FALSE); Niall

      L 1 Reply Last reply
      0
      • N Niall Barr

        Hi, You need to set CURLOPT_HTTPGET to false explicitly - the PHP documentation doesn't make this clear. curl_setopt($cURL_Session, CURLOPT_HTTPGET, FALSE); Niall

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks, I added the line you said but still getting the same error .. :( Anything else i can add/edit to make it working ? Cheers

        N 1 Reply Last reply
        0
        • L Lost User

          Thanks, I added the line you said but still getting the same error .. :( Anything else i can add/edit to make it working ? Cheers

          N Offline
          N Offline
          Niall Barr
          wrote on last edited by
          #4

          No - that works on my system, as long as the CSV file isn't too big. (OK with 150k, not with 30M) This is the version of your code I tried out.

          <?php
          function datapost($URLServer,$postdata)
          {

          $agent = "Mozilla/5.0";
          $cURL\_Session = curl\_init();
          curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer);
          curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent);
          curl\_setopt($cURL\_Session, CURLOPT\_HTTPGET, FALSE);
          //curl\_setopt($cURL\_Session, CURLOPT\_POST, 1);
          curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata);
          curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1);
          curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1);
          $result = curl\_exec($cURL\_Session);
          
          return $result;
          

          }

          $postdata = array();
          $postdata\['Id'\] = '12340';
          $postdata\['profileName'\] = 'tsunami';
          $postdata\['file'\] = file\_get\_contents('http://localhost/tmp/test.csv');
          
          $postdata\['submit'\] = urlencode('submit');
          
          $source= datapost("http://localhost/tmp/posthere.php",$postdata);
          
          echo $source;
          

          ?>

          My test posthere.php is

          L 1 Reply Last reply
          0
          • N Niall Barr

            No - that works on my system, as long as the CSV file isn't too big. (OK with 150k, not with 30M) This is the version of your code I tried out.

            <?php
            function datapost($URLServer,$postdata)
            {

            $agent = "Mozilla/5.0";
            $cURL\_Session = curl\_init();
            curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer);
            curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent);
            curl\_setopt($cURL\_Session, CURLOPT\_HTTPGET, FALSE);
            //curl\_setopt($cURL\_Session, CURLOPT\_POST, 1);
            curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata);
            curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1);
            curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1);
            $result = curl\_exec($cURL\_Session);
            
            return $result;
            

            }

            $postdata = array();
            $postdata\['Id'\] = '12340';
            $postdata\['profileName'\] = 'tsunami';
            $postdata\['file'\] = file\_get\_contents('http://localhost/tmp/test.csv');
            
            $postdata\['submit'\] = urlencode('submit');
            
            $source= datapost("http://localhost/tmp/posthere.php",$postdata);
            
            echo $source;
            

            ?>

            My test posthere.php is

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Niall Barr wrote:

            (OK with 150k, not with 30M)

            Initially i thought this could be the case as i was uploading around 12mb csv but then i tried with just 4kb but still getting the same error. I don't really understand what could be the problem - the uploading works fine if i try using normal html form submit but as we don't want to manually browse the file - we decided to go this route and it's not working.. What a pain. On the other hand why do you say it will not work for size in MB ? Is there any other auto upload way for such big files ?

            N 1 Reply Last reply
            0
            • L Lost User

              Niall Barr wrote:

              (OK with 150k, not with 30M)

              Initially i thought this could be the case as i was uploading around 12mb csv but then i tried with just 4kb but still getting the same error. I don't really understand what could be the problem - the uploading works fine if i try using normal html form submit but as we don't want to manually browse the file - we decided to go this route and it's not working.. What a pain. On the other hand why do you say it will not work for size in MB ? Is there any other auto upload way for such big files ?

              N Offline
              N Offline
              Niall Barr
              wrote on last edited by
              #6

              I think the only issue was the amount of memory available to PHP on my dev box. I tried with a file that was too big for it. Are you able to get the other post data transferred if you comment out the file_get_contents line? Niall

              L 1 Reply Last reply
              0
              • N Niall Barr

                I think the only issue was the amount of memory available to PHP on my dev box. I tried with a file that was too big for it. Are you able to get the other post data transferred if you comment out the file_get_contents line? Niall

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Niall Barr wrote:

                Are you able to get the other post data transferred if you comment out the file_get_contents line?

                I can't test that as it's some external company site where we have to upload our data so that they can do what they are suppose to do with the data. Well i use below code as initial request - so they receive the post data and they send us back id which we need to send along with the file -

                function datapost($URLServer,$postdata)
                {

                $agent = "Mozilla/5.0";

                $cURL_Session = curl_init();

                curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);

                curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);

                curl_setopt($cURL_Session, CURLOPT_POST, 1);

                curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);

                curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);

                $result = curl_exec($cURL_Session);

                return $result;

                }

                $postdata = array();
                $postdata\['profileName'\] = 'tsunami';
                $postdata\['tName'\] = 'abc';
                $postdata\['langCode'\] = 'en\_GB';
                ... etc
                

                $source= datapost("http://xyz.com/uvw/upload\_meta.php",$postdata);

                </pre>

                It using the same process - so am sure it's only file auto upload bit that's not working for us. As i said it works using normal html form submit.

                N 1 Reply Last reply
                0
                • L Lost User

                  Niall Barr wrote:

                  Are you able to get the other post data transferred if you comment out the file_get_contents line?

                  I can't test that as it's some external company site where we have to upload our data so that they can do what they are suppose to do with the data. Well i use below code as initial request - so they receive the post data and they send us back id which we need to send along with the file -

                  function datapost($URLServer,$postdata)
                  {

                  $agent = "Mozilla/5.0";

                  $cURL_Session = curl_init();

                  curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);

                  curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);

                  curl_setopt($cURL_Session, CURLOPT_POST, 1);

                  curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);

                  curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);

                  curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);

                  $result = curl_exec($cURL_Session);

                  return $result;

                  }

                  $postdata = array();
                  $postdata\['profileName'\] = 'tsunami';
                  $postdata\['tName'\] = 'abc';
                  $postdata\['langCode'\] = 'en\_GB';
                  ... etc
                  

                  $source= datapost("http://xyz.com/uvw/upload\_meta.php",$postdata);

                  </pre>

                  It using the same process - so am sure it's only file auto upload bit that's not working for us. As i said it works using normal html form submit.

                  N Offline
                  N Offline
                  Niall Barr
                  wrote on last edited by
                  #8

                  It occured to me that it's likely that the site you're posting to expects an attached file rather than the file content in a post field called file... If that's the case, then this code should work.

                  L 1 Reply Last reply
                  0
                  • N Niall Barr

                    It occured to me that it's likely that the site you're posting to expects an attached file rather than the file content in a post field called file... If that's the case, then this code should work.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    great ... this worked like a charm :) Thanks a ton. :thumbsup:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups