I am working on a small project to download files from a ftp server using a web based query. I have create a HTML form as a front end, which takes from user the ftp server name and the file names, and then interacts with the php script, to connect with ftp server and download the files specified. I have kept both the html and php files on my university webserver. I am able to download files on the machine running the webserver when I run the PHP script directly form the command line from the server. But I am not able to download files on my local computer using a web browser.
| Web-browser running on local machine | <--not able to download file--> | My PHP script running on web-server machine | <--Able to download file-->| FTP Server |
<?php
$targetFile = 'file.txt';
$curl = curl_init();
$fh = fopen(dirname(__FILE__) . '/'.$targetFile,'w+b');
if ($fh == FALSE){
print "File not opened<br>";
exit;
}
echo "<br>configuring curl...<br>";
curl_setopt($curl, CURLOPT_URL, "ftp://full_path_name_of_file");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_exec($curl);
echo curl_error($curl);
curl_close($curl);
fclose($fh);
?>
File is downloaded successfully when I run this php script from the command line from the server machine. But when I invoke the script from the web browser using my personal machine, I get an error "File not opened".
Can you please tell is there any way I can download the file via my web browser.
Thanks!