-2

I have a Link of image in a web but want to download as image jpg format. using file_get_contents, file_put_contents method in php.

$url = "https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A";
$content = @file_get_contents($url);

$img = 'assets/iamges/f.jpg';
$id = file_put_contents($img, $content);

This code not working.

Nilesh patel
  • 1,216
  • 2
  • 14
  • 38
  • Can we see the code you already have? – brombeer Jun 18 '20 at 11:25
  • 1
    Fine, then start writing code that does that. – CBroe Jun 18 '20 at 11:34
  • https://stackoverflow.com/questions/6476212/save-image-from-url-with-curl-php – Tim Anthony Jun 18 '20 at 11:48
  • Does this answer your question? [Save image from url with curl PHP](https://stackoverflow.com/questions/6476212/save-image-from-url-with-curl-php) – Tim Anthony Jun 18 '20 at 11:48
  • 3
    "This code not working" Why are you hiding errors with the `@` operator? Do you think it will help you understand what's happening? – Olivier Jun 27 '20 at 13:31
  • 1
    Have you checked your php.ini file? Downloading external resources might be disabled. – Joeri Jun 27 '20 at 23:30
  • 3
    The code itself is working, if the target directory is working, if the script has access to the Internet and if like @Joeri wrote you allowed downloading in your php.ini file. Without the error message you get noone can give you the right answer. – wizardofOz Jun 28 '20 at 08:48

7 Answers7

1

Though it is hard to answer without proper error message in PHP. But I will try to guide you towards some solution. Probably it will work.

  1. At first, make sure that you have proper permission in your PHP file and that directory where you are saving the JPEG image file. Just try something like chmod -R 0777 ./ in your current directory if you use Linux or Mac OS. For windows users, just right click on your file/folder and go to properties and choose the security tab and edit your desired permission (possibly read and write in this case).

  2. Also please make sure that where you have your PHP file, you have exactly created the directories there. I mean assets/rkgia_image/. You have created assets directory in the same folder as of your PHP file. Then inside that, you have created rkgia_image. Also, make sure they have proper permission to write content there. Just try something like chmod -R 0777 ./ in your current directory. If you want your program to create the directory automatically, then your code will probably look like the following.

    <?php
     $url = "https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A";
     $content = @file_get_contents($url);
     $path = 'assets/rkgia_image/';
     if (!file_exists($path)) {
       mkdir($path,0777, true);
     }
     $filename='f.jpg';
     $img=$path.$filename;
     $id = file_put_contents($img, $content);
    ?>
    
  3. Another reason for the error could be your server has php.ini that disables file_get_contents behavior. Please open php.ini and change allow_url_fopen=1 to enable the default behavior of file_get_contents.

  4. If none of the above works, please run the following program and let us know about the error.

    <?php
     ini_set('display_errors', 1);
     ini_set('display_startup_errors', 1);
     error_reporting(E_ALL);
     $url = "https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A";
     $content = file_get_contents($url);
     $path = 'assets/rkgia_image/';
     if (!file_exists($path)) {
        mkdir($path,0777, true);
     }
     $filename='f.jpg';
     $img=$path.$filename;
     $id = file_put_contents($img, $content);
    ?>
    
moyeen52
  • 425
  • 3
  • 13
0
<?php

$url = "https://images.pexels.com/photos/2584041/pexels-photo-2584041.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260";
$content = @file_get_contents($url);

$img = 'f.jpg';
$id = file_put_contents($img, $content);

this works ensure the url where you try to download do not had protection on include or the place where you store the received file had write rights (see cmoding .. i sugest you to set cmod to 774 checking the youtube you can get more better than this https://youtu.be/xdim-xpGdRA?t=53 )

0

You can try cURL in case allow_url_fopen is false

$ch = curl_init('https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A');
$fp = fopen('assets/rkgia_image/f.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_exec($ch);
curl_close($ch);
fclose($fp);
Bheru Lal Lohar
  • 880
  • 9
  • 17
0

Your code is working perfectly at my end except one problem that I found is if assets/rkgia_image does not exists then its giving blank. You can try with below code.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$url = "https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A";
$content = file_get_contents($url);

$img = 'assets/rkgia_image/f.jpg';

$id = file_put_contents($img, $content);

echo $id;die;
Aabir Hussain
  • 1,161
  • 1
  • 11
  • 29
0

You code seems to be working, but the problem can be multiple.

First of all, I recommend that you do not use the @ operator, cause you'll not see any error in you code. It's very important for you and us to know the trigger errors to see potential issue.

Then, if you still don't see an error, try activating it by adding the following from the start of your file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

An other way to enable errors is to enable there options directly on your php.ini file.

Now, as your code looks functional, this is some possible mistake that may be causing your troubles :

  • Unable to establish connection in HTTP due to a problem of root certificates on your server
  • Maybe the directory 'assets' or 'rkgia_image' does not exists, or both
  • Unable to write to directory due to permission problem
  • [or other]
Camille
  • 121
  • 4
0

There's nothing wrong with your code except the path, just correct your path and the code works fine.And do not make use of @.

Note: The use of @ is very bad programming practice as it does not make error disappear, it just hides them, and it makes debugging a lot worse since we can’t see what’s actually wrong with our code.

Kunal Raut
  • 2,495
  • 2
  • 9
  • 25
  • @Nilesh Patel i ran your code and it worked well the only thing wrong in your code is the path just provide it the correct path and you are ready to go!!! – Kunal Raut Jul 04 '20 at 04:13
0

First, please make sure you have allow_url_fopen set to true in your php.ini file.

If you're on a Mac, it may be a file ownership problem. Try changing the ownership of the folder to yourself.

$url = 'https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A';
$img = 'assets/rkgia_image/f.jpg';
$id = file_put_contents($img, file_get_contents($url));

if you don't have allow_url_fopen set to true, you can also use cURL

$ch = curl_init('https://v360.in/V360Images.aspx?cid=Excellent&d=F99-26A');
$fp = fopen('assets/rkgia_image/f.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
onlit
  • 728
  • 5
  • 19