[PHP] Uploading to a file
Started by TheOddByte, Dec 09 2013 12:13 PM
12 replies to this topic
#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
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
#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.
#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 #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..
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
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 trickEDIT:
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 by Mikk809h, 09 December 2013 - 02:39 PM.
#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 )
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 by Hellkid98, 09 December 2013 - 03:12 PM.
#7
Posted 09 December 2013 - 06:03 PM
Mikk809h, on 09 December 2013 - 02:37 PM, said:
No.
You need to declare the variable first..
(you need to post the "variable" variable on the website..
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
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 trickEDIT:
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.
#8
#9
Posted 09 December 2013 - 07:06 PM
Hellkid98, on 09 December 2013 - 06:42 PM, said:
Trying using another method to transfer the data. Try using $_GET['variable'], not $_POST['variable']. And add this to your url '?variable=...'
Edited by oeed, 09 December 2013 - 07:07 PM.
#10
Posted 10 December 2013 - 05:11 AM
Also don't forget to textutils.urlEncode the data you want to send.
Eg:
Eg:
var = "hello there!"
encodedVar = textutils.urlEncode(var)
http.post("http://url", "var=" .. encodedVar)
#12
Posted 11 December 2013 - 11:36 AM
If you are using http.get in Lua:
If you are using http.post in Lua:
Hope this clarifies it.
What should work:
Lua:
PHP:
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.
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 by GravityScore, 11 December 2013 - 11:44 AM.
#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?
Anyone knows about a good server with good PHP permissions?
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











