PDA

Click to See Complete Forum and Search --> : Uploading files on server


RameshChaudhary
Apr 24th, 2007, 01:11 AM
Still i am unable to uplod file on server. Please have look code bleow


<?
////////////Following Script Dowloads contents from Phonocat FTP Server Respective folders///////////////////////
set_time_limit(0);
// Videoservicesftp server connection variables

$ftp_server = "######";
$conn_id = ftp_connect($ftp_server) or die("Unable to connect to the server");
$ftp_user_name = "####";
$ftp_user_pass = "####";
//////////////////////////////////////////////////////////LOGIN WITH USER AND PASSWORD//////////////////////////////////////////////
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

////////////////////////////////CHECHK FTP CONNECTION ////////////////////////////////////////////////////////////////////////

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

/////////////////////////// GET FTP DIRECTORY /////////////////////////////////////////////////////////////////////////////
ftp_pasv($conn_id, true);
if (ftp_chdir($conn_id, "pub2")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
echo "<br>";
//echo $curdir = ftp_pwd($conn_id);

// file to upload:

$local_file = glob("D:/nusphere/apache/htdocs/Msafe/VSN/order/*.txt");

// perform file upload
for($i=0;$i<count($local_file);$i++);

$upload = ftp_put($conn_id, $curdir, $local_file[$i], FTP_BINARY);

// check upload status
if (!$upload)
echo "FTP upload has failed!";
else
echo "Uploaded successful...........";

// close the FTP stream
ftp_close($conn_id);
?>


Regards
Ramesh

kows
Apr 24th, 2007, 01:58 PM
well. the way you're checking the status of uploaded files is.. wrong? you are redefining $upload for every file found, so you're technically only checking if the last file in your matched list was a success or not.

you're also not defining $curdir, so you technically aren't uploading it to anywhere. however, even if you were defining $curdir, it would be incorrect. you need to supply a destination filename (not a destination directory) when you're setting the remote_path with ftp_put().

this is my modified and rewritten version of your script -- it gives a bit more detailed output as it goes along and will tell you exactly which files failed to upload.

<pre>
<?php
function timestamp(){
return "\r\n" . '[' . date('H:i') . '] -> ';
}
set_time_limit(0);

//server information
$ftp = array();
$ftp['server'] = 'ftp.domain.tld';
$ftp['port'] = 21;
$ftp['user'] = 'username';
$ftp['pass'] = 'password';

//attempt to connect and login
$conn = @ftp_connect($ftp['server'], $ftp['port']);
$login = @ftp_login($conn, $ftp['user'], $ftp['pass']);

//did we connect?
if($conn && $login) {
echo timestamp() . "succesfully connected to {$ftp['user']}@{$ftp['server']}:{$ftp['port']}";
}else{
echo timestamp() . "failed to connect to {$ftp['user']}@{$ftp['server']}:{$ftp['port']}";
exit;
}

//turn on passive mode if you need it
ftp_pasv($conn, true);

if(ftp_chdir($conn, 'public_http')){
echo timestamp() . 'moved to ' . ftp_pwd($conn);
}else{
echo timestamp() . 'failed to change directory';
}

//list out our currently directory
echo "\n\n";
print_r(ftp_rawlist($conn, '/' . ftp_pwd($conn) . '/'));

//we want to upload every php file in the root directory
$ftp['uploads'] = glob('e:/root/*.php');

foreach($ftp['uploads'] as $localpath){
//we need the filename of each $localpath ONLY
$remotepath = substr($localpath, strrpos($localpath, '/') + 1, strlen($localpath));
$upload = ftp_put($conn, $remotepath, $localpath, FTP_ASCII);
echo timestamp() . 'upload of "' . $localpath . '" to "' . $remotepath . '" -- status=';
if(!$upload){
echo '<strong><span style="color: #f00;">FAILED</span></strong>';
}else{
echo '<strong><span style="color: #0f0;">COMPLETED</span></strong>';
}
}
//disconnect
ftp_close($conn);
echo timestamp() . "disconnected";
?>
</pre>