File_put_contents在远程映像上不起作用


file_put_contents not working on remote image

我用下面的代码给自己发送一张图片

$img = file_get_contents("http://www.cpc.ncep.noaa.gov/products/predictions/threats/hazards_d3_7_contours.png");
file_put_contents("hazards_d3_7_contours.png",$img);
$message = $img;
$headers = "From: me";
$headers = "Content-type: image/gif";
$subject = '3-7 Weather Hazard Forecast';
$to = 'me@gmail.com';
mail($to, $subject, $message, $headers);

然而,我得到的图像是坏的知道为什么吗?

该图像的文件扩展名为.png,但您将其发送为image/gif。也许和这个有关?

您正在发送带有mime类型image/gif的电子邮件,即使从远程服务器检索到的图像是image/png。然后用from值覆盖标题。您正在寻找类似这样的内容:

$headers   = [];
$headers[] = 'From: me';
$headers[] = 'Content-type: image/png';
$subject = '3-7 Weather Hazard Forecast';
$to      = 'me@gmail.com';
mail($to, $subject, $message, implode(PHP_EOL, $headers));

这对我有用:

public function store()
{
    $file = file_get_contents($_FILES['myfile']['tmp_name']);
    file_put_contents("img/" . $_FILES['myfile']['name'], $file);
    return $_FILES['myfile'];
}