के साथ यूआरएल से छवि को सहेजें, मुझे सीधे यूआरएल से एक छवि को अपने सर्वर पर सहेजने की ज़रूरत है, मैंने कई तरीकों की कोशिश की है लेकिन सभी लगता है कि ठीक से काम नहीं करता है। file_put_contents($file_location, file_get_contents($image_url));
मुझे कोई फ़ाइल निर्देशिका त्रुटि नहीं मिली है। सरल fopen
और fwrite
दूषित छवि लौटता रहता है। यह एक काम किया, लेकिन यह jpg फ़ाइल के बजाय एचटीएमएल फ़ाइल लौटता रहता है।कर्ल
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$imgurl = 'http://some/url/to/image.jpg';
$imagename= basename($imgurl);
if(file_exists('./image/'.$imagename)){continue;}
$image = getimg($imgurl);
file_put_contents('image/'.$imagename,$image);
कुछ गायब है?
धन्यवाद।
क्या आप वाकई एक छवि फ़ाइल डाउनलोड कर रहे हैं जिसे आप डाउनलोड करने का प्रयास कर रहे हैं, न कि वेब पेज? आपको प्राप्त होने वाली HTML फ़ाइल में क्या शामिल है? – JJJ