WordPress.org

Ready to get started?Download WordPress

Forums

How add external data file (10 posts)

  1. DrMosko
    Member
    Posted 9 months ago #

    hello all,
    Im dev a plugin and need to somehow include a file from external server (*.dat) , the file is updated every month. and i dont want to release an update vesion every month.
    thank u

  2. chrismichaels84
    Member
    Posted 9 months ago #

    As long as allow_url_fopen is enabled in php.ini you can use remote files basically the same way you use local files. The PHP manual talks about it here: http://php.net/manual/en/features.remote-files.php.

    In your case, you would probably want to use the fopen() php function to load the .dat file into a variable and then do whatever you need to with the data. The documentation for that is: http://php.net/manual/en/function.fopen.php.

    If you were more specific about what you need, I could give you some example code with some security measures. What do you want to do with the file?

    Another good overview is http://phpprogramming.wordpress.com/2007/02/17/using-remote-files-in-php/

    But be warned that this can cause some serious security problems, so you should ONLY do this with files and servers you ABSOLUTELY trust. This may be disabled on shared hosting, depending.

    I do not know of a wordpress specific function that will do this. I'd like to know if there is one. Anyone else want to weigh in?

  3. bcworkz
    Member
    Posted 9 months ago #

    If the file is available through HTTP, use the HTTP API.

  4. DrMosko
    Member
    Posted 9 months ago #

    ok thank u
    I change the request and want to download the file auto every month, how do i do that?

  5. chrismichaels84
    Member
    Posted 9 months ago #

    For that, you use WP Chron. The API is pretty simple: http://codex.wordpress.org/Function_Reference/wp_schedule_event

    But you can only go up to daily, not monthly. If that's a problem, then the other option is to create a simple plugin that adds a link under an admin menu to execute the update and download. Then YOU fire it once a month.

  6. bcworkz
    Member
    Posted 9 months ago #

    OR
    Schedule a daily event that checks a timestamp stored in options the last time the file was downloaded. If a month has passed, do it again, otherwise do nothing.

    OR (there's probably even more possibilities)
    Use crontab at the server level to execute a monthly task. This is identical to manually clicking a link once a month, except once it's setup, you do nothing yourself.

  7. DrMosko
    Member
    Posted 9 months ago #

    can u give me simple code on how to download a remote file and overwrite existing one monthly?

  8. DrMosko
    Member
    Posted 9 months ago #

    hello bcworkz,
    "Schedule a daily event that checks a timestamp stored in options the last time the file was downloaded. If a month has passed, do it again, otherwise do nothing."

    $nextUpdate = $options['lastUpdate']+180 ;
    
    if ( $nextUpdate < $userTime ){
    
    		// set last update
    		$options['lastUpdate'] = $nextUpdate;
    }

    usertime is current timstamp
    nextupdate is timstamp
    options -lastupdate is timstamp also that created when the plugin activated.
    something is missing, cause the increment only happening once time, and not every refresh of the page.

  9. bcworkz
    Member
    Posted 9 months ago #

    Hi drmosko,
    I saw your other post about this as well, though I didn't at the time realize it was related to your file update scheme. As your code is out of context, even knowing the purpose, I'm not sure what's going on. In particular where is the option value coming from and how is it being saved? It sounds like the updated value is not getting saved for some reason.

  10. DrMosko
    Member
    Posted 9 months ago #

    I fixed it, thank all u

Reply

You must log in to post.

About this Topic