can't download file from ftp with ftp_get (no error) - php

I'm trying to download file from ftp, but whatever i tried i couldn't download any file from ftp and it is not giving any error.
My code fetching file names from database and compares these file names with existing ftp file(names). If file name exist on ftp i want to download this file to my local-destination folder.
I can read ftp file names but i can't download them. Hope you can help me.
Here is my code
error_reporting(E_ALL);
$ftp_server = "xx.xx.y12.34"; //server
$bt = ftp_connect($ftp_server); // connect to ftp
$username = "myusername"; $pass = "mypass"; //username and password
$connection = ftp_login($bt, $username, $pass); // login ftp with username and password
ftp_pasv($bt, true);
if ((!$bt) || (!$connection)) {
echo "failed";
exit;
}else {
$path = "/1_Swatch Files/LG Swatches/";
$destination_folder = "/home/faruk/lasenza/media/color_samples/";
// mysql connection codes..
$results = $readConnection->fetchAll($query); // fetch file names from database
$contents_on_server = ftp_nlist($bt, $path);
foreach($results as $result){
$check_file_exist = $result["CLR_CODE"].".gif";
if(in_array($check_file_exist, $contents_on_server)) {
$server_file = $result["CLR_CODE"].".gif";
if (ftp_get($bt, $destination_folder , $server_file, FTP_BINARY)) {
echo 'Succeeded';
}else{
echo "There was a problem";
}
break;
}
}
}

Related

How to upload files from database on FTP remote server? [duplicate]

This question already has answers here:
PHP ftp_put fails
(2 answers)
Closed 1 year ago.
I use PHP to upload images from a host into FTP remote server.
I have already stored the names of the files that need to be uploaded to the FTP remote server, stored on the database.
After connecting to DB and a query for select information of images, I passed variables with $_SESSION to another PHP file for upload them on FTP remote server like this:
conn_auth.php for select target images:
<?php
if (file_exists(__DIR__.'/conn_auth/db_conn.php')){
include __DIR__.'/conn_auth/db_conn.php';
}else{
die('Connection is Failed ...!');
}
$conn = OpenConn();
$sql = "SELECT * FROM users where status = 'Active'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id = $row["id"];
//search for user_id in userAthentication Table in DB
$sql = "SELECT * FROM userauthentication where user_id = $user_id";
$result = $conn->query($sql);
$user_auth = $result->fetch_assoc();
$user = array(
'id'=>$row['id'],
'name'=>$row['name'],
'family'=>$row['family'],
'auth_image'=>'',
'file_name'=>$user_auth["image"],
'phone'=>$row["phone"]
);
$url = "/home/root/app_name_1/uploads/".$user["file_name"];
if(!file_exists($url)){
$url = "/home/root/app_name_2/uploads/".$user["file_name"];
}
$user['auth_image'] = $url;
session_start();
$_SESSION["userInfo"] = $user;
$_SESSION["userAuth"] = $user_auth;
if (file_exists(__DIR__.'/conn_auth/get_auth.php')){
include __DIR__.'/conn_auth/get_auth.php';
}else{
die("I can't Connect to the Server");
}
}
} else {
echo "0 results";
}
CloseConn($conn);
On this file, I try to connect to FTP remote server and upload images with the ftp_put() method, but it does not work:
<?php
include_once __DIR__.'../conn_auth.php';
session_start();
$ftp_server="*.***.***.**";
$ftp_user_name="****";
$ftp_user_pass="**********";
$file = $_SESSION["userInfo"]['auth_image']; //to be uploaded
$folder_name = $_SESSION["userInfo"]['phone'];
$file_name = $_SESSION["userInfo"]["file_name"];
$remote_file = "/HDD/pics/$folder_name";
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_mkdir($conn_id,'auth_pics').ftp_chdir($conn_id,'auth_pics');
if (ftp_mkdir($conn_id,"$folder_name")){
ftp_chdir($conn_id,$folder_name);
}else{
echo "I Can't Create Folder For : ".$folder_name;
die();
}
// upload a file
$upload = ftp_put($conn_id, $remote_file, $file, FTP_BINARY);
if ($upload) {
echo "successfully uploaded : $file\n";
} else {
echo "There was a problem while uploading...\n";
}
// close the connection
ftp_close($conn_id);
?>
Mostly your FTP server doesn't support active mode connections, so you may try to switch to the passive mode using the ftp_pasv, but make sure to do this after you logged into the server (after ftp_login).
If you face some troubles while using the native API functions, you may want to try out an FTP client library, like this, that I've built.
Hope you find this answer useful for you.

how to get file from ftp server and copy on own server

I want to get file from client server and copy them on my server , I have successfully connected to client server, my code is below.
// connect and login to FTP server
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxx';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "<pre>";
print_r($login);
echo "</pre>";
// get the file list for /
$filelist = ftp_rawlist($ftp_conn, "/");
// close connection
ftp_close($ftp_conn);
echo "<pre>";
print_r($filelist);
echo "</pre>";
// output $filelist
var_dump($filelist);
May anyone please advise how can I achieve this?
You can use the ftp_fget function specified here: http://php.net/manual/en/function.ftp-fget.php
(ftp_fget() retrieves remote_file from the FTP server, and writes it to the given file pointer.)
Here an example provided by the documentation:
<?php
// path to remote file
$remote_file = 'somefile.txt';
$local_file = 'localfile.txt';
// open some file to write to
$handle = fopen($local_file, 'w');
// 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);
// try to download $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
echo "successfully written to $local_file\n";
} else {
echo "There was a problem while downloading $remote_file to $local_file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>
This is how I resolved this now all files will copy on your server. use ftp_ssl_connect if its secure
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxxxxx';
$ftp_conn = ftp_ssl_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn,pasv);
$output_directory="files1/ftpgetfiles/137/";
// get the file list for /
$filelist = ftp_nlist($ftp_conn, "/");
foreach ($filelist as $key => $value) {
$fp = fopen($output_directory.$value,"w");
if(ftp_fget($ftp_conn, $fp, $value, FTP_BINARY))
{
fclose($fp);
}
}
ftp_close($ftp_conn);

Pureftpd-upload while uploading image through ftp

I am trying to upload image from one server to another server through FTP by using PHP.But the uploaded data shows.pureftpdupload.5809ed2f.15.7b36.24316ca6 error.
I am using this code.
$connection = 'servername';
$username = 'xxxx';
$password = 'yyyy';
$local_file = 'http://servername/test.jpg';
$remote_file = 'admin/files/company/test.jpg';
$connection = ftp_connect($server);
if (#ftp_login($connection, $username, $password)) {
// successfully connected
//echo 'connected';exit;
} else {
echo 'not connected';
return false;
}
if (ftp_put($connection, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded\n";
} else {
echo "There was a problem while uploading \n";
}
My guess is that you want to upload a local file from a web server. But by mistake or misunderstanding, you are using file's HTTP URL (http://servername/test.jpg) instead of file's local path.
Try to use a real local path, like /home/user/test.jpg.
This may work as a generic solution: $_SERVER['DOCUMENT_ROOT']."/test.jpg"

how to list all the directories of ftp server of iis using php for download

I set up my IIS Server in Windows and set a list of folders that should only be downloaded. Now i connected to the server using php too.
$username=$_POST["username"];
$password=$_POST["paswd"];
$host="localhost";
$ftpcon= ftp_connect($host) or die("could not connect");
$login=ftp_login($ftpcon,$username,$password);
Now what i want to list is the windows directory. That will help me navigate like the file explorer in windows. Would you mind giving me some help here
Try the below code to list all your files & directories:
<?php
$ftp_server = 'xxx.xx.xx.xx';//Replace with your IP
$conn_id = ftp_connect($ftp_server);
# login with username and password
$user = 'ftp_user_name'; //Replace with your FTP User name
$passwd = 'ftp_password'; //Replace with your FTP Password
$login_result = ftp_login($conn_id, $user, $passwd);
# check connection
if (!$conn_id)
{
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $user";
exit();
}
else
{
$cwd = ftp_pwd($conn_id);
$contentList = ftp_nlist($conn_id, "."); // This denotes the current path, you can replace this with your actual path
foreach($contentList as $contentListItem)
{
if (ftp_size($conn_id, $contentListItem) == -1)
{
#Its a direcotry
echo "Directory : ".$contentListItem."<br>";
}
else
{
#Its a file
echo "File : ".$contentListItem."<br>";
}
}
}
?>

PHP script to copy a file from 1 FTP to another FTP

I would like to copy a file (eg. MYFILE.csv) from FTP 'SOURCE' to FTP 'TARGET'. What type of command would you recommend for a script in php?
I have tried this but it didn't work.
<?php
$server = 'ftp.TARGET.com' ;//address of ftp server
$user_name = 'USER_TARGET'; // Username
$password = 'PASSWORD_TARGET'; // Password
$source = 'MYFILE.csv';
$dest = '/in/MYFILE.csv';
$mode='FTP_ASCII';
// set up basic connection
$connection = ftp_connect($server) ;
// login with username and password
$login_result = ftp_login($server, $user_name, $password);
// upload a file
if (ftp_put($connection, $dest, $source, $mode)) {
echo "successfully uploaded $source\n";
} else {
echo "There was a problem while uploading $source\n";
}
// close the connection
ftp_close($connection);
?>
=> The php script will be hosted in a folder on FTP A.
Thank you for your guidance.
You can use file_put_contents
http://php.net/manual/en/function.file-put-contents.php
file_put_contents('ftp://user:pass#server/path/to/file.txt', $data);
It will return false on a failure:
if(file_put_contents('ftp://user:pass#server/path/to/file.txt', $data)) {
// ftp upload successful
} else {
// ftp upload failed
}
Why does your current method fail though?

Categories