11 Replies - 695 Views - Last Post: 19 July 2012 - 07:41 AM Rate Topic: -----

#1 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

Download Link in php

Posted 18 July 2012 - 07:18 AM

Hey i created a download link in php but it doesnt seem to work. Please help me out with this
<?php
// We'll be outputting a .png
header('Content-type: image/png');

// It will be called image1.png
header('Content-Disposition: attachment; filename="Untitled-2.png"');
?>



Html
<a href="image1.php"><img src="Untitled-2.png" style="border:0;"></a>



When i click the image and the download begins after i open the image it says that is corrupted. Whats wrong here?

This post has been edited by Bluezap: 18 July 2012 - 07:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Download Link in php

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Download Link in php

Posted 18 July 2012 - 07:26 AM

That file will output no contents? Also don't have a closing ?>, it's not needed.
Was This Post Helpful? 0
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

Re: Download Link in php

Posted 18 July 2012 - 07:50 AM

View PostRudiVisser, on 18 July 2012 - 07:26 AM, said:

That file will output no contents? Also don't have a closing ?>, it's not needed.

Yeah it just downloads a .jpg file but the file is corrupt. I tried many different image formats but all the same.
Was This Post Helpful? 0
  • +
  • -

#4 vectorialpx  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 28
  • Joined: 16-July 12

Re: Download Link in php

Posted 18 July 2012 - 08:00 AM

You don't have output content. You only have headers...
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Download Link in php

Posted 18 July 2012 - 08:12 AM

View PostBluezap, on 18 July 2012 - 03:50 PM, said:

Yeah it just downloads a .jpg file but the file is corrupt. I tried many different image formats but all the same.

It doesn't output a .jpg file to download, only send the headers telling that you want to download it.

Use either file_get_contents or readfile.

This post has been edited by RudiVisser: 18 July 2012 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

Re: Download Link in php

Posted 18 July 2012 - 08:46 AM

View PostRudiVisser, on 18 July 2012 - 08:12 AM, said:

View PostBluezap, on 18 July 2012 - 03:50 PM, said:

Yeah it just downloads a .jpg file but the file is corrupt. I tried many different image formats but all the same.

It doesn't output a .jpg file to download, only send the headers telling that you want to download it.

Use either file_get_contents or readfile.

wait so how do i implement file_get_contents into the php code?
Was This Post Helpful? 0
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Download Link in php

Posted 18 July 2012 - 08:55 AM

file_get_contents documentation; we can't do this for you.
Was This Post Helpful? 1
  • +
  • -

#8 vectorialpx  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 28
  • Joined: 16-July 12

Re: Download Link in php

Posted 19 July 2012 - 05:18 AM

readfile is a better option
Was This Post Helpful? 1
  • +
  • -

#9 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3708
  • View blog
  • Posts: 5,950
  • Joined: 08-June 10

Re: Download Link in php

Posted 19 July 2012 - 06:14 AM

View PostBluezap, on 18 July 2012 - 02:18 PM, said:

// It will be called image1.png
header('Content-Disposition: attachment; filename="Untitled-2.png"');


What do you mean: "It will be called image1.png"? You say that, but you then go on to call it Untitled-2.png in the very next line. (Incorrect comments are far worse than no comments at all...)

I'm guessing that the confusion here is that you though the "Content-Disposition" header would actually send the file called Untitled-2.png to the browser using the name of the PHP file: image1?

In actuality, the "Content-Disposition" header is just telling the browser that you will be sending it a file, and that it should name the file Untitled-2.png. You will then have to send the file data manually, as RudiVisser and vectorialpx have already explained.

By the way, I would also recommend readfile() over file_get_contents() in this context. It's just... simpler. (For the PHP interpreter, that is. The syntax difference itself is trivial.)
Was This Post Helpful? 1
  • +
  • -

#10 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

Re: Download Link in php

Posted 19 July 2012 - 06:59 AM

View Postvectorialpx, on 19 July 2012 - 05:18 AM, said:

readfile is a better option

Thanks! readfile() seems to be the best option here!
Everything seems to be working well Kudos!
Was This Post Helpful? 0
  • +
  • -

#11 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Download Link in php

Posted 19 July 2012 - 07:15 AM

Just so you're aware as to why I've been a little passive in this thread; I had already posted fully working code and explanations of what you need to do / how it works fully in your original thread (which contains readfile(), which yes is the better option here).

This post has been edited by RudiVisser: 19 July 2012 - 07:16 AM

Was This Post Helpful? 1
  • +
  • -

#12 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

Re: Download Link in php

Posted 19 July 2012 - 07:41 AM

View PostRudiVisser, on 19 July 2012 - 07:15 AM, said:

Just so you're aware as to why I've been a little passive in this thread; I had already posted fully working code and explanations of what you need to do / how it works fully in your original thread (which contains readfile(), which yes is the better option here).

Yeah no worries i understand. I just wasn't familiar with the code that you provided but that is not your fault in anyway.
Thanks for all the help anyways :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1