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)
Login using ftp_connect
Get the remote file list via ftp_nlist
Use in_array to see if the file was present in the array
How to check whether file exist or not in given URL?
You can use fopen() to check either file exists or not.
$url = "https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight";
// Open for reading only.
if(@fopen($url, 'r') ){
echo 'File Found';
}else{
echo 'file not found';
}
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
}
check if file exists in php
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
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>';
}
}
}
}
Verify remote file exists with SplFileInfo
There is no equivalent of file_exists()
for the SplFileInfo
class.
SplFileInfo::isFile()
is a different check, which correlates with the is_file()
function (they share underlying code).
You could call the file_exists()
function on $imageLocation
, provided it references a stream wrapper that supports stat()
functionality. Wrappers such as file://
, phar://
and ftp://
(the latter only partially) support this.
Unfortunately the http://
(and https://
) wrapper does not support stat()
calls. This means file_exists()
, and other functions that call stat()
, will return false
.
Related Topics
Reference: What Is a Perfect Code Sample Using the MySQL Extension
How to Convert an Array to Object in PHP
How to Properly Set Up a Pdo Connection
How to Validate an Email Address in PHP
Using Jquery $.Ajax to Call a PHP Function
Sort an Array by Keys Based on Another Array
How to Create Friendly Url in PHP
How to Prevent Form Resubmission When Page Is Refreshed (F5/Ctrl+R)
Jquery Validate Remote Method Usage to Check If Username Already Exists
Error Message "Strict Standards: Only Variables Should Be Passed by Reference"
PHP Does Not Display Error Messages
What Are the "Standard" Timezone Abbreviations
Finding the Number of Days Between Two Dates