-1

On localhost I tried to get the ftp file from server and the local file is successfully created. But when I'm trying in Ubuntu server it's displaying there was a problem and file is not downloading into server. Here is the code . And code file created in this location /var/www/html/:

<?php
// define some variables
$local_file = 'local.csv';
$server_file = 'serverfile.csv';
$ftp_server="ip address";
$conn_id = ftp_connect($ftp_server)or die("Couldn't connect to $ftp_server");
$ftp_user_name="user anme";
$ftp_user_pass="pass";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
} else {
echo "There was a problem\n";
}
ftp_close($conn_id);
?>

Please help me to solve this issue, in local host it's working fine but in Ubuntu server local file not creating/downloading.

3
  • What machine is your Localhost and what Ubuntu terminal? Are they running the same OS? Has PHP (or web-server) write permission in the directory where you want the file to be saved? Could you get a detailed error message using error_get_last()?
    – Melebius
    Jul 22, 2016 at 10:22
  • Hi Sir, i'm getting this error There was a problem Array ( [type] => 2 [message] => ftp_get(): Error opening [file] => /var/www/html/wftp.php
    – Sri P
    Jul 22, 2016 at 10:28
  • Please tell help me to resolve this problem
    – Sri P
    Jul 22, 2016 at 10:32

2 Answers 2

0

This seems to be a permission problem. The directory /var/www/html is most likely owned by root:

mirek@testsrv-ubuntu:/var/www/html$ ls -al
total 24
drwxr-xr-x  3 root  root   4096 dec 23  2015 .
drwxr-xr-x  3 root  root   4096 dec 22  2015 ..
-rw-r--r--  1 root  root  11510 dec 22  2015 index.html

The user running PHP must be allowed to write in that directory. If PHP is run by Apache, you need to allow the user www-data to write. The following command will list running Apache processes. The username is the first column.

mirek@testsrv-ubuntu:/var/www/html$ ps -ef | grep apache
root      1605     1  0 jul20 ?       00:00:16 /usr/sbin/apache2 -k start
www-data  1963  1605  0 12:01 ?        00:00:04 /usr/sbin/apache2 -k start

Since root is allowed to write anywhere, you can just change the owner of the /var/www/html directory to www-data and both root and www-data will be able to write there.

sudo chown www-data:www-data /var/www/html

More info on file permissions and owners:

8
  • permission for the file -rw-r--r-- 1 www-data www-data 0 Jul 22 10:26 whdfcleads.csv
    – Sri P
    Jul 22, 2016 at 10:46
  • @SriP Is that the file you want to write? If you want to create a new one, you need write permission on the directory.
    – Melebius
    Jul 22, 2016 at 10:47
  • i want to create at first time, once it's created the file again i have to rewrite the data by running the crons
    – Sri P
    Jul 22, 2016 at 10:49
  • Please help me to solve this issue
    – Sri P
    Jul 22, 2016 at 10:58
  • -rwxrwxrwx 1 www-data www-data 673 Jul 22 10:31 wftp.php in wftp.php this file having code
    – Sri P
    Jul 22, 2016 at 10:59
0

ftp_pasv($conn_id, true); is the solution for my issue

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .