This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheOddByte's profile picture

[PHP] Uploading to a file

Started by TheOddByte, 09 December 2013 - 11:13 AM
TheOddByte #1
Posted 09 December 2013 - 12:13 PM
Hello everyone, I've recently wondered how you can simply upload some text to a file in CC.
I've tried using http.post but it seems you have to use PHP to handle the uploaded text and save it into a file.
Now my question is how you can do this, I don't know PHP and would be grateful to receive some help about it.

- Hellkid98
Left4Cake #2
Posted 09 December 2013 - 02:30 PM

if(isset($_POST['variable'])){ //if the post value 'variable' is set
$f = "file location here" //where on the server to save the file
if (!file_exists($f)){ //if file already exists
  $handle = fopen($f, 'w'); //opens the file from the first charater
  if(!fwrite($handle,stripslashes($_POST['variable']))){ //writes to the file removeing the php escapes and will run code is not running.
   //this code will run if the file can not be written
  }
}

Hope this works. I sort of just striped apart something from my previous work.
TheOddByte #3
Posted 09 December 2013 - 02:33 PM
So with this code I can just do something like this?

http.post("<url>", "Hello World")
Anyway, Thanks :D/>
Goof #4
Posted 09 December 2013 - 02:37 PM
No.

You need to declare the variable first..

(you need to post the "variable" variable on the website..
 http.post("<url>","variable="..variable or "hello world") 
That should do the trick

EDIT:

Oh wait!!


if you want to send the whole text file via CC, then you have to use fs.open, and then read all lines, and then post that as a variable
Edited on 09 December 2013 - 01:39 PM
TheOddByte #5
Posted 09 December 2013 - 02:59 PM
Well thanks for explaining, But I don't want to send a file, And I knew how to send a file I just didn't knew that I had to have "variable="

Edit: Hmm.. It still doesn't work.. The file doesn't change :/ Does it matter what extension you have on the file you save it to? ( I currently don't have one )
Edited on 09 December 2013 - 02:12 PM
Left4Cake #6
Posted 09 December 2013 - 04:45 PM
The file doesn't change :/

If you want it to override an existing file you will have to remove the conditional: "if (!file_exists($f)){" as it checks to see if the file doesn't exist.
oeed #7
Posted 09 December 2013 - 06:03 PM
No.

You need to declare the variable first..

(you need to post the "variable" variable on the website..
 http.post("<url>","variable="..variable or "hello world") 
That should do the trick

EDIT:

Oh wait!!


if you want to send the whole text file via CC, then you have to use fs.open, and then read all lines, and then post that as a variable

Having the variable in the url is 'get', not post. You shouldn't need both.
TheOddByte #8
Posted 09 December 2013 - 06:42 PM
The file doesn't change :/

If you want it to override an existing file you will have to remove the conditional: "if(!file_exists($f)){" as it checks to see if the file doesn't exist.
Well I tried deleting the file also, And it doesn't even create a new one .-.
oeed #9
Posted 09 December 2013 - 07:06 PM
The file doesn't change :/

If you want it to override an existing file you will have to remove the conditional: "if(!file_exists($f)){" as it checks to see if the file doesn't exist.
Well I tried deleting the file also, And it doesn't even create a new one .-.

Trying using another method to transfer the data. Try using $_GET['variable'], not $_POST['variable']. And add this to your url '?variable=…'
Edited on 09 December 2013 - 06:07 PM
GravityScore #10
Posted 10 December 2013 - 05:11 AM
Also don't forget to textutils.urlEncode the data you want to send.

Eg:


var = "hello there!"
encodedVar = textutils.urlEncode(var)
http.post("http://url", "var=" .. encodedVar)
TheOddByte #11
Posted 11 December 2013 - 08:03 AM
And add this to your url '?variable=…'
Which url? In the computercraft program or in the PHP file?
Current CodesPHP

if(isset($_GET['variable'])){
$f = "highscoresData" //where on the server to save the file
if (!file_exists($f)){
  $handle = fopen($f, 'w'); //opens the file from the first charater
  if(!fwrite($handle,stripslashes($_POST['variable']))){ //writes to the file removeing the php escapes and will run code is not running.
   //this code will run if the file can not be written
  }

Lua


local function post( data )
    local encData = textutils.urlEncode( data )
    local tryPost = http.post("http://www.hellkid98.eu.pn/Lasers/upload.php?variable=...", "variable=" .. encData)
    if tryPost then
        return true
    else
        return false
    end
end
Edited on 11 December 2013 - 07:17 AM
GravityScore #12
Posted 11 December 2013 - 11:36 AM
If you are using http.get in Lua:

http.get("http://www.example.com/test.php?variable=" .. textutils.urlEncode("hello there!"))
And use the $_GET superglobal in PHP. Eg:

if (isset($_GET["variable"])) {
	echo $_GET["variable"];
}

If you are using http.post in Lua:

http.post("http://www.example.com/test.php", "variable=" .. textutils.urlEncode("hello there!"))
And use the $_POST superglobal in PHP. Eg:

if (isset($_POST["variable"])) {
	echo $_POST["variable"];
}

Hope this clarifies it.

What should work:

Lua:

local fileContents = "hello there!"
local result = http.post("http://www.example.com/upload.php", "content=" .. textutils.urlEncode(fileContents))
local resultContent = result.readAll()
result.close()
if resultContent:find("true") then
	print("yay!")
else
	print("oh :(/>/>/>/>")
end

PHP:

$filename = "test.txt";
if (isset($_POST["content"])) {
	if (!file_exists($filename)) {
		// file_put_contents does the same thing as opening the file, writing to it, and closing it. It's just easier to type and returns false on error
		if (file_put_contents($filename, $_POST["content"]) !== false) {
			exit("true\n");
		}
	}
}
exit("nope\n");

If this doesn't work, I've either made a mistake somewhere, or the server you're using hasn't given PHP enough permissions to write to files.
Edited on 11 December 2013 - 10:44 AM
TheOddByte #13
Posted 12 December 2013 - 10:03 AM
Thanks, Grav. It must be the servers fault then..
Anyone knows about a good server with good PHP permissions?