asifkamalzahid
11-17-2008, 06:58 AM
can some help me that
how i can upload the data from one directory into other directory by ftp. and after uploading i want to clean my directory from where i upload the data.
i want to do this in PHP
thanks
asifkamalzahid
11-17-2008, 09:50 AM
<?php
//Move or Upload Folder Using PHP FTP Functions
function moveFolder($_server, $_user_name, $_user_pass, $local_dir, $remote_dir) {
//$ftp_server = "hostname";
//$ftp_user = "username";
//$ftp_pass = "password";
// set up a connection or die
echo"$_user_name";
echo"$_user_pass";
//$_conn_id = ftp_connect($_server) or die("Couldn't connect to $ftp_server");
// try to login
//if (ftp_login($conn_id, $_user_name, $_user_pass)) {
//echo "Connected as $_user_name@$_server\n";
//$result = true;
//}
// else
// {
//echo "Couldn't connect as $_user_name\n";
//$result = false;
//}
// set up basic connection
$_conn_id = ftp_connect($_server);
// login with username and password
$_login_result = ftp_login($_conn_id, $_user_name, $_user_pass);
// check connection
if ((!$_conn_id) || (!$_login_result)) {
$_error = "FTP connection has failed!";
$_error .= "Attempted to connect to $_server for user $_user_name";
$result = false;
} else {
$_error = "Connected to $_server, for user $_user_name";
}
$conn_id = $_conn_id;
@ ftp_mkdir($conn_id, $remote_dir);
$handle = opendir($local_dir);
while (($file = readdir($handle)) !== false) {
if (($file != '.') && ($file != '..')) {
if (is_dir($local_dir . $file)) {
//recursive call
moveFolder($conn_id, $local_dir . $file . '/', $remote_dir . $file . '/');
} else
$f[] = $file;
}
}
closedir($handle);
if (count($f)) {
sort($f);
@ ftp_chdir($conn_id, $remote_dir);
foreach ($f as $files) {
$from = @ fopen("$local_dir$files", 'r');
$moveFolder = ftp_fput($conn_id, $files, $from, FTP_BINARY);
// check upload status
if (!$moveFolder) {
$this->_error = "FTP upload has failed! From:" . $local_dir . " To: " . $remote_dir;
$result = false;
} else {
$this->_error = "Uploaded $local_dir to $remote_dir as $this->_server";
$result = true;
}
}
}
return $result;
}
?>
asifkamalzahid
11-17-2008, 09:53 AM
i have tried with this code .it is ok with connection,but i dont know how to upload my local computer directory to server and on the server i want to save this directory in new directory.my html for is below
<form method="post" action="upload.php">
Move or Upload Folder Using PHP FTP Functions<br />
</p>
<table width="373" border="1">
<tr>
<td width="184">server address </td>
<td width="173"><input type="text" id="server_name" name="server_name" /></td>
</tr>
<tr>
<td>user name </td>
<td><input type="text" name="user_name" id="user_name" /></td>
</tr>
<tr>
<td>user password</td>
<td><input type="text" id="user_password" name="user_password" /></td>
</tr>
<tr>
<td>local directory</td>
<td><input type="text" name="local_dir" id="local_dir" /></td>
</tr>
<tr>
<td>remote directory</td>
<td><input type="text" name="remote_dir" id="remote_dir" /></td>
</tr>
<tr>
<td><input name="upload" type="submit" id="upload" /></td>
<td> </td>
</tr>
</table>
<p> </p>
</form>