File upload using curl. What to do when name of input field

0 replies
Hi,

This is the code I made to show the problem:
PHP Code:
$useragent "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729)";
$timeout 10 ;
$cookie tempnam ("/tmp""CURLCOOKIE");
  
$post = array('_method'=>"put",
                     
'authenticity_token'=>'zcvcxfsdfvxcv',
                     
'profile_image[a]'=>"@Girl-Next-Door-movie-f01.jpg"
                      
);
        
        
$ch curl_init();
        
curl_setopt$chCURLOPT_USERAGENT$useragent);
        
curl_setopt($chCURLOPT_HEADER1);
        
curl_setopt($chCURLOPT_POST1);
         
curl_setopt($chCURLOPT_HTTPHEADER, array('Expect:'));
        
curl_setopt($chCURLOPT_URL"localhost/test.php");
         
curl_setopt($chCURLOPT_COOKIEFILE$cookie);
        
curl_setopt$chCURLOPT_COOKIEJAR$cookie );
        
curl_setopt$chCURLOPT_FOLLOWLOCATIONtrue );
        
curl_setopt$chCURLOPT_ENCODING"" );
         
curl_setopt$chCURLOPT_RETURNTRANSFERtrue );
        
curl_setopt$chCURLOPT_AUTOREFERERtrue );
        
curl_setopt$chCURLOPT_SSL_VERIFYPEERfalse );    # required for https urls
        
curl_setopt$chCURLOPT_CONNECTTIMEOUT$timeout );
         
curl_setopt$chCURLOPT_TIMEOUT$timeout );
        
curl_setopt($chCURLOPT_POSTFIELDS$post);
        
$html curl_exec($ch);
        
curl_close($ch); 
Now this link used above: localhost/test.php has this code:
PHP Code:
    print_r($_POST);
    
print_r($_FILES); 
It simply prints whats in post and files.


So the above code displays this on screen:

Code:
Array
(
    [_method] => put

    [authenticity_token] => zcvcxfsdfvxcv
)
Array
(
    [profile_image] => Array
        (
            [name] => Array
                (
                    [a] => Girl-Next-Door-movie-f01.jpg
                )

            [type] => Array
                (
                    [a] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [a] => /tmp/phppLJPQV
                )

            [error] => Array
                (
                    [a] => 0
                )

            [size] => Array
                (
                    [a] => 55377

                )
        )

)
but we need to modify the code so that it should display this:

Code:
Array
(
    [_method] => put
    [authenticity_token] => zcvcxfsdfvxcv
)
Array
(
    [profile_image[a]] => Array
        (
            [name] => Girl-Next-Door-movie-f01.jpg
              
            [type] => image/jpeg
                
            [tmp_name] =>  /tmp/phppLJPQV

            [error] =>  0
                
            [size] =>  55377
                
        )

)
Meaning, it is taking this(profile_image[a]) as an array when we are defining $post because that's how curl recognizes that we want to upload only a file or an array of files on server by http post.



So, basically the problem is the name of the input field that is defined as an array (profile_image[a]) on the web page that we are trying to mock. If this(profile_image[a]) was this (profile_image_a)(without []) on that webpage, then it would not have been a problem. So, if you understand, this is basically a syntax problem. I do not know how stop curl from reading 'profile_image[a]' as an array here 'profile_image[a]'=>"@Girl-Next-Door-movie-f01.jpg. I need curl to read 'profile_image[a]' as an string and not array. I have to use [], otherwise I will not be able to mock the webpage as the name will change. It will give error.

I hope I explained the problem and also gave you a way to test if you have a solution. Again, if your code starts displaying this:
Code:
Array
(
    [_method] => put
    [authenticity_token] => zcvcxfsdfvxcv

)
Array
(
    [profile_image[a]] => Array

        (
            [name] => Girl-Next-Door-movie-f01.jpg     

            [type] => image/jpeg

            [tmp_name] =>  /tmp/phppLJPQV

            [error] =>  0
  
            [size] =>  55377
        )

)
,then we have a solution.

Thanks for helping in advance.

Regards,
Manoj
#curl #field #file #file upload #input #input field as an array #php #upload

Trending Topics