FTP Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsSystem AdministrationFTP Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old January 5th, 2004, 04:17 PM
king_killa king_killa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 130 king_killa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 59 m 37 sec
Reputation Power: 6
FTP isfile and isdir ?

I'm working on a little ftp browsing script...

I need to know if there is a way to check if something in an ftp folder is a file or a directory.

PHP Code:
<?php
$ftp_server 
"FOOBAR";
$ftp_user_name "FOOBAR";
$ftp_user_pass "FOOBAR";
// connection
$conn_id ftp_connect($ftp_server);

// login
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// change directory and get contents

if(isset($_GET[dir])) {
ftp_chdir($conn_id"$_GET[dir]");
}


$contents ftp_nlist($conn_id".");

// output contents

foreach ($contents as $cont) {
   echo 
"<a href=ftp.php?dir=$_GET[dir]/$cont>$cont</a> <br>";
}
ftp_close($conn_id);
?>


The only problem is, if you click on a file, it tries to load up a new directory. So I need to check to see if it's a file or a directory. any help?

Reply With Quote
  #2  
Old January 5th, 2004, 04:57 PM
n.eby's Avatar
n.eby n.eby is offline
the cake is a lie
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Sep 2003
Location: cake factory
Posts: 1,788 n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 19 h 49 m 39 sec
Reputation Power: 143
maybe you could parse the return values from ftp_rawlist(). there are some good user comments on that page.

the annotated manual kicks much booty.

Reply With Quote
  #3  
Old January 5th, 2004, 06:45 PM
king_killa king_killa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 130 king_killa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 59 m 37 sec
Reputation Power: 6
I figured that it may come down to that, I was kind of hoping there would be an isfile and isdir though

Reply With Quote
  #4  
Old January 5th, 2004, 09:55 PM
davonz davonz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 davonz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
Probably best to use ftp_rawlist() like what has been mentioned already as nlist() can not handel spaces in the directory name. As far as if there is a comand for seeing if the content in a folder is a file or a directory then there is such a command.. follow the links below. They are is_dir() and is_file() hope this helps :-)

http://nz2.php.net/manual/en/function.is-dir.php

http://nz2.php.net/manual/en/function.is-file.php

if your looking for a nice piece of software to prase ftp_rawlist() try this.

function dirList ($connection, $dir_path){ // This function will sort dir struture into files and folders
$list = ftp_rawlist($connection, $dir_path); // Returns a list of files and folders
//_________sort into two arrays ...files/folders ________
global $folders;
global $files;
for($i=0;$i<sizeof($list);$i++){
list($permissions,$next)=split(" ",$list[$i],2);
list($num,$next)=split(" ",cutspaces($next),2);
list($owner,$next)=split(" ",cutspaces($next),2);
list($group,$next)=split(" ",cutspaces($next),2);
list($size,$next)=split(" ",cutspaces($next),2);
list($month,$next)=split(" ",cutspaces($next),2);
list($day,$next)=split(" ",cutspaces($next),2);
list($year_time,$filename)=split(" ",cutspaces($next),2);
if($filename!="." && $filename!=".."){
if(substr($permissions,0,1)=="d"){
$folders[]=$filename;
}
else {
$files[]=$filename;

}
}
}
sort($folders);
sort($files);
}

Reply With Quote
  #5  
Old January 6th, 2004, 07:17 PM
king_killa king_killa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 130 king_killa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 59 m 37 sec
Reputation Power: 6
I know there are is_file and is_dir functions. but that won't work if I'm trying to see if an FTP folder has a file or dir.

I figured it out though.. quite interesting too.

PHP Code:
// set up basic connection
$conn_id ftp_connect($ftp_server);

// login with username and password
$login_result ftp_login($conn_id$user$password);

// get the file list for 
if(isset($_GET[dir])) {
ftp_chdir($conn_id"$_GET[dir]");
}

$buff ftp_nlist($conn_id'.');


// output the buffer
foreach($buff as $item) {

$res ftp_size($conn_id$item);
if(
"$res" != "-1") {
echo 
"file: $item <br>";
}
else {
echo 
"dir: $item <br>";
}
}

// close the connection
ftp_close($conn_id);

?> 

Reply With Quote
  #6  
Old January 6th, 2004, 07:25 PM
davonz davonz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 davonz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes your right :-)

Nice piece of coding to .. well done just watch the directory names with using ftp_nlist. I got caught out with this and it took me a while to figure out

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > FTP isfile and isdir ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway