用到了ob_start();
<?php
header("Content-type:text/html charset=utf-8");
if(!empty($_POST['post'])){
$url=$_POST['url'];
$fileName=$_POST['fileName'];
$fileName=grabImage($url,$fileName);
echo "<img src="$fileName" width='300px' height='300px'/>";
}
function grabImage($url,$fileName){
if($url=="") return false;
$p=strrpos($url,".");
$exp=substr($url,$p+1);
if($fileName=="") $fileName=mt_rand(100000,999999).".".$ext;
if($exp=="gif" || $exp=="jepg" || $exp=="png" || $exp=="jpg"){
ob_start();
readfile($url);
$content=ob_get_contents();
ob_end_clean();
$f=fopen($fileName,"w");
fwrite($f,$content);
fclose($f);
}else{
return false;
}
return $fileName;
}
?>
<form action="" method="post">
图片地址:<input type="text" name="url" /><br/>
保存地址:<input type="text" name="fileName"/><br/>
<input type="submit" name="post" value="下载"/>
</form>