Skip to content
PHP Logo

How can absolute paths be used in PHP to ensure correct file and image display?

When working with files and images in PHP, using absolute paths ensures that the correct files are accessed regardless of the current working directory. This is especially important when including files or displaying images on a webpage to prevent broken links or missing files.

php
// Using absolute paths to ensure correct file and image display
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$imagePath = $rootPath . '/images/image.jpg';

echo '<img src="' . $imagePath . '" alt="Image">';