I am currently trying to retrieve a file from an FTP-Server in order to make it accessible for the user to download. ftp_get() writes it to a path on the local machine, yes, but what I want is that it also shows up in the download history and counts as "normal" download from the internet but I didn't figure out how to do this yet. I also tried to link directly to the file in PHP with header("Location: ftp://username:password#ftp.server.com/myfile.file") but this was resulting in the browser showing the files contents (which I didn't want). Did I miss any header-Parameters ? Or is there a completely different way to do this ?
You won't be able to "redirect" a user to a file so he can download it using FTP. This is a HTTP-thing. Browsers provides FTP features and make it look like HTTP but, in fact, those are different stuff.
If this file is only accessible through FTP and it is on a remote server, the only way I can imagine so you cand 'redirect' this download to the user is:
Download the file from the FTP to your application server through FTP in PHP;
Send it to the user using PHP and appropriate file headers, something like this: https://stackoverflow.com/a/7263943/2802720
Hope it helps.
Related
Unable to correctly use php code to load a video mp4 file stored in home directory.
Hi, I am building a WP site that sells instruction video mp4 files. To protect the files I have placed them in a directory called videos which is in the home directory (outside of the public_html directory) to protect the files from being downloaded for free. I am trying to write php code for loading the video. However, I can't access the video in /home/username/videos.
My code:
add_action('template_redirect', 'video_redirect', 5);
function video_redirect(){
if (is_admin())
return;
if (!is_page(videoplayerpageonmysite))
return;
$filename="/home/username/videos/videofile.mp4";
echo " Your browser does not support the video tag.";
Each time I run the code I get a No video with supported format... error.
I'm only able to get it to load the video file when it is in the public_html folder (it works perfectly then), but not when it is located in /home/username/videos/
Please help!
You can't give the browser a file path on your server's hard disk and expect it to be able to load it. It will resolve it as a relative URL, ask the HTTP server for it, and then get a 404.
You need to give the browser a URL that actually loads the file.
If you want to limit who can access it (e.g. people who have paid), then you could write a PHP program that checks to see if the request is coming from someone who has paid (i.e. Authentication + Authorization), then reads the file and outputs it in the HTTP response.
You have to use a PHP script as the source URL in the video tag and specify some identifier for which video it should load e.g. src="loadvideo.php?id=1" or something. (This is because the source must be a valid URL which is actually accessible on the webserver - the browser, which don't forget runs on the user's machine not the server, cannot navigate to a path on disk. If it could, then moving your files outside the public_html folder would not provide any security!)
And then when the video tag is loaded into the page, it will make a request to that URL, which causes the PHP script to run. The script must associate the provided ID with the correct file on disk, fetch the contents of that file and echo them as the response, along with appropriate headers (e.g. mime type etc). You can probably find examples of this pattern online already.
Of course the PHP script will also need to authenticate the request to make sure the requestor is a signed-in, paid user, otherwise you still aren't protecting anything. Without this last step, anyone could just visit the PHP script's URL and provide an ID until they got a result, and download the videos just as if you'd put them in the public_html folder. As long as you implement security correctly though, only users who already paid for the videos would be able to download them.
Is it possible to send file from a local URL?
I need to upload files in php from your local url, eg I open the web page with:
www ... upload.php?url=c://...file.jpg,
from the url GET,I would get the file on the pc and would upload, without using html or anything else that I have to choose, just with the file url.
Important, it can only be this way, it will not be possible to choose the file during the upload, it will only be a POST or GET with the local url.
I researched and found nothing related, if anyone can help
I think it is impossible for server to get a client file by using the file path.
But maybe you can use JS and FileSystemobject to prepare the file, make it to streaming and post to the server.
And you must know FSO need a high security permission and may be disabled on users' browser.
I have a url that downloads a .csv file to my computer like this
http://oi62.tinypic.com/2nh0zzt.jpg
The url is
api.infortisa.com/api/Tarifa/GetFile?user=xxxxxxxxxxxxxxxx
But what I want is to download that file in a folder of the server, or at least open it's content so I can save it where i want.
(upload that file via FTP every time is a pain)
I can't access the direct url of the file, just what the api gives me, and that is just a direct download.
I tried curl and file_get_contents, but nothing seems to work.
How can I download that file in the server instead of my computer?
Any help would be appreciated.
I have a WordPress site, in which type1 users can upload *.doc files and type2 users should be able to open them by clicking on a link which represents a file.
The file then should be opened in a new window of the browser.
I got the first part easily - there are many guides on uploading a file.
But I couldn't find one that goes through all the steps of getting and opening a file which resides on a server directory.
I assume clicking the link should cause an ajax request which will send the file key to the server.
The server can then get from the MySQL DB the file name and location.
But what do I do then?
How do I get the file from the server directory, send it back to the client and open it for reading in the browser?
Edit:
I want the files to be confidential and not let just anyone read them so giving the users the link for the file on the server isn't the right way.
I still think I need to send the file from the server directory to the client.
How can this be done?
You can add links to uploaded media in WordPress easily. Just remember to link to the actual file:
I want the files to be confidential and not let just anyone read them so giving the users the link for the file on the server isn't the right way.
Yes it is. You can't let people read the document without giving them the document.
If you want to keep it confidential, then require the users to agree not to share the link with anyone else. Or go further and add a layer of authentication + authorisation and also require that users not hand over their credentials.
Simply, redirect the browser to the location of the file. It's depend on user's browser settings that file opens on the browser directly, or user sees open/save dialog.
But, if you want to open the file inside the browser without letting user download it, you should redirect her/him to a custom page. e.g. a page that loads a doc editor.
There's no way to show a Microsoft Word file inside the browser unless they install proper software. e.g. an Applet or ActiveX or something that be able to open MSWord files.
Consider using WP Client File Share plugin.
hy guys,
i really need your help. i've succesfully connected to ftp server via php.
i'm listing all files that are on the server. if i click a file the browser should prompt a download window to download the file.
i've absolutely no idea how to do that. which method am i going to use. ftp_get kind of confuses me. it says i have to declare a local_file as well. i just want a file on the server to download to my harddrive.
how can i do that?
regards matt
The remote file has to first be downloaded to your server before you can send it to the user. It's invisible to the user, but you don't have a choice. PHP won't let the browser talk directly to the FTP server.
Create a separate php script that calls ftp_get for a specific file, stores it temporarily to your server to allow the user to download it.
Something like:
<?php
//assume the page was called like download.php?filename=downloaded.pdf
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
$tempFile = 'temp'.rand();
ftp_get($ftp, $tempFile, $_GET['filename'], FTP_BINARY);
readfile($tempFile);
You may add code to delete the tempFile too.
If you provide a link to a file that can't be read by the browser (such as a php file, audio, video, etc.) it will ask you to download the file.
The other way is to use PHP headers on a page and print out the page, and link to that page. http://www.ryboe.com/tutorials/php-headers-force-download