Cragsterboy Posted August 13, 2008 Share Posted August 13, 2008 Hey, ive just started getting into php, so yeh im a bit of a noob at the moment. But i was wondering, is there a script which automatic downloads a file from another server to its own server ??? So like every twenty four hours (becuase the data i want to download updates every 24 hrs) it downloads the file to its server? Ive had a look around but all im finding is scripts to force users to download which i dont want, so yeh if there is a script could you help me out with it? Thanks, ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/ Share on other sites More sharing options...
wildteen88 Posted August 13, 2008 Share Posted August 13, 2008 What is the file you're going to "download"? You could use file_get_contents, and then file_put_contents. Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615860 Share on other sites More sharing options...
Cragsterboy Posted August 13, 2008 Author Share Posted August 13, 2008 What is the file you're going to "download"? You could use file_get_contents, and then file_put_contents. The file i want to download is a txt file, but its in urlencode() so when it downloads i need it to decode it as well, so i will have to find a script to do that as well, i did find a decoder script earlier but lost, but il find ti again in a moment. Do you have any tips or tricks for decoding it? K ive just done a quick script with the two things you suggested, im guessing its wrong <?php echo file_get_contents("www.test.com/test.txt"); echo file_put_contents("test.txt","www.test.com/test.txt"); ?> Am i on the correct lines there? ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615878 Share on other sites More sharing options...
wildteen88 Posted August 13, 2008 Share Posted August 13, 2008 No, your code needs to be <?php $contents = file_get_contents("http://www.test.com/test.txt"); if(!empty($contents)) { file_put_contents("test.txt", $contents); } ?> The file i want to download is a txt file, but its in urlencode() so when it downloads i need it to decode it as well Do you mean it encoded using urlencode()? If so PHP has built function called urldecode() to decode the urlencode'd string. Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615882 Share on other sites More sharing options...
Cragsterboy Posted August 13, 2008 Author Share Posted August 13, 2008 <?php $contents = file_get_contents("http://www.test.com/test.txt"); if(!empty($contents)) $a = explode('&', $QUERY_STRING); foreach($a as $key => $b) { $b = split('=', $b); echo 'Value for parameter '.htmlspecialchars(urldecode($b[0])).' is '.htmlspecialchars(urldecode($b[1]))."<br />\n"; } { file_put_contents("test.txt", $contents); } ?> Ok ive added the code to decode the urlencode(), im guessing again that code above is wrong lol as you can see iam a bit of a noob with php lol SO yeh sorry for the annoyance lol ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615913 Share on other sites More sharing options...
wildteen88 Posted August 13, 2008 Share Posted August 13, 2008 WHat that! What's supposed to be encoded? The file url or the contents of the file? If its the contents of the file then its <?php $contents = file_get_contents("http://www.test.com/test.txt"); if(!empty($contents)) { $contents = urldecode($contents); file_put_contents("test.txt", $contents); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615917 Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 What's that? urldecode() is the opposite of urlencode()...what were you trying to do? Oh, wildteen beat me to it. D: Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615919 Share on other sites More sharing options...
Cragsterboy Posted August 13, 2008 Author Share Posted August 13, 2008 WHat that! What's supposed to be encoded? The file url or the contents of the file? If its the contents of the file then its <?php $contents = file_get_contents("http://www.test.com/test.txt"); if(!empty($contents)) { $contents = urldecode($contents); file_put_contents("test.txt", $contents); } ?> Lol that made me laugh, see told you i was wrong (a large amount wrong) I was reading the link you gave me and it said that the code i put in was the right code and more cleaned up so i guess i was wrong lol. Ok right im guessing now i need to do a cron job? So that it executes this script daily? Am i correct there? for once? lol ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615935 Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615936 Share on other sites More sharing options...
Cragsterboy Posted August 13, 2008 Author Share Posted August 13, 2008 whay go me. Right i just used the script and it worked, except it didnt decode the text, so i change the chmods on it to 777, because i thoguht it was due to it not being able to execute and now my script wont work ??? ive changed the chmods back and still no luck heres my script below <title>Auto download</title> <?php $contents = file_get_contents("http://en8.tribalwars.net/map/tribe.txt"); if(!empty($contents)) { $contents = urldecode($contents); file_put_contents("world8.txt", $contents); } ?> ok now its showing this The server encountered an unexpected condition which prevented it from fulfilling the request. The script had an error or it did not produce any output. If there was an error, you should be able to see it in the error log. What am i doing wrong now? lol ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-615967 Share on other sites More sharing options...
wildteen88 Posted August 14, 2008 Share Posted August 14, 2008 Check your servers error log, the reason for the error will be logged there. Also you'll need to make sure the file world8.txt has write permissions (chmod 755 should do it). I am also amusing you're running php5, as file_put_contents is only available for PHP5. You can check the version of PHP by running phpversion() function. Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-616320 Share on other sites More sharing options...
Cragsterboy Posted August 14, 2008 Author Share Posted August 14, 2008 yeh i got php 5 and ive chmoded the fiel to that So whats gone wrong ??? ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-616864 Share on other sites More sharing options...
rofl90 Posted August 14, 2008 Share Posted August 14, 2008 i'd recommend using a php function called system for any serious downloads ie <?php //auto system('wget https://example.com/auto.zip', @$foo); if(system('unzip -r auto.zip', @$foo)) echo "downloaded" Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-616934 Share on other sites More sharing options...
Cragsterboy Posted August 15, 2008 Author Share Posted August 15, 2008 right by some luck ive got it to work But its not decoding the text from urlencode(), so looks like im going to investigate how to do that, anybody suggest any other codes to do this? ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-617015 Share on other sites More sharing options...
Cragsterboy Posted August 15, 2008 Author Share Posted August 15, 2008 Sorry for the double post but i have fixed the error, seems like the code if(!empty($contents)) was confusing it so i removed that and now it decodes it fine! Thanks for the help! ~Crag Quote Link to comment https://forums.phpfreaks.com/topic/119542-automatic-download/#findComment-617031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.