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

  • concrete5 Cookbook
  • David Strack
  • 262字
  • 2021-08-13 16:16:03

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
主站蜘蛛池模板: 柞水县| 壶关县| 富顺县| 扶绥县| 牙克石市| 吉安市| 溆浦县| 汕尾市| 温宿县| 古浪县| 江油市| 衡南县| 万盛区| 大埔县| 长丰县| 星子县| 潼南县| 高青县| 兴化市| 英德市| 西畴县| 文水县| 烟台市| 石门县| 铜梁县| 宁强县| 南昌市| 惠东县| 平和县| 柳州市| 剑川县| 嵊州市| 延长县| 威远县| 汝城县| 奎屯市| 绥宁县| 萍乡市| 德安县| 武陟县| 遵义市|