Show 3 more comments. Ibrahim Azhar Armar I'm not exactly brilliant at php. Thank's again. Why is not this the best answer? How do you handle errors with this approach? What if a is returned or the connection is interrupted or times out? Show 11 more comments. Normally, this would be fine, but I have this code in a web app, so I cant be sure users will have cURL installed.
However, I did give this a vote up. Geoff is it a distributed web app? Because if you control the hosting, then it doesn't matter about your users cURL is a library on your server. I do not control hosting. It is a distributed web app that anyone could have. Curl might be missing. But almost all shared hosting companies have CURL installed by default. I mean, I haven't seen one that doesn't. It has to be a file handler. Gricey 1, 17 17 silver badges 33 33 bronze badges.
Create a folder called "downloads" in destination server Save [this code] into. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default. You can force images or other kind of files to download directly to the user's hard drive using the PHP readfile function.
Here we're going to create a simple image gallery that allows users to download the image files from the browser with a single mouse click. We will create the PHP file with the following code to download the file forcibly. Next, the header function is used to set the necessary header information before using the readfile function.
The basename function is used to retrieve the filename, and the filesize function is used to read the size of the file in bytes, which will be shown in the opening dialog box to download the file. The flush function is used to clear the output buffer. The readfile function is used with the filename only, here. Output The following output will appear after clicking the download link of the image file. The file size of the rose. You can download the file by selecting the Save File radio button and pressing the OK button.
I am looking to allow site users to easily download files audio that we have on a remote hosting service. We have URLs that point to these files. The files are not held on the same site or server as the main site.
Currently we have links that point to these files, and some text that instructs the site user that they have to right click and select "save file as" in order to get these files downloaded to their local machine. To make this a bit more user friendly, it would be much better if a site user could just simply click a link and have a script that would download the file. Having researched this, there are many online examples and SO questions about downloading files on the same server, or downloading URLs to your server.
This is not what we want to do here. We dont want to copy any of these files from the remote hosting service to the main web server. This is for a site user to click a link and have the remote URL download the file directly to the users local machine, not the server. You could then call it from your script like: download.
You may want to build a "proxy page". Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. An other strange thing is when entering the URL in the web browser I don't get the file. It can I could only download the file when clicking the link on the web site!. Chris' answer works, but this seems to work better to download very large files without running out of memory, since it doesn't download the whole file into a variable before writing to disk:.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
0コメント