官术网_书友最值得收藏!

Getting a file's path

Once you have loaded the file object, there are a multitude of different methods that you can call, to get all of the different attributes associated with that file. One of the most common and important attributes is that file's path.

Getting ready

In this recipe, we will get a file's path and use it to display an image stored in the file manager.

Once again, we will be referencing an image with the ID of 1. Be sure to adapt the code in this recipe to load an image that actually exists in your concrete5 installation.

How to do it...

Have a look at the following steps:

  1. Open /config/site_post.php in your editor.
  2. Declare the ID of the file to be loaded:
    $fileId = 1;
  3. Load the file by its ID:
    $file = File::getByID($fileId);
  4. Get the file's path:
    $path = $file->getRelativePath();
  5. Output the path to the screen:
    echo $path;
    exit;

How it works...

The file manager stores files in various locations through the concrete5 website, typically beneath the files/ folder, which gets broken up into a few machine-readable folders consisting of integers. Since these paths are too hard to figure out and predict on a regular basis, the file object API allows us to get those paths with a simple function call.

There's more...

You will need the actual absolute path of a file, if you are working with the image in PHP's fopen function, for instance. For that, you will use the getPath function:

$fullPath = $file->getPath();

See also

  • The Loading a file by its ID recipe
主站蜘蛛池模板: 丰县| 铜山县| 福海县| 安宁市| 葫芦岛市| 都兰县| 新民市| 大方县| 台湾省| 化德县| 马山县| 印江| 新泰市| 塔河县| 保康县| 忻城县| 盐池县| 中西区| 邳州市| 曲阜市| 平顶山市| 南丹县| 类乌齐县| 星座| 通海县| 井研县| 景洪市| 达日县| 长沙县| 黎川县| 庆元县| 读书| 小金县| 龙川县| 东港市| 策勒县| 莒南县| 通道| 增城市| 牡丹江市| 册亨县|