Save file php script

Edit and save a file on server (PHP)

Script: PHP file: I expect for the C++ file of a text file at this point to get save with the content in the . How can I send back the contents to the script and save it to the same file or to a file with a new name? PHP, HTML file:

Edit and save a file on server (PHP)

I have a page that displays the content of a C++ file into a textarea and I need to be able to save the contents of it using a script. (The C++ file does not have to be configured just saved.)

I’m using a PHP script to load the code from a file to display it on the textarea . How can I send back the contents to the script and save it to the same file or to a file with a new name?

function savefiles() < var contentArea = document.getElementsById('cpp_content'); var cpp_content = contentArea.value; var request = new XMLHttpRequest(); request.open('POST', '/php/save_contents.php', true); request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); request.onload = function() < if (this.status >= 200 && this.status < 400) < console.log("Success"); var resp = this.response; >else < alert ("Target server reached, but it returned an error" ); >>; request.onerror = function() < // There was a connection error of some sort >; request.send(cpp_content); > 

I expect for the C++ file of a text file at this point to get save with the content in the textarea .

 $file = file_get_contents($fn); ?>   

ig @WookieeTyler

">

Another option would be to send the contents to a separated PHP file through an XMLHttpRequest. This way you don’t have to reload the page when saving. Something like this:

var request = new XMLHttpRequest(); request.open('POST', '/my/url/save_contents.php', true); request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); request.onload = function() < if (this.status >= 200 && this.status < 400) < // Success! var resp = this.response; >else < // We reached our target server, but it returned an error >>; request.onerror = function() < // There was a connection error of some sort >; request.send('cpp_content=' + cpp_content); 

Uploaded file doesnt get saved in the server in php,

Читайте также:  Android games with java

Php Tutorial how to File Upload and save into a folder html5

PHP Beginners tutorial uploading files from pc to remote server folder real time example.
Duration: 10:39

PHP File Upload Example

In this quick PHP file upload example we create an upload form in HTML for the browser to
Duration: 6:35

Saving file to server using PHP and Javascript

I have a data string in Javascript and I would like to store it in a text file in my server.

The normal thing to do is to send the data to a PHP medium file to process it and do the file storage part.

This is the code I used but it looks like it doesn’t run at all as the destination file matrice.txt is still blank :

What is the problem and how can I fix it ?

First thing to note: You’re calling the callback as many times as there are elements on sort_data array, which I believe it’s not the intended functionality.

Also, you should really check if your ajax() call was successful or not. Ajax call has a parameter method (instead of type ) according to the documentation.

Finally, you could write to a file with a one-liner.

So, let’s put it all together:

script.js

function finalCoords() < var matrix = []; for(var i = 0; i < sort_data.length; i++) < matrix.push([data.nodes[i].name, sort_data[i].x, sort_data[i].y, sort_data[i].z]); /* This data is collected from other arrays */ >var matrixStr = matrix.join(" "); /* This is the string I want to store */ console.log(matrixStr); $.ajax(< url: "matrice.php", /* This is the PHP Medium */ data: matrixStr, cache: false, async: true, method: 'POST', timeout : 5000 >) .done(function() < console.log("success"); >) .fail(function() < console.log("error"); >) .always(function() < console.log("complete"); >); > 

matrice.php

That should do the trick and tell if there are any errors with the ajax call.

EDIT

Taking a cue from the comment from charlietfl (thanks, btw), we should take the raw post data:

Php save file to specific location?, Try this:

How to collect file and save on server with PHP

I know this similar question is asked before, but I can’t find a solution to my specific problem. I have this code, and it saves to a file and downloads it immediately to the desktop when run from the browser. But I need it to save it on a server. How do I do this with this specific code?

Do I need to save the file into a variable e.g. $files first?

Executed, it behaves like we expect:

% php output.php hey F4LLCON! 

Now I’ll modify it to add output buffering and save to the file and write to stdout (using regular echo calls!):

After executing, the output in the file catched.txt is equal to what we got earlier (and still get) on stdout:

Now I’ll modify it again to show how generators from PHP 5.5 will provide you with an elegant solution that doesn’t need to sacrifice performance (the previous solution requires you to save all the intermediate content in one giant output buffer):

; $f = fopen("/tmp/catched2.txt", "wb"); foreach ($main() as $chunk) < fwrite($f, $chunk); echo $chunk; >fclose($f); ?> 

We aren’t storing everything in one giant buffer, and we can still output to file and stdout simultaneously.

If you don’t understand generators, here’s a solution where we pass a callback «print» function to main(), and that function is used every time we want to output (only one time here).

; $f = fopen("/tmp/catched3.txt", "wb"); $main(function($output) use ($f) < fwrite($f, $output); echo $output; >); fclose($f); ?> 

Load a file, edit it and the save it (server side) PHP, Try it out yourself, and consider this as the base for a more complex system. If you want to create a new file, just enter a new filename in the

Источник

Saving files with PHP scripts in local applications

When run in a desktop application, PHP code can access local files on the end user’s computer, so it is possible to save and modify files locally. To load compiled files, please see this page.

Please refer to the Saving Files topic of the General Demonstration for live demonstrations and further explanation about saving local files with your ExeOutput for PHP apps.

Saving and storing files locally¶

PHP provides you with several ways to load and save files. In order to correctly save files, you must ensure that the user has permissions to write to files into the location you want.

For instance, a portable application is generally started from a USB stick and the folder that contains the EXE has write permissions. Your application will be able to save files in that folder. On the contrary, if your application is installed in the Program Files directory, it is generally not allowed to save files in the application’s folder because of the Windows UAC feature.

Thus, ExeOutput for PHP provides you with a storage folder dedicated to your application. The absolute path to this folder can be retrieved with the following php command:

This folder is always available: you can use it to store the settings of your application. You may customize the name of the storage folder in ExeOutput by going to the «Output -> Output Settings» page.

PHP code: saving a file with fopen / fwrite¶

In compiled applications, you must pass absolute paths to the fopen PHP function.

If you want to let your end users choose the path where the file should be saved, you can display a Save As dialog box.

Источник

How to Write/Save to a File with PHP [Examples]

PHP Write/Save File

This tutorial will demonstrate how to write data to a file on the server in the PHP programming language. Code examples are provided to show you how it’s done.

Writing data to a file server-side is a common task. Whether you’re writing log files so you can debug your app, or saving user submitted data, you will need to save a file at some point in your web development career.

Writing data to a file in PHP is a three-step process. You must first open the file with the fopen() function, and then write to it with the fwrite() function, then close the file resource.

Creating or Opening a File with fopen()

The PHP fopen() function will open a file resource and provide a pointer to that resource which can be used by other functions. If a file does not exist at the path specified, a new file will be created.

fopen() expects both a file path and a mode as parameters – the mode specifies what type of access to the file is required – reading or writing, or both.

Writing/Saving to a File with fwrite()

fwrite() expects two parameters – a file pointer supplied by fopen() and the data to be written.

fwrite() can be called multiple times while a file resource is open to write data to the file sequentially.

Closing a file with fclose()

The fclose() function closes a file pointer, freeing it for use by other processes.

You should always close file pointers when you aren’t using them to reduce the chance of conflicting with other processes which may be trying to read or write to the file.

Examples – Writing Data to Files in PHP

Show often works better than tell, so here is how fopen() and fwrite() are used in combination to write data to a file:

// Assign the file pointer from fopen to the variable $file // Note the write permission requested (w) $file = fopen("test.txt", "w") or die("Cannot open file."); // define the data to be written to the file $data = "Any text data will do.\n"; // Write the data to fhe file with fwrite() fwrite($file, $data); // Write some additional data to the file $data = "More text data to write.\n"; // Write the additional data to the file fwrite($file, $data); // Close the file fclose($file);

Watch Out! Data will be Overwritten!

Be aware that if the file already exists, it will be overwritten by any data called between fopen() and fclose().

Appending Data to a File in PHP

To append to the file instead of overwriting it, use the append (a) mode of fopen(), rather than the write (w) mode:

// Note the append permission requested (a) $file = fopen("test.txt", "a") or die("Cannot open file."); $data = "This will be appended to the end of an existing file.\n"; fwrite($file, $data); fclose($file);

A Note on Permissions

If you receive permission errors you will need to ensure the directory you are running the script from, or the current working directory, are writable by the PHP process. On web servers, this usually means the www-data user will require permission to wread and write.

Источник

Оцените статью