Help moving a .zip file to an ftp server please?

Try this…


<?php
function ftp_upload_file($host, $user, $pass, $file, $mode = FTP_BINARY, $port = 21){
  
  if(false === is_readable($file)){
    trigger_error(sprintf('FTP: %s cannot be opened', $file), E_USER_NOTICE);
    return false;
  }
  
  if(FTP_BINARY !== $mode){
    $mode = FTP_ASCII;
  }
  
  $conn = ftp_connect($host, $port);
  
  if(false === is_resource($conn)){
    trigger_error(sprintf('FTP: Cannot connect to %s:%s', $host, $port), E_USER_NOTICE);
    return false;
  }
  
  if(false === ftp_login($conn, $user, $pass){
    trigger_error('FTP: Cannot login with supplied credentials', E_USER_NOTICE);
    return false;
  }
  
  $status = ftp_put($conn, basename($file), $file, $mode);
  
  ftp_close($conn);
  
  return $status;
}


if(ftp_upload_file('example.org', 'username', 'password', 'path/to/local/file.txt'){
  #yay
}else{
  #boo
}

Sorry obviously being a bit thick with this. I’ve put the code you posted into 1 file uploaded it and now I’m getting a syntax error!! What am I doing wrong?



$host="ftp.site.co.uk";
$user="myusername";
$pass="mypassword";
$file="myfile.csv";

function ftp_upload_file($host, $user, $pass, $file, $mode = FTP_BINARY, $port = 21){
  
  if(false === is_readable($file)){
    error_log(sprintf('FTP: %s cannot be opened', $file), E_USER_NOTICE);
    return false;
  }
  
  if(FTP_BINARY !== $mode){
    $mode = FTP_ASCII;
  }
  
  $conn = ftp_connect($host, $port);
  
  if(false === is_resource($conn)){
    error_log(sprintf('FTP: Cannot connect to %s:%s', $host, $port), E_USER_NOTICE);
    return false;
  }
  
  $status = ftp_put($conn, basename($file), $file, $mode);
  
  ftp_close($conn);
  
  return $status;
}

if(ftp_upload_file('ftp.site.co.uk', 'myusername', 'mypassword', '/home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.csv'){
  #yay
  echo"Cool";
}else{
  #boo
  echo"Not Cool";
} 


Try this, I fixed my rather sloppy coding. :wink:


<?php
function ftp_upload_file($host, $user, $pass, $file, $mode = FTP_BINARY, $port = 21){
  
  if(false === is_readable($file)){
    trigger_error(sprintf('FTP: %s cannot be opened', $file), E_USER_NOTICE);
    return false;
  }
  
  if(FTP_BINARY !== $mode){
    $mode = FTP_ASCII;
  }
  
  $conn = ftp_connect($host, $port);
  
  if(false === is_resource($conn)){
    trigger_error(sprintf('FTP: Cannot connect to %s:%s', $host, $port), E_USER_NOTICE);
    return false;
  }
  
  if(false === ftp_login($conn, $user, $pass){
    trigger_error('FTP: Cannot login with supplied credentials', E_USER_NOTICE);
    return false;
  }
  
  $status = ftp_put($conn, basename($file), $file, $mode);
  
  ftp_close($conn);
  
  return $status;
}

if(ftp_upload_file('ftp.site.co.uk', 'myusername', 'mypassword', '/home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.csv')){
  echo 'Cool';
}else{
  echo 'Not cool';
}

According to another user who posted a note on php.net (strangely with identical code to yours also trying to upload a .csv file) it was the mode indicator.

As I understood it ASCII mode was purely for text files. Can anyone confirm?

I’m now getting the following error:

Warning: ftp_put() [function.ftp-put]: Ok to send data. in /home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.php on line 30
Not cool

Here’s the full code I have now:



<?php
$host='ftp.address.co.uk';
$user='myuser';
$pass='mypass';
$file='/home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.csv';

function ftp_upload_file($host, $user, $pass, $file, $mode=FTP_BINARY, $port= 21){
  
  if(false === is_readable($file)){
    trigger_error(sprintf('FTP: %s cannot be opened', $file), E_USER_NOTICE);
    return false;
  }
  
  if(FTP_BINARY !== $mode){
    $mode = FTP_ASCII;
  }
  
  $conn = ftp_connect($host, $port);
  
  if(false === is_resource($conn)){
    trigger_error(sprintf('FTP: Cannot connect to %s:%s', $host, $port), E_USER_NOTICE);
    return false;
  }
  
  if(false === ftp_login($conn, $user, $pass)){
    trigger_error('FTP: Cannot login with supplied credentials', E_USER_NOTICE);
    return false;
  }
  
  $status = ftp_put($conn, basename($file), $file, $mode);
  
  ftp_close($conn);
  
  return $status;
}

if(ftp_upload_file('ftp.address.co.uk', 'myuser', 'mypass', '/home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.csv')){
  echo 'Cool';
}else{
  echo 'Not cool';
}


?>

I wonder if this has something to do with the remote target path, maybe we cannot upload to the home directory of the FTP server.

What is the full remote path you want to upload to?

After a Google or two, I noticed that a few solutions pointed to setting the FTP connection to passive mode, try this then. :slight_smile:


<?php
error_reporting(-1);
ini_set('display_errors', true);

function ftp_upload_file($host, $user, $pass, $file, $mode = FTP_BINARY, $passive = true,  $port = 21){
  
  if(false === is_readable($file)){
    trigger_error(sprintf('FTP: %s cannot be opened', $file), E_USER_NOTICE);
    return false;
  }
  
  $conn = ftp_connect($host, $port);
  
  if(false === is_resource($conn)){
    trigger_error(sprintf('FTP: Cannot connect to %s:%s', $host, $port), E_USER_NOTICE);
    return false;
  }
  
  if(false === ftp_login($conn, $user, $pass)){
    trigger_error('FTP: Cannot login with supplied credentials', E_USER_NOTICE);
    return false;
  }
  
  ftp_pasv($conn, (bool)$passive);
  
  if(FTP_BINARY !== $mode){
    $mode = FTP_ASCII;
  }
  
  $status = ftp_put($conn, basename($file), $file, $mode);
  
  ftp_close($conn);
  
  return $status;
}

$result = ftp_upload_file(
  'ftp.site.co.uk',
  'myusername',
  'mypassword',
  '/home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.csv'
);

echo $result ? 'Cool' : 'Not cool' ;

Genius!!! That works a dream on one of the ftp sites. Having issues with the other one that I need to send to though I think this is perhaps due to passive ftp. How would I set the script to use passive mode? Also would this work for a .zip file?

Really appreciate the help with this. Thanks again!

Yay! :smiley:

It’s already setup for passive, you can switch this though by passing false as the 6th param. :slight_smile:


$result = ftp_upload_file(
  'ftp.site.co.uk',
  'myusername',
  'mypassword',
  '/home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.csv',
  FTP_BINARY,
  false
);

echo $result ? 'Cool' : 'Not cool' ;

I’m getting the following error:

Warning: ftp_login() [function.ftp-login]: Login authentication failed in /home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.php on line 24

Notice: FTP: Cannot login with supplied credentials in /home/mysite/domains/mysite.co.uk/public_html/Feeds/myfile.php on line 25
Not cool

Problem is when I use the same login details via dreamweaver I can connect to the ftp site perfectly??

Any ideas?