Are there any specific file formats that should be avoided when using PHP to display images from a folder on a webpage?
When displaying images from a folder on a webpage using PHP, it is recommended to avoid using potentially harmful file formats such as .php, .html, .js, or any other executable file formats. This is to prevent any security risks such as code execution vulnerabilities. It is best to restrict the allowed file formats to only image file types such as .jpg, .png, and .gif.
$allowedFormats = ['jpg', 'jpeg', 'png', 'gif'];
$files = glob('path/to/images/*');
foreach ($files as $file) {
$fileInfo = pathinfo($file);
if (in_array(strtolower($fileInfo['extension']), $allowedFormats)) {
echo '<img src="' . $file . '" alt="' . $fileInfo['filename'] . '">';
}
}
Keywords
Related Questions
- How can errors in PHP scripts be effectively handled and displayed using mysql_error()?
- How can prepared statements and parameterized queries be utilized to prevent SQL injection attacks in PHP applications?
- How can mismatched certificate errors be resolved when using a mailer class like PHPMailer or SwiftMailer for email sending in PHP?