php保存網絡圖片到本地

//保存網絡圖片
    function getimg($url)

{    

$state = @file_get_contents($url,0,null,0,1);//獲取網絡資源的字符內容

    if($state){        

    $filename = './caiji/'.time().rand(1000,9999).'.jpg';//文件名稱與路徑

        ob_start();//打開輸出

        readfile($url);//輸出圖片文件

        $img = ob_get_contents();//獲得瀏覽器輸出

        ob_end_clean();//清除輸出並關閉

        $size = strlen($img);//獲得圖片大小

        $fp2 = @fopen($filename, "a");        

        fwrite($fp2, $img);//向當前目錄寫入圖片文件,並從新命名

        fclose($fp2);        

        return 1;

    } else{        

           return 0;

           }

    }
相關文章
相關標籤/搜索