473,396 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to find out if a file exists on a remote / local server? file_exists(), fopen()?

113 100+
Hi there,

This has probably been asked a million times but having looked through the PHP manual for the file_exists() function I can't seem to get the right code for what I want to do.

I have a typical scenario of wanting to check for an image files existence before displaying, I have foubd the following code to do this but it always returns false:

Expand|Select|Wrap|Line Numbers
  1. $imageURL = "http://website.com/images/image1.jpg";
  2.  
  3. $handle = @fopen($imageURL, 'r');
  4. if ($handle===false) { 
  5.   echo "image does not exist";
  6. }
  7. else {
  8.   echo "image exists";
  9.   fclose($handle);
  10. }
  11.  
What is the best way to find out if an image exists on a remote or local server?

Thanks,

Chromis
Feb 19 '08 #1
5 13107
hi Chromis,

PHP file_exists() function takes file system path. as an argument.
Feb 19 '08 #2
chromis
113 100+
hi Chromis,

PHP file_exists() function takes file system path. as an argument.
Hi abhishektiwari,

Thanks for the reply, I wish to check if a file exists using an http address, i am thinking file_exists() is not the right function to use, what function should I use?

Thanks,

Chromis
Feb 19 '08 #3
nomad
664 Expert 512MB
Hi abhishektiwari,

Thanks for the reply, I wish to check if a file exists using an http address, i am thinking file_exists() is not the right function to use, what function should I use?

Thanks,

Chromis
yes you can use file_exist

might look something like this


if (file_exists("test.txt")) {
echo "test.txt is a file!";
}


nomad
Feb 19 '08 #4
Markus
6,050 Expert 4TB
Hi there,

This has probably been asked a million times but having looked through the PHP manual for the file_exists() function I can't seem to get the right code for what I want to do.

I have a typical scenario of wanting to check for an image files existence before displaying, I have foubd the following code to do this but it always returns false:

Expand|Select|Wrap|Line Numbers
  1. $imageURL = "http://website.com/images/image1.jpg";
  2.  
  3. $handle = @fopen($imageURL, 'r');
  4. if ($handle===false) { 
  5.   echo "image does not exist";
  6. }
  7. else {
  8.   echo "image exists";
  9.   fclose($handle);
  10. }
  11.  
What is the best way to find out if an image exists on a remote or local server?

Thanks,

Chromis
file_exists() is perfect to use! Is it not obvious? You're wanting to check if a file exists ... file_exists?

;)
Feb 19 '08 #5
chromis
113 100+
file_exists() is perfect to use! Is it not obvious? You're wanting to check if a file exists ... file_exists?

;)
Yeah it's completely obvious but what is perhaps not so obvious in my explanation is that I want to find out whether a file at a specific URL exists. file_exists() in PHP 5 does not accept URLs only local path names.

Having tryed a number of different solutions only this one worked for me:

Expand|Select|Wrap|Line Numbers
  1.     function url_exists($url){
  2.         $url = str_replace("http://", "", $url);
  3.         if (strstr($url, "/")) {
  4.             $url = explode("/", $url, 2);
  5.             $url[1] = "/".$url[1];
  6.         } else {
  7.             $url = array($url, "/");
  8.         }
  9.  
  10.         $fh = fsockopen($url[0], 80);
  11.         if ($fh) {
  12.             fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$url[0]."\n\n");
  13.             if (fread($fh, 22) == "HTTP/1.1 404 Not Found") { return FALSE; }
  14.             else { return TRUE;    }
  15.  
  16.         } else { return FALSE;}
  17.     }    
  18.  
Thanks for your input though,

Chromis
Feb 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: pescott | last post by:
I am still struggling to get some files uploaded to a database as BLOB data. I have 5 includes which allows the user to upload 5 files, numbered accordingly. However, there are script errors. Duhh,...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
2
by: Dan | last post by:
Hi, I know this code is not entirely javascript, but bare with me. Can you please tell me why this does not work: page: filemanager.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
6
by: sree | last post by:
Hi, i want to check whether a file from remote server is existed or not. The file_exists() checks for local server only. But i want a function that checks the remote server.
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
3
by: leon.san.email | last post by:
This a modified part of a larger script I made. I couldn't get it working so I made a smaller part of it - hoping to get it working. Well it still doesn't work :( The following code creates the...
5
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello! I'm trying to copy a file from another computer on the network that I do not have permission with my current logon details to access. If I open the folder using the Windows file manager...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.