Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3

Thread: PHP FTP lnist

  1. #1
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    ive wrote a scipt that connects to a ftp server gets the folders contents echos it and tells you if its a folder but having a problem with
    Code:
    .
    and
    Code:
    ..
    directorys showing as files try it to see what i mean

    any thoughts?

    btw first time using ereg();

    PHP Code:
    <?php
    $ftp_server 
    "";
    $ftp_user_name "";
    $ftp_user_pass "";

    $conn ftp_connect("$ftp_server") or die("Could not connect");

    ftp_login($conn,"$ftp_user_name","$ftp_user_pass");

    $files ftp_nlist($conn,"htdocs");

    foreach (
    $files as &$value) {
        echo 
    "$value \t";
        
    $ereg ereg("\.",$value);
        if ( 
    $ereg == "1") {
            echo 
    "Is A File<br>";
        }
        else
        {
            echo 
    "Is A Folder<br>";
        }
    }
    ftp_close($conn);
    ?>
    Regards Phil,


  2.  

  3. #2
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    I assume you're using a regular expression to find a dot in the name to tell whether it's a file or folder.

    What you need is something to differentiate '.' and '..', like this:

    PHP Code:
    if (ereg("/^\.\.$/"$value) == || ereg("/^\.$/"$value) == || ereg("\."$value) !== 1) echo "Is a Folder";

    /* "/^\.\.$/" is a regular expression to show just two dots in the string, the ^ and $
     marking the start and end of the string respectively.
    "/^\.$/" is the same, only it's for one dot. The forward slashes are just delimiters
     to separate the expression from plain text. I think. Remove them if things cock up. */

    else echo "Is a File"
    It's not particularly tidy and I know it could be cleaned up a lot, but that's the gist of what you want done. Your regexp needs to take those two directories into account.

    While I hope that works (I'm not confident, really, but it should point you in the right direction), you could always just change your single regular expression to search for a dot at the end of a string. Like this:

    PHP Code:
    if (ereg("^(.*?)\.(.*?)$"$value) == 1) echo "Is a File";
    else echo 
    "Is a Folder"
    That one is shorter and might work better. It will search the string for any character,s then a dot, and then any other characters, to decide if it's a file or not. The problem is that it might treat '.' or '..' as characters also

    I'm sorry that all this is a mashup of ideas and is quite all over the place, but hopefully it will at least be useful in inspiring you to find the best solution.

    Oh, last idea that doesn't use regular expressions, but the length of the string. The problem with this one is it doesn't help you learn about regular expressions, but anyway:

    PHP Code:
    if (strlen($value) > && ereg("\."$value) == 1) echo "Is A File";
    else echo 
    "Is A Folder"
    You'd have to use it in combition with the small regexp, and that may also be inefficient, but it's another way around it.

    Apologies again for the somewhat confusing post.

  4. #3
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    cheers that last 1 did the trick. nice one!
    Regards Phil,



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:04 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com