Last active
September 22, 2020 12:25
-
-
Save Dare-NZ/5523650 to your computer and use it in GitHub Desktop.
Connect via FTP and check if directory exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// FTP login details | |
$ftp_server = 'yourserver.com'; | |
$ftp_server_path = '/public_html/'; | |
$ftp_user_name = 'username'; | |
$ftp_user_pass = 'password'; | |
// Connect to the FTP | |
$conn_id = ftp_connect($ftp_server); | |
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); | |
//List the files in this folder (non recursively) | |
$list = ftp_nlist($conn_id, $ftp_server_path); | |
// Loop entries | |
foreach($list as $entry) { | |
// Ignore '.' and '..', more complex versions could be in an | |
// array or even a text file ala .gitignore | |
if($entry != '.' && $entry != '..') { | |
// Use our test function | |
$is_dir = ftp_isdir($conn_id, $ftp_server_path . $entry); | |
// Log results | |
if($is_dir) | |
echo "$entry is a directory n"; | |
else | |
echo "$entry is a file n"; | |
} | |
} | |
// Close connection | |
ftp_close($conn_id); | |
// Our is_dir function, note the @ used to suppress useless warnings | |
function ftp_isdir($conn_id,$dir) | |
{ | |
if(@ftp_chdir($conn_id,$dir)) { | |
ftp_cdup($conn_id); | |
return true; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment