1

I have stored QR Code image as data in my database and I use this example link to download it:

http://example.com/module/index.php?QRCodeFile=1&fileID=1

Now, I am trying to download this picture inside a folder to use it to create a pdf later.

My problem is when I am using this code to download the picture in a folder:

$url = 'http://example.com/module/index.php?QRCodeFile=1&fileID=1';
$img = 'assets/temp/qr.png';
file_put_contents($img, file_get_contents($url));

The file downloads but when every I try to open I get this message "We can't open this file."

Fahad
  • 113
  • 2
  • 15
  • It may need to be in a vector format, i.e. pdf, svg, eps; – slevy1 Jul 16 '17 at 19:12
  • 1
    Who gives that error message? That certainly is not a message any base component gives, like the http server or the php engine. Instead this sounds like some framework or your own code... So... I suggest you give us more information about your actual setup... – arkascha Jul 16 '17 at 19:16
  • Check the return value of `file_put_contents` to see how many bytes (or any bytes at all) have been written to that file. Then check that the downloaded data is actually an image and not something else like HTML code. And where do you get the "We can't open this file" message (and is it the correct error message you get)? – Progman Jul 16 '17 at 19:17
  • @slevy1 the image will download as png when i put the link and the picture will be fine. – Fahad Jul 16 '17 at 19:25
  • @arkascha the Photos program on windows show the error message when i try to open the picture after download – Fahad Jul 16 '17 at 19:26
  • @Progman how can i check the value of file_put_contents? – Fahad Jul 16 '17 at 19:27
  • @Fahad Alt: is allow_url_fopen in your PHP config set to true? see https://stackoverflow.com/questions/724391/saving-image-from-php-url . Also, are you using Windows 7 or 8 b/c it doesn't appear that earlier versions of Windows have the ability; see https://www.techwalla.com/articles/how-to-open-png-files – slevy1 Jul 16 '17 at 19:29
  • @slevy1 allow_url_fopen is true .. and I am using windows 10 – Fahad Jul 16 '17 at 19:33
  • 1
    @FahadAlt You save the return value in a variable with `$result = file_get_......;`, then you write `var_dump($result);` to see the result of it. – Progman Jul 16 '17 at 19:39
  • 1
    Maybe you can try to convert png image to base64 before to save the image to your folder – aurny2420289 Jul 16 '17 at 19:42
  • Apparently a lot of people have had this same issue which MS addresses; see https://answers.microsoft.com/en-us/windows/forum/windows_10-files/windows-10-will-not-open-png-files-or-let-me-save/ff1c2ff8-c470-4600-8779-a56a2dc382ff – slevy1 Jul 16 '17 at 22:27
  • @Progman I tried to figure out what the result of the page and it was an html page of the login page .. however, when I post the url in the browser it downloads the image – Fahad Jul 17 '17 at 06:10

1 Answers1

0

This predicament is due to an issue that could have been avoided had MS continued to include Microsoft Office Picture Manager in Windows 10. I have it on a Windows XP box and it easily opens a QR code image as a ".png" file. However, MS for whatever reason decided to remove this program from Windows 10; see here So, the good news is that you may install it; see here. Also, on the XP box, I can right-click on the image file icon and if I select Paint, that program will open the file. This may possibly work in Windows 10 -- you'd have to give it a try and see for yourself b/c apparently MS made changes to that program, too. Lastly, MS has a tool that may interest you; see here.

slevy1
  • 3,797
  • 2
  • 27
  • 33