How to get filename in php in put request

假装没事ソ 提交于 2019-11-28 05:32:10

问题


I know we can read put request in file using

file_get_contents("php://input");

But how do i get the filename in the put request?


回答1:


Filename is not sent as a part of the request. If you want to get the filename on the server side, you will have to pass it along by other means, i.e. query parameters.




回答2:


If you're using PUT like in PUT Method Support page, you can get the filename they PUT to as $_SERVER['REQUEST_URI']

For example, if I have the receiving PHP script

<?php
/* PUT data comes in on the stdin stream */
$request_body = @file_get_contents('php://input'); 

error_log("Received PUT,  filename: {$_SERVER['REQUEST_URI']}, " . strlen($request_body) . " bytes");
?>

When I issue the CURL:

curl -X PUT -T "localfile.txt" http://example.com/interesting/target/name.txt

I get the output (in my error log)

Received PUT,  filename: interesting/target/name.txt, 4931 bytes

If you're asking how I could find out the name the file had back on the source file system (localfile.txt) I don't think that gets passed along.



来源:https://stackoverflow.com/questions/14479386/how-to-get-filename-in-php-in-put-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!