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

Anyone able to help me "convert" this lua code into PHP?

Started by Goof, 22 May 2014 - 02:09 PM
Goof #1
Posted 22 May 2014 - 04:09 PM
Hello..

I dont know if i should post this as a comment in my topic about listing all files on computer,
but this isnt about the same program, so i thought it would be better to start this topic in general.

I am making an upload.php site, where i can upload files..

But its long time since i last used php, and i cant remember much of the functions.
So i thought about making everything in lua, then if someone could help me convert into PHP.

my code:
Spoiler


local postedFileData = post["fileData"]
local postedAuth = post["auth"]
local postedMethod = post["method"]
local postedFileName = post["fileName"]
local uploadPath = "http://somerandomwebsite.com/uploads/"
local currentID = 0
if postedMethod == "startSession" then
  local folderIDS = fs.list( uploadPath )
  if posted
  currentID = 0
  for k,v in pairs( folderIDS ) do
    currentID = currentID + 1
  end
  fs.makeDir( uploadPath .. tostring( currentID ) )
elseif postedMethod == "inSession" then
  -- Now this dir is ( for example )
  if string.find( postedFileName, "/" ) then
    -- if the path includes folders which isnt created in
    -- this folder, create it.
  end
  -- Now all paths are completed...
  -- now to create the file uploaded
  local fileOpened = fs.open( uploadPath .. tostring( currentID ) .. "/" .. postedFileName, "w" )
  fileOpened.write( postedFileData )
  fileOpened.close()
  return true, currentID -- IN PHP this should be echo 'true|' . currentID

end

The most "hard" part in this convertion, would be the "startSession" thing, since i dont know how to go through loops and tables etc.

I hope this explained as much as possible about my problem

Thanks in advance.
awsmazinggenius #2
Posted 23 May 2014 - 01:02 AM
There are frameworks for making sites in Lua, you know.
GravityScore #3
Posted 23 May 2014 - 02:15 AM
There are frameworks for making sites in Lua, you know.

Specifically https://github.com/ignacio/luanode (Node.js for Lua instead of JS)
zekesonxx #4
Posted 23 May 2014 - 03:47 AM
Or or you could actually use Node instead of LuaNode or Luvit; and get things like community support and a package manager.
awsmazinggenius #5
Posted 23 May 2014 - 02:32 PM
I'm saying that he could use one of those frameworks and then be able to write directly in Lua instead of in PHP or JavaScript.
Goof #6
Posted 24 May 2014 - 10:54 AM
Well… Yeah i didnt actually know there were a lua implemention…
but after some hours of trying to convert this as much as i can, i came up with this:
EDIT: I got most of it work, by using the mysql db as a sessionID counter…
But i still have problems, listing all files and dirs/subdirs, then putting the filepath and the content into an array.

Spoiler

<?php
  $postedAuth = hash(sha256, $_POST["auth"]);
  $postedMeth = $_POST["meth"];
  $postedFile = $_POST["data"];
  $uploadsPath = "uploads/";
  $pass = hash(sha256, "somethingsecretandhidden");
  $Sessions = false;
  $SessionCount = false;
  echo "Posted method: " . $postedMeth;
  #if($postedAuth==hash(sha256, $pass)) {
	if(strpos($postedMeth, "startSession")) {
	  $Sessions = array();
	  $files = glob($uploadsPath . "*");
	  foreach($files as $file)
	  {
		if(is_dir($file))
		{
		  if($file!="." &amp;&amp; $file!="..") {
			$Sessions[] = $file;
		  }
		}
	  }
	  $SessionCount = strval( sizeof( $Sessions ) + 1 ) . "/";
	  echo 'CREATING DIRS... : ' . $uploadsPath . $SessionCount;
	  mkdir( $uploadsPath . $SessionCount ) or die("Unable to create path!");
	  mkdir( $uploadsPath . $SessionCount . "System" );
	  mkdir( $uploadsPath . $SessionCount . "System/Apis" );
	  mkdir( $uploadsPath . $SessionCount . "System/Apps" );
	  echo ' SessionCount: ' . $SessionCount;
	} elseif(strpos($postedMeth, "inSession")) {
	  echo 'Putting file into: ' . $uploadsPath . $SessionCount . "System/test.lua";
	  $filee = fopen( "test.lua", "w");
	  fwrite( $filee, "TESTING" );
	  fclose( $filee );
	  echo 'Put file!';
	}
	echo '\nDone!';
  #} else{
  #  echo "Invalid Access";
  #}
?>



My only problem so far is to make the $sessionCount global or something… because it seems to be false when insetting test.lua inside that $sessioncount


Maybe i need to implement mysql db? if so, how would i use it?


Thanks
Edited on 25 May 2014 - 05:46 AM