If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Senior Member
    Join Date
    Apr 2005
    Hatfield, England
    Member #
    Can a php file copy itself to another location then delete itself after it is run?

    The code for this would be something like.

    if directory isn't blah {
    copy to directory
    delete self
    Run other code


  3. #2
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Bandung, Indonesia
    Member #
    It would most likely need to be done via a separate file, AFAIK. Just use a header redirect to another file that does the move.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Cottage Grove, Minnesota
    Member #
    717 times
    That's an interesting paradox.

    Copy itself, jump to it's new location, execute and delete the old one.

    I think it would actually work if you knew for certain that the copied script was
    completely written before it was executed. There might be a server time
    delay if the server happened to be really busy at the time, and didn't get the
    file in place before you finished the original script.

    I think it would be very important to make sure you don't get into an endless loop.
    ... and, I also am not sure what application this might be used for, or why you would do it.

    Can you imagine? It would be a time warp or something.

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Atlanta, GA
    Member #
    1 times
    Yeah, not knowing for certain why you'd do this, a PHP script can probably do that on Unix variants, and can definitely not do that on a Windows box.

    Why? Windows locks files when they're being accessed. So if you're accessing a file and trying to delete it, you're SOL, because Windows won't let you.

    Certain Unix-based filesystems (reiserfs, for example) don't operate this way. Instead, when you delete a file, they get rid of the pointer to the file (which is how all systems work), but they guarantee the validity of the pointers running programs have already gotten until all those programs are done running. Thus, you can delete a directory, for example, even if you have something running a file in that directory. In this case, that means you can delete your PHP script even if the PHP script is running, and it'll be gone instantly for everyone except the webserver that's actually running the file.

    In cases where file locking prevents you from deleting it, you can go a nasty roundabout way and write a list of files that need deletion to a separate file, and then run a cron (or other periodic) job that reads the file listing files that need deletion and deletes those files. Like I said, though, that's a particularl form of heinous.

    Why do you need this functionality anyway? Chances are anything that needs this can probably be done a better way :-P

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 09:31 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: