tatty27
tatty27

Reputation: 1554

get name of single file in a directory using php

Although I can find plenty of examples of listing all of the files in a directory

$dir = '../upload/'.$id.'/ask_temp';

But I need to get the name of a single file so I can store it in a variable to use elsewhere.

There is and only ever will be one file in there.

Upvotes: 0

Views: 42

Answers (1)

MisterBla
MisterBla

Reputation: 2385

In regards to ComFreek's answer, make sure to filter out any directory. (. and ..)

I'm assuming you're using at least PHP 5.3

$files = array_filter(scandir($dir), function($val) use($dir)
{
    return is_file($dir.'/'.$val);
});

$myFile = $files[0];

Another way is to this, this is probably easier. (first 2 are always '.' and '..')

$files = scandir($dir);
$myFile = $files[2];

Upvotes: 1

Related Questions