Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Downloading a remote file with PHP
Old 08-14-2007, 04:38 PM Downloading a remote file with PHP
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
Hi I have made a script to download a remote file using PHP I do not know why it won't work

Can someone help me?

PHP Code:
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('wget -P/dload $_GET["page"];');
?>
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
 
Register now for full access!
Old 08-14-2007, 05:09 PM Re: Downloading a remote file with PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Simply use file_put_contents() if you have http wrappers allowed, or curl:

File_put_contents:
PHP Code:
$return=file_put_contents($url$fileName); 
curl:
PHP Code:
function getPage($url="http://www.example.com/"){
    
$ch curl_init();
    
curl_setopt($ch,CURLOPT_URL$url);
    
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
    
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
    
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    
curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/');
    
curl_setopt($ch,CURLOPT_TIMEOUT,10);
    
curl_setopt($ch,CURLOPT_FOLLOWLOCATIONtrue);
    
$html=curl_exec($ch);
    if(
$html===false){
        
$m=curl_error(($ch));
        
error_log($m);
        return 
false;
    }
    
curl_close($ch);
    return 
$html;
}
$file=getPage($url);
if(
$file!==FALSE){
   
file_put_contents($file$fileName);

__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 08-14-2007 at 05:11 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-14-2007, 06:28 PM Re: Downloading a remote file with PHP
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
I wanted to do it with the wget command
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 08-14-2007, 06:53 PM Re: Downloading a remote file with PHP
blackfalcon's Avatar
Skilled Talker

Latest Blog Post:
New Site: 10DollarBluRay.com
Posts: 62
Name: JuanJose H. Galvez
Trades: 0
Make sure you have permissions to use the exec command. Some enviroments especially shared hosting enviroments do not allow you to use it.
blackfalcon is offline
Reply With Quote
View Public Profile Visit blackfalcon's homepage!
 
Old 08-14-2007, 07:01 PM Re: Downloading a remote file with PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok...

Many things in your snippet then.
First, when you are using single quotes, PHP variables are not accessible.
You must either use double quotes as delimiter, or get out of the string and back after the variable call.
If you use double quotes, take care of placing arrays or objects between {}
PHP Code:
exec('wget -P '.$_SERVER['DOCUMENT_ROOT'].'/dwn/'.$file.' yada yada yada'); 
or
PHP Code:
exec("wget -P {$_SERVER['DOCUMENT_ROOT']}/dwn/$file yada yada yada"); 
Second, separate the option and the parameter
Code:
wget -P/dload $_GET["page"];
will have a syntax error.

Note too that when you use a shell access, PHP will open it in the current directory.
So, specifying "/something" as download directory is assured not to work, except if you have control of the server and made the root level of your tree write enabled for apache.
Much unlikely.

Specify something relative to the current directory, or better, absolute via the $_SERVER['DOCUMENT_ROOT'] variable.

Finally, I don't know if you ever used wget, but it output LOTS of data.
You would better turn them off, by using a -q option

This would make something like:
PHP Code:
exec("wget -q -P {$_SERVER['DOCUMENT_ROOT']}/dwn/ {$_GET["page"]}'); 
Finally, I'd like to invite you to look at the different shell commands available in PHP, if you haven't already done so.
They behave differently, both in the calling process, in the environment variables available to them (Beware, I looked for days error in my code before understanding that the problem was that HOME=/home/xxx was not present, and called certain programs to fail) and in the way of returning infos.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Downloading a remote file with PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.61263 seconds with 12 queries