How can PHP be used to check if a requested file exists on a server and redirect to another address if it doesn't?
To check if a requested file exists on a server and redirect to another address if it doesn't, you can use the `file_exists()` function in PHP to check if the file exists. If the file does not exist, you can use the `header()` function to redirect the user to another address.
<?php
$file = 'path/to/file.txt';
if (file_exists($file)) {
// File exists, do something
} else {
header('Location: http://www.example.com/redirected_page.html');
exit;
}
?>
Keywords
Related Questions
- What are the potential pitfalls of not explicitly defining a variable as true or false in PHP?
- How can PHP developers optimize the use of SQL queries with the LIMIT clause to fetch specific entries based on non-sequential IDs?
- What is the significance of giving a name attribute to a select field in HTML forms when working with PHP?