Tomás Juárez
Tomás Juárez

Reputation: 1524

Regex for files path

I'm trying to validate the path of an uploaded file in php, that must be validated with regular expressions and not with native functions.

The path that I'm getting is:

C:\xampp\tmp\php33DB.tmp

So, when I test this path with /^[a-zA-Z0-9\-\_:\\]*\Z/ returns false.(I think it's because the path have the backward slashes \ )

There's a way to validate the path for Linux and Windows servers of the files uploaded in PHP only with regex?

Upvotes: 2

Views: 1748

Answers (2)

chrislondon
chrislondon

Reputation: 12041

I recommend using this function instead: file_exists

So in your code you could do:

if (file_exists($tmpFile)) {
    // ... do stuff
}

Upvotes: 0

user1646111
user1646111

Reputation:

$text = 'C:\xampp\tmp\php33DB.tmp';
preg_match("#^[a-zA-Z0-9_\\\\:.-]+#",$text,$out);
print_r($out);

Output:

Array ( [0] => C:\xampp\tmp\php33DB.tmp )

However, this file is temporary file that created by PHP upload, I think no need to validate it because it was created by server not client, you can just check the mime type of uploaded file, and check is_uploaded_file() if the file was uploaded by HTTP Post.

Upvotes: 3

Related Questions