#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    PHP cURL file upload


    Good evening,

    I'm working with a gene similarity online tool, and i need to simulate the user choices by php. In the online form there are various select box, a textarea(UserData) and a button type file.

    So, i'm captured the post data that sends out the online form, and it is this:

    Code:
    POSTDATA =-----------------------------268172264427696
    Content-Disposition: form-data; name="SMeasure"
    
    3
    -----------------------------268172264427696
    Content-Disposition: form-data; name="SOntology"
    
    2
    -----------------------------268172264427696
    Content-Disposition: form-data; name="SFamily"
    
    2
    -----------------------------268172264427696
    Content-Disposition: form-data; name="IncludeIEA"
    
    1
    -----------------------------268172264427696
    Content-Disposition: form-data; name="SApproach"
    
    2
    -----------------------------268172264427696
    Content-Disposition: form-data; name="Identifier"
    
    1
    -----------------------------268172264427696
    Content-Disposition: form-data; name="SModel"
    
    2
    -----------------------------268172264427696
    Content-Disposition: form-data; name="SCombination"
    
    7
    -----------------------------268172264427696
    Content-Disposition: form-data; name="UserData"
    
    
    -----------------------------268172264427696
    Content-Disposition: form-data; name="InputFile"; filename="test.txt"
    Content-Type: text/plain
    
    Q15942 Q15942
    Q15942 P27918
    Q15942 P01034
    Q15942 P01040
    Q15942 P04053
    Q15942 P00746
    Q15942 P20138
    Q15942 P10620
    Q15942 P15923
    Q15942 Q06481
    P27918 Q15942
    P27918 P27918
    P27918 P01034
    P27918 P01040
    P27918 P04053
    P27918 P00746
    P27918 P20138
    P27918 P10620
    P27918 P15923
    P27918 Q06481
    P01034 Q15942
    P01034 P27918
    P01034 P01034
    P01034 P01040
    P01034 P04053
    P01034 P00746
    P01034 P20138
    P01034 P10620
    P01034 P15923
    P01034 Q06481
    P01040 Q15942
    P01040 P27918
    P01040 P01034
    P01040 P01040
    P01040 P04053
    P01040 P00746
    P01040 P20138
    P01040 P10620
    P01040 P15923
    P01040 Q06481
    P04053 Q15942
    P04053 P27918
    P04053 P01034
    P04053 P01040
    P04053 P04053
    P04053 P00746
    P04053 P20138
    P04053 P10620
    P04053 P15923
    P04053 Q06481
    P00746 Q15942
    P00746 P27918
    P00746 P01034
    P00746 P01040
    P00746 P04053
    P00746 P00746
    P00746 P20138
    P00746 P10620
    P00746 P15923
    P00746 Q06481
    P20138 Q15942
    P20138 P27918
    P20138 P01034
    P20138 P01040
    P20138 P04053
    P20138 P00746
    P20138 P20138
    P20138 P10620
    P20138 P15923
    P20138 Q06481
    P10620 Q15942
    P10620 P27918
    P10620 P01034
    P10620 P01040
    P10620 P04053
    P10620 P00746
    P10620 P20138
    P10620 P10620
    P10620 P15923
    P10620 Q06481
    P15923 Q15942
    P15923 P27918
    P15923 P01034
    P15923 P01040
    P15923 P04053
    P15923 P00746
    P15923 P20138
    P15923 P10620
    P15923 P15923
    P15923 Q06481
    Q06481 Q15942
    Q06481 P27918
    Q06481 P01034
    Q06481 P01040
    Q06481 P04053
    Q06481 P00746
    Q06481 P20138
    Q06481 P10620
    Q06481 P15923
    Q06481 Q06481
    -----------------------------268172264427696
    Content-Disposition: form-data; name="Submit"
    
    Submit
    -----------------------------268172264427696--
    My php code is simply this:

    Code:
    <?php 
    
    $url = 'http://web.cbio.uct.ac.za/ITGOM/tools/itgomEngine.php';     
    
    $postData = array();
    $postData['SMeasure'] = '3';
    $postData['SOntology'] = '2';
    $postData['SFamily'] = '2';
    $postData['IncludeIEA'] = '1';
    $postData['SApproach'] = '2';
    $postData['Identifier'] = '1';
    $postData['SModel'] = '2';
    $postData['SCombination'] = '7';
    $postData['UserData'] = "";
    $postData['InputFile'] = new CURLFile('C:\test.txt', 'text/plain', 'test.txt');
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_POST, true );;
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    
    $response = curl_exec($ch);
    echo $response;
    
    ?>
    I can set correctly the select options, but i can not set a file (the label next to the button remains empty).
    Any ideas?

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,122
    Rep Power
    539
    It will send files to the $_FILE array, not $_POST.

    Code:
    <?php 
    
    if(empty($_POST)) {
        $url=(empty($_SERVER['HTTPS'])?'http':'https').'://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
    
        $postData = array();
        $postData['SMeasure'] = '3';
        $postData['SOntology'] = '2';
        $postData['SFamily'] = '2';
        $postData['IncludeIEA'] = '1';
        $postData['SApproach'] = '2';
        $postData['Identifier'] = '1';
        $postData['SModel'] = '2';
        $postData['SCombination'] = '7';
        $postData['UserData'] = "";
        $postData['InputFile'] = new CURLFile(__FILE__, 'text/plain', __FILE__);
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POST, true );;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    
        $response = curl_exec($ch);
        echo $response;
    } else {
        echo('<pre>'.print_r($_POST,1).'</pre>');
        echo('<pre>'.print_r($_FILES,1).'</pre>');
    }
    
    ?>

    Comments on this post

    • squalowww87 agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2014
    Posts
    2
    Rep Power
    0
    Thanks for the reply, but I do not understand your code
    I'm working with a online tool, without API, and there are a lot of data to be processed.
    For this reason, i need to automate this process using php.
    Php code I've posted does not receive a file, but it must send.

    It is now more clear the problem? Thanks!
  6. #4
  7. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,276
    Rep Power
    4193
    Originally Posted by squalowww87
    I can set correctly the select options, but i can not set a file (the label next to the button remains empty).
    Any ideas?
    This sounds like you are expecting $response to output a <input type="file"> element with it prefilled with c:\test.txt as the selected file. If this is indeed what you are refering to then what you are attempting to do is not possible. A file input cannot be assigned a default value, the end user must choose the file themselves. This is a security measure to prevent people from pre-filling file inputs and auto-submitting the form, essentially stealing a persons files.

    If that is not what you are referring to then you'll need to explain better what you are trying to accomplish and what you mean by the label is empty.
    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,122
    Rep Power
    539
    Originally Posted by squalowww87
    Thanks for the reply, but I do not understand your code
    I'm working with a online tool, without API, and there are a lot of data to be processed.
    For this reason, i need to automate this process using php.
    Php code I've posted does not receive a file, but it must send.

    It is now more clear the problem? Thanks!
    The code was just an example to simulate a single file which both sends the file and receives it, and actually sends the PHP file which you are using. If $_POST is not empty, it must be receiving a file. I've never used CURLFile until now, and haven't totally tested it, but it sure looks like it is sending and receiving. Did you try it? When I do, I see the following. The next step would be to do something with /tmp/phpuDJsps in your receiving part of the file.
    Code:
    Array
    (
        [SMeasure] => 3
        [SOntology] => 2
        [SFamily] => 2
        [IncludeIEA] => 1
        [SApproach] => 2
        [Identifier] => 1
        [SModel] => 2
        [SCombination] => 7
        [UserData] => 
    )
    
    Array
    (
        [InputFile] => Array
            (
                [name] => CURLFile.php
                [type] => text/plain
                [tmp_name] => /tmp/phpuDJsps
                [error] => 0
                [size] => 940
            )
    
    )

IMN logo majestic logo threadwatch logo seochat tools logo