I am trying so fetch a file from another server into my server using the ftp in php.
There were two steps. The first step is to send the file from my server which I have done successfully.
Now I need to fetch a file from the warehouse ftp server. I connect to it successfully as well as get the list of files from there too. But I do not know how can i fetch it. For sending here is the code. I tried switch between MY SERVER and The Warehouse server but it does not work and keeps on saying the file doesnt exist where as file is there.
Any ideas?
Here is the code for sending the file
$ftp_server = SERVER_IP;
$ftp_user_name = FTP_USER;
$ftp_user_pass = FTP_PASS;
$ftp_dir = "public_html/inbound/";
$mode = "list_xml_files";
$file = "";
if ($file_list = ftp_list_xml_files ($ftp_server,$ftp_user_name,$ftp_user_pass,$ftp_dir,$mode,$file)) {
$ftp_server = WAREHOUE_SERVER;
$ftp_user_name = WAREHOUSE_FTP_USER;
$ftp_user_pass = WAREHOUSE_FTP_PASS;
$ftp_dir = FILE_DIRECTORY;
$mode = "ftp_to_warehouse";
foreach ($file_list as $v) {
if ($file_uploaded = ftp_list_xml_files ($ftp_server,$ftp_user_name,$ftp_user_pass,$ftp_dir,$mode,$v)) {
echo "FILE SENT";
}
else
{
echo "ERROR";
}
function ftp_list_xml_files ($ftp_server,$ftp_user_name,$ftp_user_pass,$ftp_dir,$mode,$file) {
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!<br>";
echo "Attempted to connect to $ftp_server for user $ftp_user_name<br>";
exit;
} else {
#echo "Connected to $ftp_server, for user $ftp_user_name";
}
switch ($mode) {
case "list_xml_files":
$contents = array();
ftp_chdir($conn_id, $ftp_dir); // Change Directory
// get .xml files in the current directory
$contents = ftp_nlist($conn_id, "*.xml");
/////////////////////////////////////////////////////////////// What if there's no files to process?
if (count($contents) > 0) {
// output $contents
// print_r($contents);
// exit;
return $contents;
} else {
return FALSE;
}
break;
case "ftp_to_warehouse":
// upload the file
$source_file = 'tracking_number_xmls/pending/' . $file;
ftp_chdir($conn_id, $ftp_dir); // Change Directory
$destination_file = $file;
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_TEXT); // FTP_BINARY
// check upload status
if (!$upload) {
echo "FTP upload has failed!<br>";
return FALSE;
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file<br>";
return TRUE;
}
break;
default:
echo "Nothing Done<br>";
}
// close the FTP stream
ftp_close($conn_id);
} // end function
Related
I wrote this code for upload file via ftp.
<?php
$file = 'index.php';
$fp = fopen($file, 'r');
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Initate the upload
$ret = ftp_nb_fput($conn_id, $file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Do whatever you want
echo ".";
// Continue upload...
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "There was an error uploading the file...";
exit(1);
}
fclose($fp);
?>
and I get this error:
Warning: ftp_nb_fput(): Could not open data connection to port 2804: Connection refused
I disable my firewall but not work!
Try running in pasv mode, ftp_pasv($conn_id, true); Also please use ftp_close($conn_id) when you're done.
Thanks Ohgodwhy!
I have to download files from linkshare server to my server by using cron.
Every thing is perfect if the file size is less then 2 GB but if exceeds it fails to download.
code is given below
$ftp_server = "***.*******.com";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, '******', '*******');
$ret = ftp_nb_get($conn_id, $localfile, $serverfile, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Do whatever you want
echo ".";
// Continue downloading...
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "There was an error downloading the file...";
exit(1);
}
Thanks in advance
This is my code for get all files from link share ftp
<?php
session_start();
$i = $_REQUEST['i'];
if($i==""){
$i=0;
$source_dir=("linkshare");
$source_folder=dir($source_dir);
while($files_list=$source_folder->read())
{
if ($files_list!= "." && $files_list!= "..")
{
$pat="linkshare/";
unlink($pat.$files_list);
}
if($files_list!="")
{
$pat="linkshare/";
unlink($pat.$files_list);
}
}
}
$destinationnameeeeee = "linkshare/";
ini_set("max_execution_time",300000000000000000);
$ftp_server = ''; //ftp server name
$ftp_user_name = ''; //ftp user name
$ftp_user_pass = ''; //ftp user password
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$source_folder = ftp_nlist($conn_id, ".");
foreach($source_folder as $folder_list)
{
$folderlisting = explode("_",$folder_list);
$folders_list[]= $folder_list;
}
$_SESSION['folder_list'] = $folders_list;
//print_r($folders_list);
$folder_count=count($_SESSION['folder_list']);
$cur_folder = $_SESSION['folder_list'][$i];
$source_file = str_replace('.lmp', '', $_SESSION['folder_list'][$i]);
$destination_file = $destinationnameeeeee.str_replace('.lmp', '', $_SESSION['folder_list'][$i]);
echo $destination_file;
if ((!$conn_id) || (!$login_result))
{
echo "<br />FTP connection has failed!";
echo "<br />Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
}
else
{
echo "<br />Connected to $ftp_server, for user $ftp_user_name";
}
// download the file
$download = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
// check download status
if (!$download)
{
echo "<br />FTP download has failed!";
}
else
{
echo "<br />Downloaded $source_file from $ftp_server as $destination_file";
//if($i<=$folder_count)
if($i>=0)
{
$i=$i+1;
}
if($i==8)
{
exit; // 8 file only now download if you want to extent yourself
}
header("Location:ftpget.php?i=$i");
}
ftp_close($conn_id);
?>
I can load same file for after every file downloading time.
tryfollowing this link, it could help you
runtime behavior for the FTP connection
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 .
How can I save an image over FTP? Here's my code:
$conn_id = ftp_connect('***');
$login_result = ftp_login($conn_id, '***','***');
if( $login_result) echo 'connected';
$save = "FTP://temp/". $FileName;
imagepng($this->Picture, $save);
/*if (ftp_put($conn_id,$save,$save, FTP_ASCII))
echo "successfully uploaded \n";
else
echo "There was a problem while uploading \n";
*/
You could do:
ob_start();
imagepng($this->Picture);
$image = ob_get_clean();
$stream = stream_context_create(array('ftp' => array('overwrite' => true)));
file_put_contents("ftp://user:pass#host/folder/".$FileName, $image, 0, $stream);
Hope this helps you
<?
//Configuration
$ftp_server = "ftpaddress";
$ftp_user = "username";
$ftp_pass = "password";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
//trying to connect with login details
if (#ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected! ";
} else {
echo "Couldn't connect!";
}
//You can change it with the file name, this is for if you're upload using form.
$myName = $_POST['name']; //This will copy the text into a variable
$myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored.
?>
<?PHP
$destination_path = "dest/path/";
//your desired path for uploading)
$destination_file = $destination_path."img.jpg";
//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.
$file = $myFile['tmp_name'];
//Converts the array into a new string containing the path name on the server where your file is.
$upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
if (!$upload) {// check upload status
echo "FTP upload of $destination_file has failed!";
} else {
echo "Uploaded $file to $conn_id as $destination_file";
}
?>
I have a PHP script which I use to rename a file on the FTP server root.
I need to change this to rename the file in a directory on the FTP root.
current working
rename file in root to despgoods.csv
desired working
rename file in root/despgoods/despgoods.csv
My PHP script is:
$ftp_server = "ftp.ftpserver.co.za";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "username";
$ftp_user_pass = "password";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$targetname = "DespGoods.csv";
$contents = ftp_nlist($conn_id, '');
if (!$contents) {
echo 'No Files Present on Server: <br/> ';
echo mysql_error();
die;
}
$filename = $contents[0];
if ($filename == "despGoods.csv") {
echo 'DespGoods already exists, no need to rename: <br/> ';
echo mysql_error();
} else {
ftp_rename($conn_id, $filename, $targetname);
ftp_close($conn_id);
echo "1 available file renamed to DespGoods.csv <br>";
}
Thanks in advance, I appreciate the help as always,
Regards,
Ryan
http://php.net/manual/en/function.ftp-chdir.php
or
$contents = ftp_nlist($conn_id, 'despgoods');
$filename = $contents[0];
if ($filename == "despGoods.csv") {
echo 'DespGoods already exists, no need to rename: <br/> ';
} else {
ftp_rename($conn_id, 'despgoods/'.$filename, 'despgoods/'.$targetname);
echo "1 available file renamed to DespGoods.csv <br>";
}
ftp_close($conn_id);
P.S. What mysql_error() doing in your code?
Use this
$ftp_server = "ftp.ftpserver.co.za";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "username";
$ftp_user_pass = "password";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$targetdir= "despgoods"; //target directory under which file is to be modified
$targetname = "DespGoods.csv";
ftp_chdir($conn_id, $targetdir);
$contents = ftp_nlist($conn_id, '');
if (!$contents) {
echo 'No Files Present on Server: <br/> ';
echo mysql_error();
die;
}
$filename = $contents[0];
if ($filename == "despGoods.csv") {
echo 'DespGoods already exists, no need to rename: <br/> ';
echo mysql_error();
} else {
ftp_rename($conn_id, $filename, $targetname);
ftp_close($conn_id);
echo "1 available file renamed to DespGoods.csv <br>";
}
Also check http://www.php.net/manual/en/function.ftp-chdir.php