ftp_get() corrupt large file download - php

I have to download a large file(approx~-9mb) in .gz format from the server using ftp. I have written a function which downloads the file completely and correctly but when i put this code online , the file is downloaded but the file gets corrupt.
Here is my code:
function downloadFile($ftp_server, $username, $password, $server_file, $local_file)
{
// download server file
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $username, $password);
ftp_pasv($ftp_conn, true);
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file.";
// exit;
} else {
echo "Error downloading $server_file.";
}
}
Here is how i call it
ini_set('max_execution_time', 0);
$username = "username";
$password = "password";
$xmlFile = __DIR__ . "/monster.xml.gz";
$local_file = __DIR__ . "/monster.xml";
$ftp_server = "ftp.monster.com";
$server_file = "/US~Partner~Sample Feed.xml.gz";
downloadFile($ftp_server,$username,$password,$server_file,$xmlFile);
The file must be downloaded and the then I will convert it into XML for further processing.
Note: The file is downloading perfect on localhost , this problem occurs as we put it on live server.

GZ file may not only have text in it . So you have to download it with binary mode so that anything containing in it could be download successfully
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file.";
// exit;
} else {
echo "Error downloading $server_file.";
}

Related

Ho to use ftp_put for file upload in PHP?

I try to upload a file using ftp_put after submitting a form. However my code does not work as expected:
$ftpHost = '192.168.180.36';
$ftpUsername = 'userdownload';
$ftpPassword = 'Toms!';
$filepath = "C://xampp/htdocs/Helpdesk/fmt/download";
$connId = ftp_connect($ftpHost,21) or die("Couldn't connect to $ftpHost");
$ftpLogin = ftp_login($connId, $ftpUsername, $ftpPassword);
$file_name = $_FILES['file']['name'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
// try to upload file
if(ftp_put($connId, $file_name, $filepath.'/'.$file_name, FTP_BINARY)){
echo "File transfer successful - $file_name";
}else{
echo "There was an error while uploading $file_name";
}
And I get this error message:
There was an error while uploading $file_name
There are two thing i noticed in your code. the first one is the mode of transfer depends upon your file type.Files that are in the ascii/text file extension list are transferred as ascii, all other files are transferred as binary.The second one is may be there is a problem in your file which you received may be it is black first check that. You can try this code it is working for me.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>

ftp_get fetch file on remote server failed to open stream: No such file or directory

<?php
$local_file = 'filename';
$remote_file = '/folder name/filename';
$ftp_server ='IP';
$ftp_user_name = 'NAME';
$ftp_user_pass = 'PW';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($conn_id, true);
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
// close the connection
ftp_close($conn_id);
?>
I'm using the above script successfully on a different server with file located in the root folder. But on this one I need to fetch a file that's in a sub-directory (let's say 'folder name' for now). I've tried different things but all throw an error "failed to open stream: No such file or directory".
I've tried:
$remote_file = '/folder name/filename.csv';
$remote_file = './folder name/filename.csv';
$remote_file = '../folder name/filename.csv';
$remote_file = '/../folder name/filename.csv';
It's my first time dealing with ftp_get command. Can someone please help I'm badly stuck... don't know what I'm doing wrong here
For those who might run into the same issue, I've sorted it out by added the following lines of code (using the function ftp_chdir) and it worked fine:
// turn passive mode on
ftp_pasv($conn_id, true);
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// try to change the directory to somedir
if (ftp_chdir($conn_id, "Live Stock")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}

change ftp_get download directory

I am using the below code to download using ftp
$local_file = $_GET['u'];
$server_file = $_GET['u'];
$ftp_host = '';
$ftp_user_name = '';
$ftp_user_pass = '';
$ftp_conn = ftp_connect($ftp_host) or die("Could not connect to $ftp_host");
$login = ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp_conn,true);
ftp_chdir($ftp_conn, '');
$file_list = ftp_nlist($ftp_conn, ".");
//var_dump($file_list);
//var_dump(ftp_get($conn_id, $local_file, $server_file, FTP_BINARY));
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
am getting successfully written to filename but i cant seem to find the file on my computer. Do you if i can change where the file is being downloaded to.
Pass an absolute path (instead of "myfile.txt" try "/home/user/path/to/yourfile.txt")

php upload local file to ftp server

i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size!
also i tried to enable passive mode but it still uploads an empty file.
Try enabling track_errors and access $php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);
I had the same issue. When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected.
I had ran into this as well. Bit late but thought I'd post my solution:
$file_name = "localfile.txt";
Get content from your existing file
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content
$content = "This is content";
upload file
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);

Upload Multiple files to remote FTP Server using php

I need to use php to upload to an ftp server 4 files. I have the following example code, that I am working from. How could this code be changed to upload multiple files that were already on the server (not uploaded at the time of the ftp transfer).
Lets say I have 4 files in a subfolder relative to the php file that does the upload, lets call the subfolder “/fileshere/” with the following 4 files in it:
file1.zip
file2.zip
file3.zip
file4.zip
I need the script to upload each of the files, then give a done message.
Below is the starting code I am using and trying to adapt. Any help would be much appreciated:
$ftp_server = "ftp.yourserver.com";
$ftp_user_name = "ftpuser";
$ftp_user_pass = "ftppassword";
$remote_dir = "/target/folder/on/ftp/server";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = #ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
//default values
$file_url = "";
if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);
//check if directory exists and if not then create it
if(!#ftp_chdir($conn_id, $remote_dir)) {
//create diectory
ftp_mkdir($conn_id, $remote_dir);
//change directory
ftp_chdir($conn_id, $remote_dir);
}
$file = $_FILES["file"]["tmp_name"];
$remote_file = $_FILES["file"]["name"];
$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}
if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
Try this code
<?php
// connect and login data
$web = 'www.website.com';
$user = 'admin';
$pass = 'password';
// file location
$server_file = '/public_html/example.txt';
$local_file = 'example.txt';
//connect
$conn_id = ftp_connect($web);
$login_result = ftp_login($conn_id,$user,$pass);
//uploading
if (ftp_put($conn_id, $server_file, $local_file, FTP_BINARY))
{echo "Success";}
else {echo "Failed";}
?>
if you want to upload multiple files just put your files names into array then put whole code into for loop .

Categories