PHP检查文件是否在不同域名上的存在情况
Jun 13, 2016 am 10:47 AM
PHP检查文件是否在不同域名下的存在情况
Earlier today I needed to find out if a file exists on a different domain. Initially I used the file_exists function, but then when that threw back an error I remembered that file_exists only checks whether a file or directory exists on the same server as the script.
After I played around with various functions, I came up with a few lines of code that actually works:
How to check if file exists on a different domain
$image = "http://www.example.co.uk/images/1.jpg";$handle = @fopen("$image", "r");if(strpos($handle, "Resource id") !== false){echo "file does exist";}else{echo "file does not exist";}?>
The logic explained
Ok, so if the file exists (1.jpg) the fopen function will throw back a “resource id” response. So I check the response to see if “response id” exists with the strpos function. It’s really as simple as that.
I’m not entirely sure if my method is the best, nor the most efficient, but it seems to work pretty well, and I can’t think of any other methods. Anyone know of any other/better methods?
Better solution
Thanks to a comment left by Paul I’ve been made aware of a better solution.
?
$url = "http://www.example.com/index.php";$header_response = get_headers($url, 1);if ( strpos( $header_response[0], "404" ) !== false ){ // FILE DOES NOT EXIST} else { // FILE EXISTS!!}
?
?

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Poésie aléatoire d'application native Hongmeng

Utilisez la fonction File.length() de Java pour obtenir la taille du fichier

Comment convertir un blob php en fichier

Renommez les fichiers à l'aide de la fonction File.renameTo() de Java

Après 2 mois, le robot humanoïde Walker S peut plier les vêtements

Développement Laravel : Comment renvoyer une réponse en utilisant Laravel Response ?

Utilisez la fonction File.getParentFile() de Java pour obtenir le répertoire parent du fichier
