我需要通过PUT方法接收图像发送。因此,我正在编写脚本来测试它。我想接收并使用相同的脚本发送它。我该如何实现呢?以下变体没有任何回声和字符串关于祝贺http方法发送正常。
<?php
//if they DID upload a file...
var_dump(file_get_contents("php://input"));
if($_FILES['photo']['tmp_name'])
{
echo $_FILES['photo']['error'];
if($_FILES['photo']['error']==0)
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
$message = 'Congratulations!!!!!!!.';
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], 'url.../1.jpg');
echo'Congratulations! Your file was accepted.';
$image = fopen('url.../1.jpg', "rb");
var_dump($image);
$ch = curl_init();
/* Set cURL options. */
curl_setopt($ch, CURLOPT_URL, "http://url.../upload.php");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $image);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($image));
/* Execute the PUT and clean up */
$result = curl_exec($ch);
fclose($image); //recommended to close the fileshandler after action
curl_close($ch);
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
else
{
echo"WORKS";
}
答案 0 :(得分:1)
将图像发送回浏览器的最简单方法是使用网址。
<?PHP $imgFile=""; if($_FILES['photo']['tmp_name']) { ///Your existing code $imgFile="http://" . $_SERVER['HTTP_HOST'] .'/Your image url here'; //Ex:\\ http://yourserver.com/images/1.jpg - //you can take this from your move_upload_file } ?> <img src="<?PHP echo $imgFile; ?>" />
有用的链接How to receive a file via HTTP PUT with PHP
即使是安静的服务,您也可以使用json或xml将图片网址发回。 PUT不是一个好主意,除非你因某种原因需要发回图像数据。你应该重新考虑一下你的逻辑吗?
答案 1 :(得分:0)
错误是:
strlen($image)
strlen如果错误,必须是文件大小,并且在我的网址中我有www
。这不是我的帖子,但它是错误的。
更有经验的程序员帮助了我。
r.php读取流:
$ res = file_get_contents(“php:// input”);
$file = fopen('1.txt', "w");
fputs($file, $res);
fclose($file);
var_dump($res);
s.php获取流和init r.php
$image = fopen('/var/www/testfiles/1.jpg', "rb");
var_dump($image);
$ch = curl_init();
/* Set cURL options. */
curl_setopt($ch, CURLOPT_URL, "http://url withot www/r.php");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $image);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize('/var/www/testfiles/1.jpg'));
/* Execute the PUT and clean up */
$result = curl_exec($ch);
fclose($image); //recommended to close the fileshandler after action
curl_close($ch);
die("OK");