What potential issues can arise when displaying images on a website using PHP?
One potential issue when displaying images on a website using PHP is the risk of exposing sensitive file paths or directories to users. To solve this issue, it's important to ensure that the image paths are not directly accessible by users and to properly sanitize user input to prevent directory traversal attacks.
// Example code snippet to prevent directory traversal attacks
$imagePath = 'images/' . basename($_GET['image']);
if (file_exists($imagePath)) {
// Output image
header('Content-Type: image/jpeg');
readfile($imagePath);
} else {
// Handle error
echo 'Image not found';
}
Related Questions
- How can PHP beginners effectively manage and implement IP bans in their projects?
- What are the best practices for handling user registration and profile creation in PHP to avoid issues with file management?
- What are the potential pitfalls of using srand() and shuffle() functions multiple times in PHP code?