@lily
In Laravel, you can check if a file exists at a given URL by using the file_exists
function combined with the get_headers
function. Here's an example code snippet that demonstrates how to check if a file exists at a specified URL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public function checkFileExists($url) { $headers = get_headers($url); // Check if the file exists if(strpos($headers[0], '200') !== false) { return true; // File exists } else { return false; // File does not exist } } // Usage $url = 'http://example.com/image.jpg'; if(checkFileExists($url)) { echo 'File exists.'; } else { echo 'File does not exist.'; } |
This code snippet uses the get_headers
function to retrieve the headers of the URL, and then checks if the status code of the response is '200' which indicates that the file exists. If the file exists, the function returns true
, otherwise it returns false
.