Writte text file with message to FTP - php

I want to create a folder and create a file with a given message inside that folder on a FTP server.
Following is my attempt:
<?php
$dirname = "files/";
$dir = $_GET["name"];
$dirname .= $dir;
$filepath = $dirname."/message.txt";
$txt = $_GET["message"];
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, "login", "password");
// try to create the directory and file
if (ftp_mkdir($conn_id, $dirname)) {
echo "successfully created $dirname\n";
$myfile = fopen('php://temp', 'r+') or die("Unable to open file!");
fwrite($myfile, $txt);
rewind($myfile);
ftp_fput($conn_id, $filepath, $myfile, FTP_ASCII);
} else {
echo "There was a problem while creating $dirname\n";
}
// close the connection
ftp_close($conn_id);
?>
This creates the folder with the given name and also puts a message.txt file inside that folder but the file is always empty. How can I add my message to the file?

I would be most comfortable with using shell_exec to write to a local file. My solution would look like this.
<?php
$dirname = "files/";
$dir = $_GET["name"];
$dirname .= $dir;
$filepath = $dirname."/message.txt";
$txt = $_GET["message"];
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, "login", "password");
// try to create the directory and file
if (ftp_mkdir($conn_id, $dirname)) {
echo "successfully created $dirname\n";
$myfile = fopen('php://temp', 'r+') or die("Unable to open file!");
// Here is the good stuff with shell_exec.
$myfilelocation = '~/myfile.txt';
shell_exec( "cat $txt >> $myfilelocation" );
ftp_fput($conn_id, $filepath, $myfile, FTP_ASCII);
} else {
echo "There was a problem while creating $dirname\n";
}
// close the connection
ftp_close($conn_id);
?>

You seem to be opening the file in read mode ('php://temp', 'r+')
try fopen ('php://temp', 'w +')

Related

Create and upload a new file via ftp in php [duplicate]

This question already has answers here:
PHP ftp_put fails
(2 answers)
Closed 1 year ago.
Is it possible to upload a new file in ftp instead of rewrite an old one? I need code for creating a new file and uploading it into the server via ftp. If I try to upload it via ftp_put(), it does not work.
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
$ftp = ftp_connect("web");
ftp_login($ftp, "user", "pass");
echo ftp_put($ftp, "Tulassi/tulasi_test", $objPHPExcel, FTP_BINARY);
ftp_close($ftp);
exit;
<?php
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = '';
$destination_file = "new.xls";
$source_file = $objWriter;
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII);
if (!$upload) {
echo "FTP upload has failed!112";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>

PHP - reading FTP file without downloading it/saving it locally

I would like to get FTP files content without saving it locally.
This is what I have so far:
$ftp_server = "my_server";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to the server");
if (#ftp_login($ftp_conn, "username", "password")) {
$local_file = 'C:\Users\user\Desktop\testing.txt';
$fp = fopen($local_file, "w");
$d = ftp_nb_fget($ftp_conn, $fp, "commands.yml", FTP_BINARY);
while ($d == FTP_MOREDATA) {
$d = ftp_nb_continue($ftp_conn);
}
if ($d != FTP_FINISHED) {
echo "Error downloading $server_file";
exit(1);
}
ftp_close($ftp_conn);
fclose($fp);
$filename = 'C:\Users\user\Desktop\testing.txt';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
} else {
echo "Couldn't establish a connection.";
}
The code above saves the file and read the file content. Is it possible to read the file without saving it locally?
From the answer on the official PHP site from bob at notallhere dot com:
Don't want to use an intermediate file? Use 'php://output' as the
filename and then capture the output using output buffering.
ob_start();
$result = ftp_get($ftp, "php://output", $file, FTP_BINARY);
$data = ob_get_contents();
ob_end_clean();

Upload Multiple files to remote FTP Server using php

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 .

Setting imagepng path to save over FTP

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";
}
?>

PHP upload text to file

I am after some help with some code for a HTML feature.
I am wanting to get information from two textboxes on a web site (txtName,txtBody), and then save the contents of these textboxes to a file on an ftp server using PHP.
Can someone point me in the correct direction to do this?
thanks
File put contents can be used to accomplish this as dagon says, below is a simple example.
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
//Assign txtName to variable.
$name = $_POST['txtName'];
// Append a new person to the file
$current .= $name;
// Write the contents back to the file
file_put_contents($file, $current);
?>
If you dealing with ftp then you have to use http://www.php.net/manual/en/function.ftp-fput.php
$file = $_POST['txtName'];
file_put_contents($file, $_POST['txtBody']);
$fp = fopen($file, 'r');
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
fclose($fp);

Categories