1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

php code to download a .txt file on a url ?

Discussion in 'PHP & Perl' started by mic141414, Dec 13, 2012.

  1. mic141414

    mic141414 Regular Member

    Joined:
    Jul 3, 2008
    Messages:
    344
    Likes Received:
    117
    Location:
    USA
    Home Page:
    Hey,

    Anyone with php knowledge could make that for me ? (or charge me a few $)

    I will have 2 or more sites with a txt file (http://blablable.com/filetext.txt, http://blablabla2.com/filetext.txt, etc )
    Lets say that these sites won't be up for long
    I need to be able to save those txt files at regular interval so i don't lose the info in case site got shut down.
    The txt file will be modified each time a surfer enters the info i need.

    So with each save, the txt can be overwrite .. but need to be sure that in case it becomes 404 or not reachable, the latest saved is kept and not written with a blank file

    Let me know thanks in advance

    PS: i tried in crontab like this wget -O /tmp/filetext.txt http://blablable.com/filetext.txt it wont work .. target size file is 0 and nowhere to found in server where the cron runs. Need a small php app please
     
  2. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    I'm not sure but I dont think u need php for that, you can in bash. Is tha first line of text file always the same?
     
  3. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    525
    Likes Received:
    311
    Gender:
    Male
    Are you able to view those TXT files in the browser in first place ?

    If they are protect with htaccess you will only be able to get it from FTP
     
  4. mic141414

    mic141414 Regular Member

    Joined:
    Jul 3, 2008
    Messages:
    344
    Likes Received:
    117
    Location:
    USA
    Home Page:
    yes able to see them in the browser and not htaccess protected


     
  5. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    525
    Likes Received:
    311
    Gender:
    Male
    Then the file probably needs useragent or referrer to allow u to download or view it and with that wget command it does not define it.

    Try adding these to your wget command:

    Code:
    --referer='http://domain.com'
    And:

    Code:
    --user-agent='Mozilla/4.0 (Windows; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
     
  6. mic141414

    mic141414 Regular Member

    Joined:
    Jul 3, 2008
    Messages:
    344
    Likes Received:
    117
    Location:
    USA
    Home Page:
    okay thanks i am gonna try
    thanks for your help
     
  7. mmkevin

    mmkevin Newbie

    Joined:
    Feb 27, 2012
    Messages:
    14
    Likes Received:
    1
    try to use a download trigger something like this and run it in your browser


    Code:
    trigger.php
    
    [COLOR=#0000BB][FONT=monospace]<?php 
    [/FONT][/COLOR][COLOR=#FF8000][FONT=monospace]// shouldn't this supress output to the browser?: 
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Header[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Content-Type: application/x-octet-stream"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]);  
    [/FONT][/COLOR][COLOR=#FF8000][FONT=monospace]// filename for download: 
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Header[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]""[/FONT][/COLOR][COLOR=#007700][FONT=monospace]);  
    [/FONT][/COLOR][COLOR=#FF8000][FONT=monospace]// dump URL: 
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$loc[/FONT][/COLOR][COLOR=#007700][FONT=monospace]=[/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]""[/FONT][/COLOR][COLOR=#007700][FONT=monospace]; 
    [/FONT][/COLOR][COLOR=#FF8000][FONT=monospace]// open location: 
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$fp[/FONT][/COLOR][COLOR=#007700][FONT=monospace]=[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fopen[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$loc[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"r"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]); 
    [/FONT][/COLOR][COLOR=#FF8000][FONT=monospace]// read contents: 
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$content[/FONT][/COLOR][COLOR=#007700][FONT=monospace]=[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]fread[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$fp[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]100000000[/FONT][/COLOR][COLOR=#007700][FONT=monospace]); 
    [/FONT][/COLOR][COLOR=#FF8000][FONT=monospace]// output: 
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]echo [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$content[/FONT][/COLOR][COLOR=#007700][FONT=monospace]; 
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]?>
    [/FONT][/COLOR]
    
    
    or CURL
    
    
    set_time_limit([COLOR=#800000]0[/COLOR]); [COLOR=gray]// unlimited max execution time[/COLOR]$options = array(  CURLOPT_FILE => [COLOR=#800000]'/path/to/download/the/file/to.zip'[/COLOR],  CURLOPT_TIMEOUT => [COLOR=#800000]28800[/COLOR], [COLOR=gray]// set this to 8 hours so we dont timeout on big files[/COLOR]  CURLOPT_URL => [COLOR=#800000]'big/file.zip'[/COLOR],);$ch = curl_init();curl_setopt_array($ch, $options);curl_exec($ch);