Home > other >  How to check string is a file?
How to check string is a file?

Time:04-24

I have stored file names and datas in single 'comment' column in database. I want to check the string is file or not. If it is file, then I need to show file link. I tried

var_dump(is_file('path/filename'))

This always shows bool(false) as output even in image.jpg file. How to check the string is file or not?

CodePudding user response:

You can do with glob function

$result = glob("./path/filename.*");

CodePudding user response:

The php functions, like is_file(), taking path arguments, handle absolute (starting with /) and relative pathnames differently.

Relative pathnames like your path/filename example use the path of your php program as a starting point. So if your php program is in /var/www/code/foo.php, your example looks for /var/www/code/path/filename. It seems unlikely you want that.

Edit Files you serve to your audience via your web server have two pathnames. For example.

  1. https://example.com/img/flower.jpg is its URL, visible to the audience,
  2. /var/www/img/flower.jpg is its pathname in the file system of the server machine.

is_file() uses the second one.

You didn't tell us enough about your server's file system layout or the paths you store in your database for us to suggest a definitive fix to your problem.

  •  Tags:  
  • php
  • Related