我正在尝试在 Yii2 中添加 REST api 供移动应用程序用来上传图像/音频文件。我试图使用 PUT 方法从 http 表单数据获取图像/文件数据,但由于某种原因 fopen("php://input", "r"); 返回空流。我尝试了此示例中给出的代码http://www.php.net/m...put-method.php。
<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
/* Close the streams */
fclose($fp);
fclose($putdata);
?>
Run Code Online (Sandbox Code Playgroud)
同时,使用 POST 方法也可以。使用以下代码进行 POST
$putdata = fopen($_FILES['photo']['tmp_name'], "r");
$filename …Run Code Online (Sandbox Code Playgroud)