473,467 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Downloading a file from http url

1 New Member
Hi,

I'm trying to download a file from a http page that requires user authentication. The file can be downloaded manually by clicking on the link. I would like to do this automatically using a PHP script.

Here's the code I am using:
Expand|Select|Wrap|Line Numbers
  1. $srcFilePath = "http://username: password@www.mydomain.com//filename.txt ";
  2.  
  3. $desFilePath = "filename.txt";
  4.  
  5. if (!copy($srcFilePath, $desFilePath)) {
  6. echo "failed to copy $file...\n";
  7.  
I get the following error with the above code:

Warning: copy(http://username: password@www.mydomain.com//filename.txt) [function.copy]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /…/ADETEST/ADETEST3.PHP on line 27

Thanks in advance

Ade
Nov 22 '07 #1
1 1633
pbmods
5,821 Recognized Expert Expert
Heya, Ade. Welcome to TSDN!

You'll want to try using basic authentication headers with an HTTP stream context to accomplish this.

Put in English (or rather PHP):
Expand|Select|Wrap|Line Numbers
  1. $__url = 'http://www.mysite.tld/path/to/file.txt';
  2. $__auth = 'Authentication: Basic ' . base64_encode('username:password');
  3.  
  4. $__req =
  5.     stream_context_create
  6.     (
  7.         array
  8.         (
  9.             'http'    =>
  10.                 array
  11.                 (
  12.                     'method'    => 'get'
  13.                     'header'    => $__auth
  14.                 )
  15.         )
  16.     );
  17.  
  18. $__data = file_get_contents($__url, null, $__req);
  19.  
Nov 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: S.W. Rasmussen | last post by:
Handling file downloading with Randy Birch very nice code (http://vbnet.mvps.org/code/internet/ftpdofiledownload.htm) raises a second question: I my case, the user should not be allowed to...
4
by: Luke StClair | last post by:
Only marginally belonging in this newsgroup... but oh well. I've just started writing in python, and I want to make the files available on the web. So I did the standard <a...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
4
by: Richard L Rosenheim | last post by:
I know that I can download a file from a web server by using the WebClient.DownloadFile method. But, does anyone know of an example of downloading a file from a web server with the ability to...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
2
by: Tomas Martinez | last post by:
Hi there! I'm trying to download a file in my asp.net web, but when downloading it from a Firefox browser, instead of downloading the example.exe file, it's downloading example.exe.htm. My code...
5
by: rony_16 | last post by:
Hi, I have a problem downloading a file . after i connect to the website and get the stream , i treing to write the file on the HD. public void SaveStreamToFile(string filePath, Stream stream) {...
1
by: DBC User | last post by:
Hello, I would like to copy a very large file (80MB) from the web, which is the best approach for downloading such a large file and also how can I show how much percent completed to user. I...
3
by: Chuck Renner | last post by:
Please help! This MIGHT even be a bug in PHP! I'll provide version numbers and site specific information (browser, OS, and kernel versions) if others cannot reproduce this problem. I'm...
6
by: cyusman | last post by:
Hi, We have just moved our application to a new webfarm server environment which utilizing hardware load balancing, SSL off-loading and HTTP compression off-loading.My application is running on...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.