Is there anyway I can type wget command into a php and that will download file directly from another server to my own server?

Is there anyway I can type wget command into a php and that will download file directly from another server to my own server?

<?php
  $foo = system('wget http://www.myserver.com/file.txt ~',$output);
?>

Is there anyway I can type wget command into a php and that will download file directly from another server to my own server?

You can run wget by invoking it via the shell as shown above. This however requires that the configuration on your server allows PHP to execute shell commands.

You can however use the regular PHP functions to achive what you want to do with wget.
These are called the Stream Functions.
http://www.php.net/stream

They range from the simple functions such as:

// retrieve the file into a string
$file = file_get_contents('http://example.com/file.ext');

// retrieve the file into an array containing each line
$linesArray = file('http://example.com/file.ext');

to a bit lower level functions such as fopen() and fsockopen() that allow a bit more flexibility.

The only restriction on the stream functions is the ability to use the HTTP wrapper for PHP Streams. This is defined in php.ini, called allow_url_fopen.

I came from Google and would like to note to those looking for the same solution I was, in terms of text at least, also consider using the cURL functions that can be extended into PHP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.