I am currently uploading files to a third party server through SFTP by using PHP ssh2
The connection are working fine and I tried to upload files using fwrite
$resourceId = $this->sftp;
$intResourceId = intval($resourceId);
$dir = "ssh2.sftp://$intResourceId/./".$this->un."/".$file_name;
$stream = #fopen($dir, 'w');
if (!$stream) {
return "Could not open file: $file_name";
}
$data_to_send = #file_get_contents(LOCAL_FILE_DIR.$file_name,true);
if ($data_to_send === false){
return "Could not open local file: $file_name.";
}
if (#fwrite($stream, $data_to_send) === false){
return "Could not send data from file: $file_name.";
}
/*
readdir():
Array
(
[0] => 1810171932.txt
[1] => response_dir
)
*/
The files were uploaded when I read the directory but after a few seconds it was removed from the directory.
/*
readdir():
Array
(
[0] => response_dir
)
*/
Any idea why? Does the file being uploaded or there are some permission issue which I need to take care of?
Thanks in advance.
Related
I am trying to download two files from two different FTP servers. One file is 10 MB in size while the other is 3.3GB in size. The 10MB file is downloaded every time without a problem. The 3.3GB file always encounters an error:
Code Error: [2] ftp_get(): Opening BINARY mode data connection for
bigfile.gz (3232089332 bytes).Error/Warning on line 55 in file
script.phpPlease go over the collector code.
The size of the file is exactly 3232089332, so this issue appears after the file has finished downloading completely.
Both files are .gz files (so I know they are binary).
There is enough space on the hard drive (currently free 47GB).
It is worthwhile to note that I am able to download the file without any issues using Filezilla.
Any help would be highly appreciated.
The code is as follows:
function ftpDownload($server, $username, $password, $filename) {
if (strpos($server, '://') !== false) $server = substr($server, strpos($server, '://') + 3);
# set up basic connection
echo "Connecting to $server\n";
$connectionId = ftp_connect($server);
if (!$connectionId) {
return ['success' => false, 'error' => "FTP Connection has Failed"];
}
# login with username and password
echo "Logging in\n";
$loginResult = ftp_login($connectionId, $username, $password);
ftp_pasv($connectionId, true);
# check connection
if (!$loginResult) {
return ['success' => false, 'error' => "Failed to login"];
}
# Verify the file exists
echo "Locating $filename\n";
$result = ftp_size($connectionId, $filename);
if ($result == -1) {
return ['success' => false, 'error' => "Unable to locate $filename in server", 'filename' => false];
}
echo "File size ".number_format(($result / 1024 / 1024))." MBs\n";
# Download the file
echo "Downloading $filename, this may take a while\n";
if (file_exists(__DIR__.'/files/'.$filename)) unlink(__DIR__.'/files/'.$filename);
ftp_pasv($connectionId, true);
$success = ftp_get($connectionId, __DIR__.'/files/'.$filename, $filename, FTP_BINARY);
ftp_close($connectionId);
if ($success == false) {
return ['success' => false, 'error' => "Failed to download file $filename from server, received error from FTP"];
}
if (!file_exists(__DIR__.'/files/'.$filename)) {
return ['success' => false, 'error' => "Unable to locate $filename in server, filename was not properly stored locally"];
}
}
Reached a solution. As the timeout occurred after the file has been fully downloaded, instead of using ftp_get() I used the following:
$fp = fopen($filename, 'w');
#$success = ftp_fget($connectionId, $fp, $filename, FTP_BINARY);
Then to verify check whether the transfer was successful, I just compared the downloaded file's filesize using filesize() with the remote file's filesize which I've obtained beforehand (can be seen in the question's code).
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 am trying to Save Remote Image to local server using Phils Curl srcipt in Codeigniter.
To ensure directory is there with proper permissions I have:
if (!is_dir('images')){
mkdir('./images', 0777, true);
}
Here is what I tried.. ( Source )
$this->load->library('curl');
$this->load->helper('file');
$remoteURL = 'http://fermeteogranada.com/camara.jpg';
$img = $this->curl->simple_get($remoteURL);
$localURL = './images/main.jpg';
if ( ! write_file($localURL, $img)){
$this->session->set_flashdata('err_message', 'Unable to write the file');
}
It is creating a file named as main.jpg but with 0 bytes.
I also tried to use copy and file_get_content
if ( ! copy($localURL, $img)){
$error = get_last_error();
$this->session->set_flashdata('err_message', $error['message']);
}
if ( ! file_put_content($localURL, file_get_content($img) )){
$error = get_last_error();
$this->session->set_flashdata('err_message', $error['message']);
}
But It is also not working as well, It is not giving any error message.
I would first debug $img data verify it has a bunch of binary junk in it. You may also want to check the http status. Perhaps the website is denying remote curls by user agent.
What about doing this ?
$remoteFile = 'http://fermeteogranada.com/camara.jpg';
$remoteResource = file_get_contents($remoteFile);
$saveTo = './images/main.jpg';
file_put_contents($saveTo, $remoteResource);
I tried in local and it works just fine.
$files is an array to store file names.
createZipFile function is to make zip file. addFiles function returns true as Boolean value.
I am not sure where I am going wrong with the code.
<?php
$files[] = 'test.php';
$files[] = 'test1.php';
$files[] = 'test2.php';
createZipFile ( 'backup.zip', $files);
function createZipFile($pathtosave, $filestozip) {
// create zip file
$zip = new ZipArchive ();
if ($zip->open ( $pathtosave, ZipArchive::CREATE ) != true)
{
throw new Exception( 'Cannot create or open the zip file' );
}
else
{
foreach ( $filestozip as $zipfiles )
{
$zip->addFile($zipfiles);
}
}
// save it
$zip->close();
}
?>
The code works correctly from the command line. This is possibly a permission issue. If you are executing this through a web server, make sure the user account that the web server runs under has write permissions to the directory in which you wish to create the zip file. In this case it's the same directory as the script. You can also set global write permissions with:
chmod 777 {web directory}
But this is a security risk. It will allow any user account on the system to modify, create or delete files in the directory.
I am attempting to transfer a zip file via ftp from one server to mine, so that I can use the data in the file to update my database table. Here is my ftp.php file:
<?php
header('Content-type: text/html; charset=utf-8');
$date = "2013-05-21-11-19-40";
$ftp_server="ftp.server.com";
$ftp_user_name="BookCellar";
$ftp_user_pass="*****";
$file = "/reports/other/compreport_abebooks_" .$date. ".zip";//tobe uploaded
$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip";
?>
and my ftpUpload.php file is:
<?php
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Content-Type: application/zip');
require_once('ftp.php');
// set up basic connection
$conn_id = ftp_ssl_connect($ftp_server);//ftp_connect
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
exit;
} else {
echo "Connected for user $ftp_user_name" ;
}
ftp_chdir($conn_id, '/home/bookcell/bookcellaronline.com/html/testbcos/accounting/');
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
echo $php_errormsg;
// close the connection
ftp_close($conn_id);
?>
When I run these I get the error messge:
[<a href='function.ftp-put'>function.ftp-put</a>]: failed to open stream: No such file or directory in /chroot/home/bookcell/bookcellaronline.com/html/testbcos/accounting/ftpUpload.php on line 25
Line 25 is:
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
I have researched a bunch of other posts on SO and have not found a solution. My connection works, but I can't get the file to transfer.
How do I (if even possible) get this to transfer to my server?
EDIT: Am I missing the fact that I need to connect not only to their server($file) AND my server ($remote_file)????
You can't put a path to the destination file
$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip";
e.g. - this doesn't work
ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);
you have to put
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
A FTP server hides his absolute path /home/bookcell/bookcellaronline.com/html/
All folders must be relative to the root
ftp_chdir($conn_id, '/testbcos/accounting/');
test the result of ftp_chdir ! Are your in the right directory ?
echo ftp_pwd($conn_id);
Try to connect to the FTP server via a browser.
ftp://BookCellar#ftp.server.com
What you get is / the root. Folders and files that you see in the browser, are below the root directory.
Update : A working out of the box example.
the $password = line must be replaced with Download pass
ftp.php
<?php
$password = "????";
$resource = ftp_connect('ftp.strato.com');
$login = ftp_login($resource, 'ftp_mx_all#moskito-x.de', $password);
$list = ftp_rawlist($resource, '/');
print_r($list);
?>
You will get with print_r
Array ( [0] => drwxr-xr-x 2 ftp ftp 4096 May 23 20:15 aFolder [1] => -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt )
we can see there is a folder aFolder and a file tutorial.txt.
We are interest what files are in the folder aFolder ?
So replace the $list = with
$list = ftp_rawlist($resource, '/aFolder');
and run the php script again. The output :
Array ( [0] => drwxr-xr-x 3 ftp ftp 4096 May 23 19:24 .. [1] => -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt [2] => -rw-r--r-- 1 ftp ftp 271 May 23 21:16 tutorial.zip )
Now we want to download aFolder/tutorial.txt .
Add following below print_r($list); .
echo "<br />\n";
$local_file = "tmp.txt" ;
$file = ftp_get($resource, $local_file, '/aFolder/tutorial.txt',FTP_ASCII);
if ($file) {
echo "$local_file has been successfully written\n";
} else {
echo "An error has occurred\n";
}
The Output :
The folder where the php script is has changed.
now there is a new file tmp.txt !
If you bring this little script to run. We can go further.
From our chat :
Your server does not allow a ftp call to a url.
look allow_url_fopen = ON
echo ini_get('allow_url_fopen');
if (!ini_get('allow_url_fopen')) {
ini_set('allow_url_fopen', 1);
}
echo ini_get('allow_url_fopen');
Then try it again.
set_time_limit(0);
exec("wget --continue http://example.com/site.zip");
exit;
In the first excerpt change the remote tile to be
`$remote_file = "compreport_abebooks_" .$date. ".zip";`
prior to the put you are changing into the directory.
Also note that the directory that you reference in the ftp_chdir call differs from the one in the $remote_file referenced at the top.
/home/bookcell/bookcellaronline.com/html/testbcos/accounting/
vs
/chroot/home/bookcellaronline.com/html/testbcos/accounting/