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

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
主站蜘蛛池模板: 滁州市| 楚雄市| 剑川县| 南溪县| 土默特右旗| 化州市| 舞钢市| 宣化县| 开江县| 台南市| 蓬安县| 伊吾县| 梨树县| 通江县| 大埔区| 平陆县| 武义县| 京山县| 太白县| 静安区| 广州市| 昭觉县| 巩留县| 东辽县| 乐亭县| 藁城市| 天门市| 抚顺市| 广平县| 兴和县| 遵化市| 若羌县| 剑河县| 益阳市| 尖扎县| 平舆县| 淄博市| 泰顺县| 榆中县| 三门县| 富宁县|