Connect with us

Tip: Create temporary files in PHP


Tip: Create temporary files in PHP

PHP provides two very useful functions to create temporary files. Instead of creating unnecessary tmp directories in your applications, it’s better to rely on the global directory assigned for that matter.

To create a unique file with the right permissions use the tempnam function:

$filename = tempnam('/tmp_directory', 'filePrefix');

You can test this from the command line like this:

php -R 'tempnam("/tmp", "hello");'

The output file looks something like this:

my-macbook:tmp willy$ ls -la hello*

-rw-------  1 willy  staff   Mar 11 03:26 helloyvEzzb

Couple this function with sys_get_temp_dir and you’ve solved the dilemma:

$filename = tempnam(sys_get_temp_dir(), 'filePrefix');

Related:  [WordPress] Custom Post Type Single Page not Found 404 Error

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in coding

Sign up for our newsletter and get a free affiliate e-book!

About Me:

Szabi Kisded

Hi, my name is Szabi and I'm documenting my journey earning an online (semi)passive income. Learn more

Mega Plugin Bundle:

CodeCanyon Portfolio:

Online Courses:

Recommended Theme:

Popular Posts:

Latest Posts:

To Top