I'm trying to download images from a remote FTP server and upload them to our own rackspace account. This goes fine for about 3000 images but then it crashes and gives me the following error:
exception with message 'ftp_fget(): Transfer complete.'
We've tried changing the code to use ftp_get(), to not use a temp file to store it in, but it always resulted in the same error. It always fails on the same files, if I were to delete a couple of files that were already downloaded and run the scripts again it has no problem downloading them... it just fails again once it hits those specific images on the FTP server. I've tried downloading those images manually from the server and it worked, it seems nothing is wrong with them.
This is basically the code that does it:
$this->handle = ftp_connect($ftpurl);
$loggedIn = ftp_login($this->handle, $ftpusername, $ftppassword);
if ($loggedIn === false) {
throw new Exception('Can\'t login to FTP server');
}
if ($this->handle === false) {
throw new Exception('Could not connect to the given url');
}
ftp_pasv($this->handle, true);
$fileList = ftp_nlist($this->handle, '.');
if (count($fileList) === 0) {
throw new Exception('No files found on FTP-server');
}
foreach($fileList as $filename) {
try {
$container->getObject($filename);
// Image already exists, rackspace has no convenient hasImage() function
} catch (Exception $ex) {
$temp = tmpfile();
ftp_fget($this->handle, $temp, $filename, FTP_BINARY);
//upload $tmp to rackspace
}
}
Any ideas what could be the issue here?
Related
i'm having trouble using PHP Codeigniter to SFTP upload files to a remote server.
when i upload file more than one. so here my code:
for($row = 0; $row < $total; $row++)
{
$origin = $directory.$file[$row];
$destination = $des.$file[$row];
$send= $this->upload($config,$origin,$destination);
$filename = $file[$row];
print_r($filename."\n");
} $this->sftp->close();
so if i have 1000 files to upload,and success upload from 1 until 50. but when file 51 fails to upload and get error message
ftp unable to upload
what causes can display the error above,how to create handle to reconnecting and reupload next file. so this function:
public function upload($config,$origin,$destination)
{
$connect = $this->sftp->connect($config);
$upload = $this->sftp->upload($origin, $destination, 'ascii', '0775');
//create handling upload
return $upload;
}
and sample function upload from library sftp:
$file_to_send = #file_get_contents($locpath);
$sftp = intVal($this->sftp);
$stream = fopen("ssh2.sftp://{$sftp}{$rempath}", 'w');
if (#fwrite($stream, $file_to_send) === FALSE)
{
if ($this->debug == TRUE)
{
$this->_error('ftp_unable_to_upload');
}
return FALSE;
}
return TRUE
source sftp library from library sftp
so how to handle to reconnecting and reupload file when upload get message ftp unable to upload. Thanks
you have to increase the page loading time If you are working on localhost.
once hosted no need of increasing the loading time.
I have a code:
scandir("ssh2.sftp://" . intval($sftpHandle) . $remoteDir);
Why the same code works for one server but doesn't for another?
There is a files on both servers. I can manage them via Filezilla without problems.
The first one just returns array('.') even if there is a lot of files, another one returns array('file1', 'file2', 'file3', etc)
Even if I cannot list a files using scandir(), command ssh2_scp_recv($sshHandle, $remoteDir/file1, $localDir/file1) works fine.
Also ssh2_exec($sshHandle, "ls $remoteDir") works fine to me.
Please note that I'm using $ftpHandle for scandir() but $sshHandle for ssh2_* functions.
Using $sshHandle for scandir() cause "Segmentation fault" error.
I know that I can workaround this by parsing ssh2_exec($sshHandle, "ls $remoteDir") output, but would prefer do it right way if possible.
My PHP version is 7.0.31
This should work probably
$connection = ssh2_connect($url);
// login
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect server.');
// Create SFTP resource
if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create connection.');
$localDir = '/path/to/local/dir';
$remoteDir = '/path/to/remote/dir';
// download all the files or list all
$files = scandir('ssh2.sftp://' . $sftp . $remoteDir);
if (!empty($files)) {
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
// here you can print files using $file
// or download file by uncommenting below line
//ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");
}
}
}
This is my code to send single file from local to SFTP server. Single file successfully send.
I want to send one folder from local to SFTP server , but i dont know how to send one folder. i need help
<?php
$src = 'xxxxxxx';
$filename = 'test.txt';
$dest = 'xxxxxxxx'.$filename;
// set up sftp ssh-sftp connection
$connection = ssh2_connect('xxxxxx', 22);
ssh2_auth_password($connection, 'username', 'password');
// Create SFTP session
$sftp = ssh2_sftp($connection);
$sftpStream = #fopen('ssh2.sftp://'.$sftp.$dest, 'w');
try {
if (!$sftpStream) {
throw new Exception("Could not open remote file: $dest");
}
$data_to_send = #file_get_contents($src);
if ($data_to_send === false) {
throw new Exception("Could not open local file: $src.");
}
if (#fwrite($sftpStream, $data_to_send) === false) {
throw new Exception("Could not send data from file: $src.");
} else {
//Upload was successful, post-upload actions go here...
}
fclose($sftpStream);
} catch (Exception $e) {
error_log('Exception: ' . $e->getMessage());
fclose($sftpStream);
}
?>
ssh2_scp_send in php looks like just support sending file.
maybe these ways work:
1.compress folder, send the compressed file
2.use ssh2 to mkdir, then send each file iteratively
3.try with system() function to excute cmd line order "scp -r folder_path user#host:target_path"
I can connect to a server using an FTP client and move files up and down with no issue. When I try with ftp_put it fails to upload the files. I am opening a directory on server 1 and reading the files and removing anything with any . listing, as the files are read i am displaying the files on screen to see that they are listed and then trying to upload them using ftp_put to server 2 but they are failing to upload. Can anyone see why this does not work please. The permissions on the folder on server 2 are set correctly and i am connected and have tried using pasv mode.
$conn_id = ftp_connect($ftp_server,$port);
$login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
if (!$conn_id) {
echo 'Failed to connect';
} else {
if (!$login_result) {
echo 'Failed to log in';
} else {
ftp_pasv($conn_id, true);
$path='this/path';
$dir_handle = opendir($path) or die("Error opening $path");
while ($file = readdir($dir_handle)) {
if (substr($file,0,1)=='.') {
} else {
$upload = ftp_put($conn_id, 'Testdir/FilesInThisDir/'.$file, $file, FTP_ASCII);
print (!$upload) ? 'Cannot upload '.$file : 'Upload complete';
print "<br>";
}
}
}
}
ftp_close($conn_id);
The answer turned out to be simple really. The guy did not give me the absolute path to work with :)
I want to transfer site from shared hosting server to dedicated server.current server has folder,which includes approx. 16000 images...we can not use FTP to download these much images.and i do not have SSH rights? how can i download images from this shared hosting server.
we can not use FTP to download these much images
Nonsense. FTP (the protocol) is perfectly capable of downloading 16000 files. If your FTP program is causing you trouble, simply pick a better FTP program. If you can handle commandline applications, wget is nice, since it supports recursion and continuation.
Unless they are located within a directory that is within the web root of a web application server you are out of luck.
Zip them all, adapted from http://davidwalsh.name/create-zip-php
<?php
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
//glob images folder into an array
foreach (glob("*.jpg") as $filename) {
$files_to_zip[]='images/'.$filename;
}
//create the zip
$result = create_zip($files_to_zip,'my-images.zip');
if($result==true){echo'Download Images';
}else{
echo'Could not create zip';}
?>