I am trying to figure out how to make a simple html code so that whenever anyone on the page types anything into the provided text box and hits submit, it adds that written text to an already existing .txt file on my server.
UPDATE 2/20/14 9:29AM: Well that's unfortunate. I kind of figured I required a .php but sadly my wepbage is hosted through homestead and they do not have .php functionality. Was just hoping there was a workaround. Thanks for the responses.
If your server can run php then the following page can be requested when the user clicks submit. (using post method)
<?php
$in = $_POST['name'];
$file = 'names.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $in;
// Write the contents back to the file
file_put_contents($file, $current);
?>
You would have to use PHP to do this. Make the form action on your form link to a PHP script and inside have something like this.
<?php
$file = 'test.txt';
$currentText = file_get_contents($file);
$currentText .= $_POST['text'];
file_put_contents($file, $currentText);
?>
Related
This question already has answers here:
Intervention Image: Save image directly from an url with original file name and ext?
(3 answers)
Closed 2 years ago.
I have an API project using Laravel and these requires me to have a download and save it to public folder and in database.
Flow:
A field will come through the API call, i.e. through the company_logo field. It will come through as a URL: http://a.mktgcdn.com/p/Lr5Sfx0OllncmEPfQGX11ZqUZSmcUhbvc5a-u-FrMCU/1.0000/300x300.jpg. I need to grab the image and save it locally to a unique folder per listing and save it also to database.
I don't have code yet because I don't know what to do here in Laravel.
You can simply do that by using Intervention Image
You can find how to install here
$path = 'http://a.mktgcdn.com/p/Lr5Sfx0OllncmEPfQGX11ZqUZSmcUhbvc5a-u-FrMCU/1.0000/300x300.jpg';
$filename = basename($path);
Image::make($path)->save(public_path('yourimagefolder/' . $filename));
this will save the image. and following code will save it to database
$image = new ModelName();
$image->imagename =$filename;
$image->save();
Hope this help, you need to define $url and $filename variable
$data = file_get_contents( $url );
file_put_contents( public_path($filename), $data );
I am trying to use the following php code to display another html page. Sadly nothing is printed on the screen, and yes I have checked and confirmed that the link works. Any thoughts on why this could be happening would be helpful thank you.
$site = readfile("http://k9minecraft.tk/thanks.html");
echo $site;
First, make sure php is configured so that allow_url_fopen is on.
If you want to save the string to a variable, try using file_get_contents instead since it adds the file to memory. Refer file_get_contents for more detailed information on official documentation.
$site = file_get_contents("http://k9minecraft.tk/thanks.html");
echo $site;
The readfile function reads the file directly to the output buffer, so it doesn't require an echo. Refer readfile for more detailed information on official documentation.
readfile("http://k9minecraft.tk/thanks.html");
readfile is more efficient in terms of memory usage, whereas file_get_contents more useful in many situations.
<?php
//other php codes here
?>
Link Name
<?php
//continue other php codes.
?>
how about that ? Without using readfile.
readfile() actually returns just the number of characters read from the file. The content of the file is stored in buffer.
Turn output buffering OFF.
Use something like ob_end_flush
Check this too...It may help you
I have a perl CGI script, that works, to upload a file from a PC to a Linux server.
It works exactly as intended when I write the call to the CGI in my own HTML form and then execute, but when I put the same call into an existing application, the file is created on the server, but does not get the data, it is size zero.
I have compared environment variables (those I can extract from %ENV) and nothing there looks like a cause. I actually tried changing several of the ENV in my own HTML script, to the values the existing application was using, and this did not reveal the problem.
Nothing in the log gives me a clue, the upload operation thinks it was successful.
The user is the same for both tests. If permissions were an issue, then the file would not even be created on the server.
Results are the same in IE as in Chrome (works from my own HTML script, not from within the application).
What specific set up should I be looking at, to compare?
This is the upload code:
if (open(UPLOADFILE, ">$upload_dir/$fname")) {
binmode UPLOADFILE;
while (<$from_fh>) {
print UPLOADFILE;
}
close UPLOADFILE;
$out_msg = "Done with Upload: upload_dir=$upload_dir fname=$fname";
}
else {
$out_msg = "ERROR opening for upload: upload_dir=$upload_dir filename=$filename";
}
I did verify that
It does NOT enter the while loop, when running from inside the application.
It does enter the while loop, when called from my own HTML script.
The value of $from_fh is the same for both runs.
All values, used in the below block, are exactly the same for both runs.
You could check the error result of your open?
my $err;
open(my $uploadfile, ">", "$upload_dir/$fname") or $err = $!;
if (!$uploadfile) {
my $out_msg = "ERROR opening for upload: upload_dir=$upload_dir filename=$filename: $err";
}
else {
### Stuff
...;
}
My guess, based on the fact you are embedding it in another application, is that all the input has been read already by some functionality that is part of the other application. For example, if I tried to use this program as part of a CGI script, and I had used the param() function from CGI.pm, then the entire file upload would have been read already. So if my own code tried to read the file again, it would receive zero data, because the data would have been ready already.
I have a ruby file namely sample.rb. I want to execute that from my form and, actually my ruby code when executed will write to a html file. So what i need to do is when i click on the button in the form it should execute the ruby code and then it has to open the html file that comes from execution in one of the frame in my form. Is it is possible? If possible! how to do?
What i have tried is
<?php
exec("./sample.rb")
?>
But what it does is. It simply took the url of the api that i have used in ruby code and it returns the json from that api.
May be you should use rails for that http://rubyonrails.org
You are invoking sample.rb as an external command, just like any other shell script. Therefore you have to capture and process its output yourself.
You say that "it returns the JSON from the api". That's fine, you can extract the data you are interested in, e.g.:
<?php
$json = exec("./sample.rb");
$data = json_decode($json);
$url = $data->url; # assuming there is an URL field
?>
Now you can for example output a link:
click
or generate some JavaScript:
<script>
window.location.href="<?php echo $url ?>";
</script>
or redirect the user via a HTTP header:
<?php
header("Location: $url");
?>
I am working on my blog again, but I want to make an online text editor where you click a button and it saves it to their computer, and if you click the load button it loads a file from their computer. Does anyone know how to do that?
Thanks in Advance!
There is currently no way to write to the filesystem of the client. This is for security reasons.
However, with JavaScript you can store some data (up to 5MB) in the localStorage object and retrieve it from there later on. localStorage #MDN
The localStorage can only store strings in its key-value store, so you might have to serialize your objects before storing them.
Storing
window.localStorage.setItem( 'yourId', JSON.stringfy( yourData ) );
Retrieving
var yourData = JSON.parse( window.localStorage.getItem( 'yourId' ) );
I'm unsure why there is so strong an it's impossible current with client-side storage: all you have to do is export the file as whatever format you want (even plain-text) and have the user download it -- akin to Save As. To load the file, have the user upload it again -- akin to Open.
This can be accomplished by posting the contents of the textarea to a php script, for example.
This is no more steps than using Save As... and Open dialogs in any other local word processing application.
The only reason you might want a user to do this, though, is to allow them to edit the file locally while they were disconnected from your blogging platform. So a more elegant alternative, unless you want the user to actually edit the file locally, is to save draft versions on the server (like StackExchange does).
Loading can be implemented like this in a form (sending to a PHP-script for example):
<input name="usedFile" type="file" class="file" size="50"
accept="text/csv,text/plain" />
This allows for example loading of text- and csv-files.
I think saving is not possible like this.
#comment
It really depends on what you want to do with the file. Here is a simple loading function:
if (!empty($_FILES['usedFile']['tmp_name'])) {
load_uploaded_file($_FILES['usedFile']['tmp_name'], $my_loaded_data);
}
// ...
function load_uploaded_file($filename, &$data) {
if (!file_exists($filename)) {
return False;
}
$data = array();
$rowCount = 0;
$handle = fopen($filename, 'r');
while (($line = fgets($handle, 4096)) !== False) {
$data[$rowCount] = $line;
$rowCount++;
}
fclose($handle);
return True;
}