|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-02-13 23:53 UTC] lawrence at dub3solutions dot com
Description: ------------ When building an options array, setting one of the CURLOPT_POSTFIELDS key/values to a CURLFile object will result in the following PHP Warning: PHP Warning: curl_setopt_array(): Invalid filename for key variable_name When this warning is raised, curl_setopt_array returns true, and proceeds normally except it skips any files added. Not sure if this is the proper method, but I was asked to tag @bishop who was following my issue on stackoverflow. https://stackoverflow.com/questions/54680799/php-7-2-curlfile-gives-invalid-filename-warning Test script: --------------- $curl = curl_init(); $curlOpts = array( CURLOPT_POST => 1, CURLOPT_URL => $postUrl, CURLOPT_TIMEOUT => 20, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => 'apikey' ); $postFields = array('var1' => 'value'); $postFields['variable_name'] = curl_file_create($filePath); $curlOpts[CURLOPT_POSTFIELDS] = $postFields; curl_setopt_array($curl, $curlOpts); $curl_response = curl_exec($curl); $info = curl_getinfo($curl); curl_close($curl); Expected result: ---------------- I would expect the file to get added to the curl options. Actual result: -------------- The file is ignored and a warning is added to the http logs PHP Warning: curl_setopt_array(): Invalid filename for key variable_name PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 04:00:01 2025 UTC |
This appears to depend very much on the exact file name path passed as the first argument to curl_file_create. Can you copy & paste an example of path raising this error, as well as provide information about what kind of filesystem this file resides? Additionally, can you try with this sample code: <?php error_reporting(-1); $file = tmpfile(); $path = stream_get_meta_data($file)['uri']; file_put_contents($path, md5(random_bytes(64))); $opts = [ CURLOPT_URL => 'https://postman-echo.com/post', CURLOPT_RETURNTRANSFER => 1, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => [ 'file' => curl_file_create($path), ], ]; $curl = curl_init(); $pass = curl_setopt_array($curl, $opts);The supplied test code returns true for $pass. In fact, it also works if I set $path to the exact file I was using when I was getting the error. See below for the exact code. The only thing I have changed is fourth and fifth level directory names for privacy, but the renamed folders are identical in length and character type to what I was using. <?php error_reporting(-1); //$file = tmpfile(); //$path = stream_get_meta_data($file)['uri']; //file_put_contents($path, md5(random_bytes(64))); $path = '/var/www/sites/Abc1/abc-de.abc1abcd.com/wp-content/uploads/d3dforms_contact/contact_20190214_1936.csv'; $opts = [ CURLOPT_URL => 'https://postman-echo.com/post', CURLOPT_RETURNTRANSFER => 1, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => [ 'file' => curl_file_create($path), ], ]; $curl = curl_init(); $pass = curl_setopt_array($curl, $opts); print $pass; Could it be that when adding in the authorization parameters something gets mixed up? The contents of the file are very light in case you want them: "First Name","Last Name",Email,Company,Phone,Interests,Subject,Message,Picture,"Pardot ID","Date Submitted","Remote IP" Lawrence,Johnson,lj@test.com,,,"Industry Information",,"Test message here More info.",/wp-content/uploads/d3dforms_contact/GUTGfallkvaqeNHrY7h830t4Hwv7UZ/d98b99127be5eeab40225f8de2e15ae222cd04e8.jpg,,"2019-02-14 10:57:59",AA.BB.CC.DD