“@/path/to/a/file” in PHP, what does it mean?

前端 未结 3 1771
遥遥无期
遥遥无期 2021-01-27 17:02

I have stumbled to following code example:

$image = \'file/path\';
$code = $tmhOAuth->request(\'POST\', \'https://upload.twitter.com/1/statuses/update_with_me         


        
3条回答
  •  清歌不尽
    2021-01-27 17:33

    The {$expression} syntax is one way to embed a variable or expression in a string in PHP, like the #{expression} syntax in Ruby.

    So "@{$image}" is equivalent to '@'.$image.

    The @ is used by the curl module to differenciate a regular POST variable value from a filename to upload. Your library must use the curl module internally or follow the same conventions.

    When setting POST variables, if any value is prefixed with an @, it is considered to be a filename to upload:

    curl_setopt($curl, CURLOPT_POSTFIELDS, array(
        'regular_variable' => 'value',
        'some_file' => '@/path/to/filename', // this is treated as a file to upload
    ));
    

    This is not very well known and can lead to security issues if the programmer is not aware of this. This can be disabled by passing a query-string to CURLOPT_POSTFIELDS (http_build_query()).

    This has no special meaning for PHP itself.

提交回复
热议问题