- 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
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- JSP網(wǎng)絡(luò)編程(學(xué)習(xí)筆記)
- Mastering JavaScript Object-Oriented Programming
- 垃圾回收的算法與實現(xiàn)
- Machine Learning with R Cookbook(Second Edition)
- Raspberry Pi for Secret Agents(Third Edition)
- 重學(xué)Java設(shè)計模式
- ADI DSP應(yīng)用技術(shù)集錦
- Visual C#通用范例開發(fā)金典
- Java系統(tǒng)化項目開發(fā)教程
- Scala Reactive Programming
- Android移動開發(fā)案例教程:基于Android Studio開發(fā)環(huán)境
- Spring Boot+MVC實戰(zhàn)指南
- JavaScript+jQuery網(wǎng)頁特效設(shè)計任務(wù)驅(qū)動教程
- 實戰(zhàn)Java高并發(fā)程序設(shè)計(第2版)