Connect with us

Tip: Create temporary files in PHP

coding

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  0 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');

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 selling plugins on CodeCanyon. I will show you every step of it: learning to code, plugin ideas, WordPress stuff and more. Read more…

Mega Plugin Bundle:

CodeCanyon Portfolio:

Learn to Code Plugins:

Latest Promotions:

Recommended Theme:

Start Your Own Blog:

Popular Posts:

Latest Posts:

To Top
Grab the largest plugin bundle from CodeCanyon!