Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:50:02+00:00 2026-05-24T05:50:02+00:00

I would like to be able to copy a text file that’s on my

  • 0

I would like to be able to copy a text file that’s on my server, edit it based on the user input, and allow them to download the new file, but then delete it either after their session is done, or a certain amount of time.

I know how to copy and edit files, but I don’t know how to make temporary files or a temporary directory, or how to dynamically name them… If multiple people are making files at once, I don’t want to run into problems with making two files of the same name.

I’ve tried searching, but I’m not exactly sure how to phrase it. I’ve come across some pages on the PHP site about temporary files that are deleted as soon as the PHP stops running, but that wouldn’t allow the users to download the file.

What exactly I would like to know:

-How to dynamically name copies of server files that are automatically deleted after a certain amount of time.

edit: Sorry, I know this might be more of a website problem than PHP, but I’m new to both.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:50:02+00:00Added an answer on May 24, 2026 at 5:50 am

    You can use tempnam() to create a unique filename:
    http://www.php.net/manual/en/function.tempnam.php

    There is also tmpfile(), which does the same, except that it a) Also creates the file and returns a file handle instead of a filename and b) automaticlly deletes the file at fclose() or when the script ends.

    If you want to copy a file, then tempnam() would probably be the best suited.

    You can delete the files manually at the end of the script, or just use a cronjob to delete them if there more than a day old. If you can’t set cronjobs, you can create a “adminpage” and fetch it every day orso using a cronjob (Or scheduled task) from another machine.

    To answer the question “How do I delete old files”:

    First, you will probably want to set the $prefix argument for tempnam() to a “separate” directory. By default, it’ll typically use /tmp/ which holds lots of other stuff. Use /tmp/mydir/ (Make sure to check if it exists before writing to it!) or something in your home directory.

    After that, you have two methods to find and clean up the files.

    The first is using find, this is a powerful commandline tool which, as you might of guessed, finds files. As command fill in:

    find /tmp/mydir/ -type f -and -mtime -1d -delete
    

    To briefly explain: -type f means it only matches files (and not directories), -mtime -1d means it only matches files with a modification date of minus 1 day ago. And finally -delete will delete the file.

    find is very powerful and useful, but may also be a bit daunting.

    Another method is writing your own (PHP) script to search for files and call that. You can use scandir() to get a list of a directory contents, and stat() to get various info about files (Such as modification date).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div containing some text that I would like the user to
We would like to be able to nightly make a copy/backup/snapshot of a production
I would like to be able to use the Tab key within a text
I would like to be able to display some dynamic text at the mouse
I would like users to be able to copy and paste embed tags for
In my Android app, I would like to be able to copy a read
I've written a program that highlights numbers and copies them. I would like to
I would like to be able to copy a table and it's data and
Would like to be able to set colors of headings and such, different font
I would like to be able to loop through all of the defined parameters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.