Simulating a form uploading a file to a server

Discussion in 'PHP' started by neterslandreau, Jan 11, 2007.

  1. #1
    I have an HTML form like:
    
    <form method="post" enctype="multipart/form-data">
    <input type="file" name="file_name">
    <input type=submit>
    </form>
    
    Code (markup):
    I want to simulate a user submitting the form and I think the curl library is probably the way to do it. So I wrote this function:
    
    function upload_file($path_to_file)
    {
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
        $postData = array();
        $postData['file_name'] = $path_to_file;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );
        $response = curl_exec( $ch );
    }
    
    Code (markup):
    If I send this to phpinfo(), I see the normal $_POST array but no $_FILE array. If I actually submit the form without curl, I see the $_FILE array as expected.

    After reading the curl man pages, I tested on the command line:
    
    me@mybox:~$ curl -F filename=/home/me/myimg.gif http://127.0.0.1/pi.php
    _REQUEST["filename"] = /home/me/myimg.gif
    _POST["filename"] = /home/me/myimg.gif
    
    Code (markup):
    Still no $_FILE array. Second test (man page says prefacing with @ will force the encoding to be multipart):
    
    me@mybox:~$ curl -F filename=@/home/me/myimg.gif http://127.0.0.1/pi.php
    curl: (26) failed creating formpost data
    
    Code (markup):
    I'm wondering if anybody out there has ever successfully simulated submitting a form via curl to upload a file?

    TIA
     
    neterslandreau, Jan 11, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    _FILES is a superglobal that's only populated when a file is uploaded, curl doesn't have the ability to simulate that I don't think.
     
    krakjoe, Jan 11, 2007 IP
  3. neterslandreau

    neterslandreau Peon

    Messages:
    279
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    curl more or less emulates a browser call and the only difference the server sees is the user-agent. Because I have been beating my head against the wall all day with this, I didn't notice that I had allowed a typo in the file name. Once I fixed that, I was able to make the call from the command line successfully so I went back to my original problem. (A curl error #26 will only occur if the file is unable to be read or non-existent.)

    My original problem is this: I've got a form from which I can upload an image. On a change to the input value (<input type=file>, I want to do an AJAX call so I can populate the form with a preview of the image. If I pass the path/to/file value that is populated in the form to my php script and do a curl call with that pure ($postData['file_name'] = "$img") value, it doesn't create the _FILE structure but the _POST is populated. If I call it as $postData['file_name'] = "@$img", I get an error #26 (the file is nonexistent which is logical because it exists only on the local machine, not the server)

    I think it has to be a matter of 2 separate forms and I should probably refer this question to some AJAX gurus for a possible solution.

    I've learned a couple of things as I've gotten older. First, if I had realized I was gonna live so long, I'd have started saving for retirement a lot sooner. Second, I shouldn't have ignored Javascript for as long as I have. Doh!
     
    neterslandreau, Jan 11, 2007 IP