473,513 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dir navigation with php ftp functions

I'm having trouble navigating my directory structure using php's ftp
functions (such as ftp_chdir and ftp_cdup.) I'm writing a program that
allows a user to easily create robot.txt files and upload them to
their server. They should be able to log in to their server using ftp
and browse their directory structure, specifying which robots to allow
into each directory.

When the user logs into their server, they are presented with a list
of directories displayed as links. Clicking a directories link will
take them into that directory and will present them with the
sub-directories contained in that directory. I have the program
running at

http://www.storeminder.biz/robots/

Each time is link is clicked, the page is refreshed and ftp_connect
called, meaning that the user is logged back in to their root
directory. I have a stack containing the directories that have been
navigated, so that the program can direct the user to the drectory
they intend to be in.

This seems overly complicated. Is it possible to do what I am trying
to do just using php's ftp functions, without storing a session
variable containing all the directories that have been navigated? Is
is possible to not have to re-login to the root directory every time a
page that uses ftp functions is called?

The solution I have seems to work well and handles symbolic links, but
has the drawback that I can't navigate above the directory that the
user originally logs into. It also just doesn't seem like the correct
way to do this. I'm sure there is a simpler, more proper way.

Any ideas would be greatly appreciated.
I'm posting my code below. I tried to make it well commented and easy
to understand.

Thanks,

Esoos
<?php

include 'include.php';
session_start();

//If $_POST variables are set then we are logging in for the first
time and set $_SESSION variables accordingly
if (isset($_POST['server'])) {
$post = clean_post($_POST);
$_SESSION['ftp_server'] = $post['server'];
$_SESSION['ftp_username'] = $post['username'];
$_SESSION['ftp_password'] = $post['password'];
//Create a stack/array which holds the name of the directories we
have traversed
$_SESSION['directories'] = Array();
}

//if we are not connected to a server then redirect to the login page
if (!isset($_SESSION['ftp_server'])) {
header("Location: login.php");
exit;
}

//If $_GET variables are set then we are actively navigating the
directory structure
if (isset($_GET['dir'])) {
$get = clean_get($_GET);
$ftp_dir = $get['dir'];
}

// set up a connection or die
$conn_id = ftp_connect($_SESSION['ftp_server']) or die("Couldn't
connect to " . $_SESSION['ftp_server'] . "<br>");

// try to login
if (@ftp_login($conn_id, $_SESSION['ftp_username'],
$_SESSION['ftp_password'])) {
echo "Connected as " . $_SESSION['ftp_username'] . "@" .
$_SESSION['ftp_server'] . "<br>";
$_SESSION['username'] = $_SESSION['ftp_username'];
} else {
echo "Couldn't connect as " . $_SESSION['ftp_username'] . "<br>";
}

// turn passive mode on
ftp_pasv($conn_id, true);

//If we are navigating the directoty structure
if (isset($ftp_dir)) {
//if we are navigating to the parent directory pop the name of the
current directory off the stack
if($_GET['act'] == 'cdup') {
array_pop($_SESSION['directories']);
}
//otherwise we are navigating to a child directory
else {
//grab the name of the current directory and push it on the
$_SESSION['directories'] stack
$pieces = explode("/", $ftp_dir);
$relative_path_dir = array_pop($pieces);
array_push($_SESSION['directories'], $relative_path_dir);
}
//Since the stateless nature of http forces us to always login to
the root directory
//we use the $_SESSION['directories'] stack/array to navigate to
child directory we are trying to access
foreach($_SESSION['directories'] as $rel_dir) {
if (ftp_chdir($conn_id, $rel_dir)) {
//do nothing for now
} else {
echo "Couldn't change directory\n";
}
}
//we are in the child directory, so display the path
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
}
else {
//we are in the root directory, so display the path
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
}

function create_dir_tree() {
global $conn_id;
$files = ftp_nlist($conn_id, ""); //get files in directory
if(is_array($files)) {
//display a link to the parent directory
echo "<br /><a href=index.php?&act=cdup&dir=" . ftp_pwd($conn_id)
.. ">parent directory</a>";
foreach ($files as $file) {
$isfile = ftp_size($conn_id, $file);
if($isfile == "-1") { //Is it a directory? (as opposed to a file)
//display a link to each child directory
$string = "<br /><a href=index.php?&dir=" . ftp_pwd($conn_id) .
"/" . $file . ">" . $file . "</a>";
//replace any double slashes with single slashes
$url = preg_replace("/\/\//", "/", $string);
print $url;
}
}
}
else echo "<br /><a href=index.php?&act=cdup&dir=" .
ftp_pwd($conn_id) . ">parent directory</a>";
}
?>
<html>
<head></head>
<body>
<table border="1" cellSpacing="3" cellPadding="3" width="100%">
<tr>
<td>
<?php create_dir_tree(); ?>
</td>
<td>
<form method="post" action="create_text.php">
<table border="1" cellSpacing="3" cellPadding="3" width="100%">
<tr>
<td>
<ul>
<li><input type="checkbox" id="all" name="all">* (all
spiders)</input></li>
<li><input type="checkbox" id="googlebot"
name="googlebot">googlebot</input></li>
<li><input type="checkbox" id="askjeeves"
name="askjeeves">askjeeves</input></li>
<li><input type="checkbox" id="slurp" name="slurp">inktomi
slurp</input></li>
<li><input type="checkbox" id="scooter"
name="scooter">altavista scooter</input></li>
<input type="hidden" name="path" value="<?php echo
ftp_pwd($conn_id) ?>"></input>
</ul>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="Submit"></input>
<input type="hidden" name="activity"
value="logging_in"></input>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

<?php
// close the connection
ftp_close($conn_id);
?>
Jul 17 '05 #1
0 2038

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2110
by: Veli-Pekka Tätilä | last post by:
Hi, My first post here. I've found some serious accessibility flaws in the Python 2.4 docs and wish they could be rectified over time. I'm very new to Python and initially contacted docs at python...
2
5155
by: Mitch | last post by:
I am hosting a web browser ctl in a container that implements the IDocHostUIHandler interface. I'm using this to control the context menu.This works fine. Then, I added a mouseover event to the...
2
563
by: Jay | last post by:
I have used Smart Navigation on several projects and it works wonderfully. For my last project I used some javascript menus. SmartNavigation caused them to not function. So I used SmartScoller...
28
2650
by: laredotornado | last post by:
Hi, Surprisingly, I can't get the drop down menus to work on PC IE 6. If you roll over "PRODUCTS", normally a drop down menu appears (on Safari and Firefox), but on PC IE, nada. ...
0
1742
by: Andy_Khosravi | last post by:
I'm having a problem trying to optimize the performance of one of my A97 databases. I have very slow record navigation after a change I made to the table structure, and I'm not sure how best to...
3
2500
by: Paul | last post by:
I want the <div id="navigation"column to be the same color all the way to the bottom. The "background-image: url(bg_menu_tile.gif);" was a try to force it with a long 1-pixel graphic - didn't...
10
2516
by: EA | last post by:
I am sure I must be missing something about building navigation bars with CSS. Yes it is a very clever and efficient way to format navigation structures on simple one navigation level webs, i.e....
4
2893
by: Jamey Shuemaker | last post by:
Howdy, Saw a couple threads from the past few years on this topic, but didn't really find any solutions. Here's one I found:...
5
1465
RMWChaos
by: RMWChaos | last post by:
Apparently, I can't do anything the easy way, which seems to lead me here so very, very often. Here is what I am trying to do this time: 1. Autogenerate two navigation bars, "navLeft" and...
0
1944
by: emalcolm_FLA | last post by:
Hello and TIA for your consideration. I have created several db's for a non-profit and they want custom navigation buttons to display "You are on the first record, last record, etc". With this...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.