How to Check If a File Exists on a Remote Server Using PHP

How can I check if a file exists on a remote server using PHP?

I used this, a bit easier:

// the server you wish to connect to - you can also use the server ip ex. 107.23.17.20
$ftp_server = "ftp.example.com";

// set up a connection to the server we chose or die and show an error
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_login($conn_id,"ftpserver_username","ftpserver_password");

// check if a file exist
$path = "/SERVER_FOLDER/"; //the path where the file is located

$file = "file.html"; //the file you are looking for

$check_file_exist = $path.$file; //combine string for easy use

$contents_on_server = ftp_nlist($conn_id, $path); //Returns an array of filenames from the specified directory on success or FALSE on error.

// Test if file is in the ftp_nlist array
if (in_array($check_file_exist, $contents_on_server))
{
echo "<br>";
echo "I found ".$check_file_exist." in directory : ".$path;
}
else
{
echo "<br>";
echo $check_file_exist." not found in directory : ".$path;
};

// output $contents_on_server, shows all the files it found, helps for debugging, you can use print_r() as well
var_dump($contents_on_server);

// remember to always close your ftp connection
ftp_close($conn_id);

Functions used: (thanks to middaparka)

  1. Login using ftp_connect

  2. Get the remote file list via ftp_nlist

  3. Use in_array to see if the file was present in the array

Using phpseclib to check if file already exists

There is a closing curly brace missing right before the second else keyword.

Please try to use a code editor with proper syntax highlighting and code formatting to spot such mistakes on the fly while you are still editing the PHP file.

The corrected and formatted code:

require('constants.php');
$files = $sftp->nlist('out/');
foreach ($files as $file) {
if (basename((string)$file)) {
if (strpos($file, ".") > 1) { //Checks if file
$filesize = $sftp->size('out/' . $file); //gets filesize
if ($filesize > 1) {
if (file_exists('import/' . $file)) {
echo $file . ' already exists';
} else {
$sftp->get('out/' . $file, 'import/' . $file); //Sends file over
}
} else {
echo $file . ' is empty.</br>';
}
}
}
}

Check if file exists on remote machine

The remote server probably resolves files using the Host header.

If so, you need to use a domain name.

You may be able to explicitly pass a Host header to the IP address, but I wouldn't recommend it.

How to check if a file exists from a url

You have to use CURL

function does_url_exists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($code == 200) {
$status = true;
} else {
$status = false;
}
curl_close($ch);
return $status;
}

Android checks if file exists in a remote server using its URL

I believe you are doing this in your main thread. Thats the reason its not working, you cant perform network operations in your main thread.

Try putting the code in AsyncTask or Thread.

Edit 1: As a quick fix try wrapping your "file checking code" like this:

    new Thread() {

public void run() {
//your "file checking code" goes here like this
//write your results to log cat, since you cant do Toast from threads without handlers also...

try {
HttpURLConnection.setFollowRedirects(false);
// note : you may also need
//HttpURLConnection.setInstanceFollowRedirects(false)

HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
if( (con.getResponseCode() == HttpURLConnection.HTTP_OK) )
log.d("FILE_EXISTS", "true");
else
log.d("FILE_EXISTS", "false");
}
catch (Exception e) {
e.printStackTrace();
log.d("FILE_EXISTS", "false");;
}
}
}.start();

Fastest way to check remote file existance

There is no fastest way. If you need and want to probe a remote file, then that requires a HTTP request. There are multiple options for that, but only with slight performance differences. The actual delay comes from issuing a HTTP transfer.

get_headers() is the simplest option:

 $exists = strstr(current(get_headers($url)), "200");

How do I test if an remote image file exists in php?

file_exists uses local path, not a URL.

A solution would be this:

$url=getimagesize(your_url);

if(!is_array($url))
{
// The image doesn't exist
}
else
{
// The image exists
}

See this for more information.

Also, looking for the response headers (using the get_headers function) would be a better alternative. Just check if the response is 404:

if(@get_headers($your_url)[0] == 'HTTP/1.1 404 Not Found')
{
// The image doesn't exist
}
else
{
// The image exists
}


Related Topics



Leave a reply



Submit