欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

保存到临时文件

$temp = tempnam(sys_get_temp_dir(), 'TMP_');

file_put_contents($temp, file_get_contents("$path/$filename"));


$temp = tmpfile(); fwrite($temp, file_get_contents("$path/$filename"));



$path = 'https://i.stack.imgur.com/koFpQ.png'; $filename = basename($path); Image::make($path)->save(public_path('images/' . $filename));

$info = pathinfo($url); $contents = file_get_contents($url); $file = '/tmp/' . $info['basename']; file_put_contents($file, $contents); $uploaded_file = new UploadedFile($file, $info['basename']);


copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.jpeg');if not, use file_get_contents

$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png"); $fp = fopen("/location/to/save/image.jpg", "w"); fwrite($fp, $content); fclose($fp);





       var_dump($path);
   $tmpfile = tempnam(sys_get_temp_dir(), 'TMP_');
    file_put_contents($tmpfile,file_get_contents($path));

   $uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(tempnam(sys_get_temp_dir(), 'TMP_'),substr(strrchr($path,'/'),1),'application/octet-stream');
   var_dump( $uploadedFile->getMimeType());var_dump("GGGGGGGGG");



$info = pathinfo($url);
$contents = file_get_contents($url);
$file = '/tmp/' . $info['basename'];
file_put_contents($file, $contents);
$uploaded_file = new UploadedFile($file, $info['basename']);


642down voteaccepted

If you have allow_url_fopen set to true:

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));

Else use cURL:

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
  • 1
    Thanks bro, your code help me to solve the problem. But could u pls help me to make the script automated .I mean when a new gif image come to the url (“example.com/image.php”) then our script automatically fetch the new image and store it to my directory? – riad Apr 7 '09 at 8:26
  • 28
    And how do you know, that the new image "came"? – vartec Apr 7 '09 at 8:37
  • 2
    I think riad means using a $_GET variable containing the URL of the image http://example.com/fetch-image.php?url=http://blabla.com/flower.jpg. In the case of this example, you could just call $_GET['url'] in your PHP script, like so: $ch = curl_init($_GET['url']);. – Mathias Bynens Nov 29 '09 at 13:04
  • 4
    +1 for being the only answer I've seen that includes the "b" for binary flag. – Will Morgan Oct 17 '12 at 16:12 
  • 24
    @vartec: coz it was smoking a cigarette and had a big smile on its face :) – Jimbo May 27 '13 at 20:22

copy('http://example.com/image.php', 'local/folder/flower.jpg');

$content = file_get_contents('http://example.com/image.php');
file_put_contents('/my/folder/flower.jpg', $content);

Here you go, the example saves the remote image to image.jpg.

function save_image($inPath,$outPath)
{ //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

save_image('http://www.someimagesite.com/img.jpg','image.jpg');

Vartec's answer with cURL didn't work for me. It did, with a slight improvement due to my specific problem.

e.g.,

When there is a redirect on the server (like when you are trying to save the facebook profile image) you will need following option set:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

The full solution becomes:

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

I wasn't able to get any of the other solutions to work, but I was able to use wget:

$tempDir = '/download/file/here';
$finalDir = '/keep/file/here';
$imageUrl = 'http://www.example.com/image.jpg';

exec("cd $tempDir && wget --quiet $imageUrl");

if (!file_exists("$tempDir/image.jpg")) {
    throw new Exception('Failed while trying to download image');
}

if (rename("$tempDir/image.jpg", "$finalDir/new-image-name.jpg") === false) {
    throw new Exception('Failed while trying to move image file from temp dir to final dir');
}

$img_file='http://www.somedomain.com/someimage.jpg'

$img_file=file_get_contents($img_file);

$file_loc=$_SERVER['DOCUMENT_ROOT'].'/some_dir/test.jpg';

$file_handler=fopen($file_loc,'w');

if(fwrite($file_handler,$img_file)==false){
    echo 'error';
}

fclose($file_handler);

See file()PHP Manual:

$url    = 'http://mixednews.ru/wp-content/uploads/2011/10/0ed9320413f3ba172471860e77b15587.jpg';
$img    = 'miki.png';
$file   = file($url);
$result = file_put_contents($img, $file)


普通分类: