a53-416
a53-416

Reputation: 3935

Get the path of the downloads folder in PHP?

I'm building a script that will save an image from the web to the user's computer. This is what I've learned, so far:

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

Is this the right way to do this? If so, how would I get the path to, say, the downloads folder, in the user's machine?

Upvotes: 0

Views: 9696

Answers (2)

Evan Mulawski
Evan Mulawski

Reputation: 55334

You can't. The Downloads folder is a browser-specific location that only the user has control of. The file will download to the folder that is specified by the user.

Use readfile along with header to force a Save As... dialog to appear.

<?php
header('Content-disposition: attachment; filename=image.jpg');
header('Content-type: image/jpeg');
readfile('/server/path/to/image.jpg');
?>

Upvotes: 4

Pekka
Pekka

Reputation: 449415

If so, how would I get the path to, say, the downloads folder, in the user's machine?

You can't store contents on the user's computer this way, only on your local server.

You need to serve the file as a download, which the user can then "Save as..." in their browser.

Upvotes: 1

Related Questions