天天看點

PHP中file_put_contents函數和fwrite函數

file_put_contents函數有危險

https://www.jb51.net/article/127521.htm

推薦使用fwrite函數替代,下面是示例代碼

$url = "http://yourwebsite.com/path/imgtoread.jpg";

$filetosave = PATH_ON_SERVER . "filetosave.jpg";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);

$fileraw = curl_exec($ch);

curl_close ($ch);

if(file_exists($filetosave)){

unlink($filetosave);

}

$fp = fopen($filetosave,'x');

fwrite($fp, $fileraw);

fclose($fp);