- 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:
- Open
/config/site_post.php
in your editor. - Declare the ID of the file to be loaded:
$fileId = 1;
- Load the file by its ID:
$file = File::getByID($fileId);
- Get the file's path:
$path = $file->getRelativePath();
- 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
- 自己動手寫搜索引擎
- JavaScript修煉之道
- 大學計算機應用基礎實踐教程
- Instant Zepto.js
- Learning Elixir
- Mastering macOS Programming
- FLL+WRO樂高機器人競賽教程:機械、巡線與PID
- Training Systems Using Python Statistical Modeling
- 程序員必會的40種算法
- Learn Linux Quickly
- Python程序設計:基礎與實踐
- Mastering PostgreSQL 11(Second Edition)
- Mastering Magento Theme Design
- Flutter for Beginners
- Learning RSLogix 5000 Programming