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.j开发者_开发技巧pg';
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?
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');
?>
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.
精彩评论