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

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
主站蜘蛛池模板: 海盐县| 桓台县| 青海省| 长沙市| 海原县| 鲜城| 咸宁市| 曲周县| 岐山县| 内江市| 保康县| 陈巴尔虎旗| 成武县| 乌苏市| 崇礼县| 南漳县| 凌海市| 五指山市| 敦煌市| 宜兰市| 永德县| 湟中县| 赞皇县| 威信县| 仪征市| 巴楚县| 商洛市| 中方县| 长武县| 黔南| 乾安县| 驻马店市| 南木林县| 安国市| 迁西县| 内丘县| 临夏市| 黄大仙区| 原平市| 五峰| 八宿县|