PHP Read a ftp file line by line from localhost - php

I have a text file stored on a ftp account. I have the hostname, username and the password. I dont know what is the name of the file, but i know that on root there is only 1 file. I have to read that file line by line and store that content into a php variable.
This is what i have tryed:
$ftp_server = "myhostname";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "myusename";
$ftp_user_pass = "mypassword";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) { }
else {
ftp_pasv( $conn_id, true );
$contents = ftp_nlist($conn_id, ".");
$filename = $contents[0]; //name of the file
$handle = fopen($filename, "r");
$file_content = fread($handle, filesize($filename));
print_r($file_content);
}
I run this script from my localhost, but it seems it trys to read a file stored in the same folder where is this php file insted of reading from the ftp server.

You need to download a file from FTP with ftp_get first and then fopen and fread on local machine.
http://php.net/manual/en/function.ftp-get.php

Related

how to get file size on ftp server using php?

i need a list of all the files on an FTP server. I can get a list of files using php , but no idea how to get the file size. How do I get the file sizes? Thanks.
You can call
ftp_size
As in
int ftp_size ( resource $ftp_stream , string $remote_file )
To get the size of files over FTP.
For example:
$file = 'somefile.txt';
// 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);
// get the size of $file
$res = ftp_size($conn_id, $file);
if ($res != -1) {
echo "size of $file is $res bytes";
} else {
echo "couldn't get the size";
}
// close the connection
ftp_close($conn_id);
However only some FTP servers support this.
Source: http://php.net/manual/en/function.ftp-size.php

File upload via FTP error

Code:
$file = '/export/clients.xml';
$remote_file = '/clients.xml';
$ftp_server = "ftp://my.address.com";
$ftp_user_name = "user";
$ftp_user_pass = "password";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
Now the original file path is: ftp://my#address.com/domains/website.com/public_html/export/clients.xml
How should I write the file path so that it could complete the file transfer? I want the file to be copied into the root directory of my FTP server. But it doesn't work this way.
Problem was FTP server name. I removed the ftp:// part and now it's working.

PHP Unzip after FTP Upload

I have these lines of code which uploads a file via FTP. After uploading, I need to unzip the file. Problem encountered was, the file is successfully uploaded but I cannot unzip it. Can anyone help me with this?
if(isset($_POST['submit'])){
$file = $_FILES['uploaded_file']['name'];
$remote_file = $_FILES['uploaded_file']['name'];
$ftp_server = "1xx.xx.xx.xx";
$ftp_user_name = "xxuser";
$ftp_user_pass = "xx2016";
$toform2 = "FormType/Upload/";
$tounzip2 = "Unzip/";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// upload the file
$ftype = substr ($file,0,2);
if ($ftype == "F2") {
ftp_chdir ($conn_id,$toform2);
$upload = ftp_put($conn_id,$remote_file,$file,FTP_ASCII);
$file_path = $toform2;
// check upload status
if($upload){
// Unzip file
$zip = new ZipArchive();
$x = $zip->open($toform2);
if ($x === true) {
ftp_chdir ($conn_id,$tounzip2);
$zip->extractTo($tounzip2);
$zip->close();
echo "success"."</br>";
}else{
echo "fail";
}
//echo "Uploaded $source_file to $ftp_server as $destination_file" ;
}else{
//echo "FTP upload has failed!" ;
}
}
If the script has SSH access to the remote server, you can ssh2-exec a remote script to unzip your file.
SSH2-Connect:
http://php.net/manual/en/function.ssh2-connect.php
SSH2-Exec:
http://php.net/manual/en/function.ssh2-exec.php
First upload your file using FTP. (SFTP would be better)
Open an SSH connection and execute the unzip command remotely.

How can I open a file from another ftp and write?

I tried to open a file in another ftp and write but I coulnd how can I do that?
$ftpstream = #ftp_connect('****');
//Login to the FTP server
$login = #ftp_login($ftpstream, '****', '***');
if($login) {
echo "Conectado";
$fp = fopen('file.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
}
The code you have posted is wrong. It opens up an FTP stream (ftp_connect), but then writes a file to the local file system (fopen). Your ftp stream won't allow you to write to it with fwrite- you need to use the commands to transfer entire files.
You can do what you want with fopen if you use an ftp:// scheme.
For example:
$fp = fopen("ftp://user:password#example.com/file.txt","w");
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
Alternativley, you can write the file to the local file system and use an ftp stream to transfer it:
$file = 'somefile.txt';
// create your file in the local file system here.
$remote_file = 'readme.txt';
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
As always, be clear on what you want to do.
PHP FTP reference is here
PHP scheme & wrapper reference (for use with fopen) is here

Moving an uploaded file onto a remote server

I am trying to move an uploaded file onto a remote server, this isn't working;
move_uploaded_file($tmp_name, "uploads/$code1/$code.$fileex");
$ftp_server = "IP";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$file = $tmp_name;
$remote_file = "/public_html/test/uploads/";
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
I get this erorr;
Warning: ftp_put() [function.ftp-put]: Can't open that file: Is a directory in /home/file/public_html/uploaded.php on line 52
Your $remote_file variable is pointing to a directory when it should point to a file. Try changing $remote_file to $remote_file = "/public_html/test/uploads/".$file;
You should probably wrap the portion that uploads the file in an if statement that checks to see if you are actually connected properly to the FTP
Also, when uploading a file, you need file 1 and file 2. Right now you've supplied file 2 and a directory.
http://php.net/manual/en/function.ftp-put.php
The file you are trying to move to is the directory "/public_html/test/uploads/", you need to append the filename and extension onto the directory.
Add the following line at the end of the /etc/vsftpd.conf file
Add pasv_promiscuous=YES it

Categories