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);
Related
I have a bunch of movies that I'm trying to transfer from my CentOS server onto my Windows PC. But when I run them through this script they end up being corrupt. Is there something wrong with the script?
Thanks
$allFiles = glob("/var/www/html/ftp_pending/*");
// 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);
foreach($allFiles as $singleFile)
{
// check if a file exist
$path = "/"; //the path where the file is located
$file = substr( $singleFile, strrpos( $singleFile, '/' )+1 );
$check_file_exist = $path.$file; //combine string for easy use
// Returns an array of filenames from the specified directory on success or
// FALSE on error.
$contents_on_server = ftp_nlist($conn_id, $path);
// Test if file is in the ftp_nlist array
if (in_array($check_file_exist, $contents_on_server))
{
echo "$file is already on FTP Server, no need to re-upload <br />";
}
else
{
$localfile = '/var/www/html/'.$file.'';
$remote_file = $file;
// upload a file
if (ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
{
echo "successfully uploaded $file\n";
}
else
{
echo "There was a problem while uploading $file\n";
}
};
}
// remember to always close your ftp connection
ftp_close($conn_id);
You're trying to upload something other than a text-based file while using
(ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
You should be using FTP_BINARY instead of FTP_ASCII since movies (and images) are binary files.
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 +')
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();
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
I have a problem creating a local file, then uploading it to ftp server.
Here is my code:
$file = 'test';
$string = $phone.','.$y.','.$z;
$fp = fopen($file, 'w+');
fwrite($fp, $string);
fclose($fp);
$fp = fopen($file, 'r');
$conn_id = ftp_connect('domain');
ftp_login($conn_id, 'user', 'pass');
if (ftp_fput($conn_id, $file, $fp, FTP_BINARY)) {
} else {
}
ftp_close($conn_id);
fclose($fp);
unlink($file);
I dont get any error, I just exceed memory. I dont have any other problems with the ftp server, anywhere else but when I am uploading. As I said the file is only 12 bytes.