Saving image from PHP URL

后端 未结 12 1728
日久生厌
日久生厌 2020-11-21 17:10

I need to save an image from a PHP URL to my PC. Let\'s say I have a page, http://example.com/image.php, holding a single \"flower\" image, nothing else. How ca

12条回答
  •  迷失自我
    2020-11-21 18:17

    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');
    }
    

提交回复
热议问题