I have made a class file which are running on my server.I am using it as a API.my some client will use it.In their program i have to create an object remotely of this class.
How will use my class file as RMI that object can create easily on other server.
Thanks
I don't think anything close to Java's RMI (as far as I understand it) can be done in PHP.
The best thing that comes to my mind is
Create an object in the remote script
Serialize that object
Return the serialized data to the calling script
Unserialize the data back into an object (note that all class definitions must be present locally for this to work!)
Note that things like active database connections, file handles and so on can not be transferred this way.
Whether this is a good - and fast enough - way to do what you want is hard to say. Maybe it helps.
To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as 'ftp://user:password#ftp.example.com/path/to/file'. (You can use the same sort of syntax to access files via HTTP when they require Basic authentication.)
<?php
$file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
if (!$file) {
echo "<p>Unable to open remote file for writing.\n";
exit;
}
/* Write the data here. */
fwrite ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file);
?>
http://php.net/manual/en/features.remote-files.php
include ("ftp://username:password#ftp.example.com/public_html/example.php");
$class = new Ford();
$dataRow = $class->consulta();
var_dump($conexion);
Related
In phalcon you can get uploaded file with this piece of code
//Check if the user has uploaded files
if ($this->request->hasFiles() == true) {
//Print the real file names and their sizes
foreach ($this->request->getUploadedFiles() as $file){
echo $file->getName(), " ", $file->getSize(), "\n";
}
}
each $file is an Phalcon\Http\Request\File instance.
But what if I want to create an file instance from an existing file on the server, how can I do that?
What I tried is this:
new Phalcon\Http\Request\File(array($fileDir));
But it returns an instance with empty properties.
any help would be appreciated :D
As per the documentation of that class I think the constructor does not expect an array. So just leave out the array( ) that you are passing to the constructor and you should be fine. Disclaimer: I did not check out the code and rely on the documentation being proper here.
Code example:
new Phalcon\Http\Request\File($fileDir);
But what if I want to create an file instance from an existing file on the server, how can I do that?
This class is designed to work with $_FILES superglobal, so as for me you are using wrong tool. I would create your own wrapper for using files that are already on server, or go for SplFileObject.
Anyway, how should parameter array look like you may be able to anderstand from here starting at line 74+ and it pretty reflects $_FILES structure.
When the submit button is pressed,the isset($_POST['ta'] works,but the file is not updated inside database with '---------'. any suggestion where I am going wrong?
if ( isset( $_POST['ta'] ) ) {
$handle = fopen('saw42.TextGrid', "a");
require('db_connection.php');
fwrite( $handle, "-----------");
fclose( $handle );
}
try this
if(isset($_POST['ta'])){
$handle=fopen('saw42.TextGrid',"a");
require('db_connection.php'); // don't know why this line is here
if ($handle===false){
echo 'Unable to open file';
}else{
fwrite($handle,"-----------");
fclose($handle);
}
}
Try to check your permissions on Unix OS , Is your file is 0644 or 0444
I pressume, the require-line fails and so the file is opened, but the script is aborted before something gets written inside. If errors are turned of (as is on some preconfigured systems), no error message will be shown.
Nevertheless the question is a bit confusing, since if a database (in the sense of a relational database system accessable through a database server) is meant, the code should not use any fopen-calls. If the 'database' is a simple file, the requirement of the db_connection.php seems to be unclear.
To clarify things a bit:
A (relational) database is a collection of tables (relations) that perhaps refer to each other. Such databases are typically filled and asked via SQL-language or some object-oriented interface (MySQL, MS-SQL, SQLITE, ...)
A database in the sense of 'some data' can also reference a simple file. In this case, you have to organize data by yourselves and use file access methods to access it.
I'm writing a system for a browser application that will store some particular php scripts in a database and then pull them out and execute them when needed. At first I tried using exec() and piping to php the output of a script that got the scripts out of the database and printed them. This worked in one use case, but not all, and feels brittle anyway, so I'm looking for a better way.
I'm now attempting to accomplish this through use of a PHP file stream in memory. For instance:
$thing = <<<'TEST'
<?php
$thing = array();
print "Testing code in here.";
var_dump($thing);
?>
TEST;
$filename = "php://memory";
$fp = fopen($filename, "w+b");
fwrite($fp, $thing);
//rewind($fp);
fclose($fp);
include "php://memory";
However, nothing is printed when the script is executed. Is this even possible by this means, and if not, is there another way to do this? I'm trying to avoid having to write temporary files and read from them, as I'm sure accessing the filesystem would slow things down. Is there a URL I can provide to "include" so that it will read the memory stream as if it were a file?
I don't think eval() would do this, as, if I remember correctly, it's limited to a single line.
Also, please no "eval = include = hell" answers. Non-admin users do not have access to write the scripts stored in the database, I know that this needs special treatment over the life-cycle of my application.
You need to use stream_get_contents to read from the php://memory stream. You cannot include it directly.
eval() and include are actually pretty the same. So eval() works with multiple lines - just FYI. However, I would prefer include here, I always think it's faster. Maybe I'm wrong, no Idea.
However, I think you should debug your code, I don't see a reason per-se why it should not work. You might need to rewind the pointer (you have commented that), but what you should check first-hand is, that your PHP configuration allows to include URLs. I know that that setting prevents using of the data:// URIs, so you might have this enabled.
Also you can always try if PHP can open the memory by using file_get_contents and dumping out. This should give you the code. If not, you already made some mistake (e.g. no rewind or something similar).
Edit: I've not come that far (demo):
<?php
/**
* Include from “php://memory” stream
* #link https://stackoverflow.com/q/9944867/367456
*/
$thing = <<<TEST
<?php
\$thing = array();
print "Testing code in here.";
var_dump(\$thing);
TEST;
$filename = "php://memory";
$fp = fopen($filename, "w+b");
fwrite($fp, $thing);
rewind($fp);
var_dump(stream_get_contents($fp));
This is what I found out:
You should not close the "file". php://memory is a stream once closed it will disappear.
You need to access the $fp as stream than, which is not possible for include out of the box AFAIK.
You then would need to create a stream wrapper that maps a stream resource to a file name.
When you've done that, you can include a memory stream.
The PHP settings you need to check anyway. There are more than one, consult the PHP manual.
It might be easier to use the data URI (demo):
<?php
/**
* Include from “php://memory” stream
* #link https://stackoverflow.com/q/9944867/367456
*/
$thing = <<<TEST
<?php
\$thing = array();
print "Testing code in here.";
var_dump(\$thing);
TEST;
include 'data://text/plain;,'. urlencode($thing);
See as well: Include code from a PHP stream
If there is a way to include from php://memory then this is a serious vulnerability. While it has many uses, eval is used quite often with code obfuscation techniques to hide malicious code.
(Every tool is a weapon if you hold it right.)
With that said, there (thankfully) doesn't appear to be any obvious way to include from php://memory
I need to include one PHP file and execute function from it.
After execution, on end of PHP script I want to append something to it.
But I'm unable to open file. It's possible to close included file/anything similar so I'll be able to append info to PHP file.
include 'something.php';
echo $somethingFromIncludedFile;
//Few hundred lines later
$fh = fopen('something.php', 'a') or die('Unable to open file');
$log = "\n".'$usr[\''.$key.'\'] = \''.$val.'\';';
fwrite($fh, $log);
fclose($fh);
How to achieve that?
In general you never should modify your PHP code using PHP itself. It's a bad practice, first of all from security standpoint. I am sure you can achieve what you need in other way.
As Alex says, self-modifying code is very, VERY dangerous. And NOT seperating data from code is just dumb. On top of both these warnings, is the fact that PHP arrays are relatively slow and do not scale well (so you could file_put_contents('data.ser',serialize($usr)) / $usr=unserialize(file_get_contents('data.ser')) but it's only going to work for small numbers of users).
Then you've got the problem of using conventional files to store data in a multi-user context - this is possible but you need to build sophisticated locking queue management. This usually entails using a daemon to manage the queue / mutex and is invariably more effort than its worth.
Use a database to store data.
As you already know this attempt is not one of the good ones. If you REALLY want to include your file and then append something to it, then you can do it the following way.
Be aware that using eval(); is risky if you cannot be 100% sure if the content of the file does not contain harmful code.
// This part is a replacement for you include
$fileContent = file_get_contents("something.php");
eval($fileContent);
// your echo goes here
// billion lines of code ;)
// file append mechanics
$fp = fopen("something.php", "a") or die ("Unexpected file open error!");
fputs($fp, "\n".'$usr[\''.$key.'\'] = \''.$val.'\';');
fclose($fp);
I experimenting with twitter streaming API,
I use Phirehose to connect to twitter and fetch the data but having problems storing it in files for further processing.
Basically what I want to do is to create a file named
date("YmdH")."."txt"
for every hour of connection.
Here is how my code looks like right now (not handling the hourly change of files)
public function enqueueStatus($status)
$data = json_decode($status,true);
if(isset($data['text'])/*more conditions here*/) {
$fp = fopen("/tmp/$time.txt");
fwirte ($status,$fp);
fclose($fp);
}
Help is as always much appreciated :)
You want the 'append' mode in fopen - this will either append to a file or create it.
if(isset($data['text'])/*more conditions here*/) {
$fp = fopen("/tmp/" . date("YmdH") . ".txt", "a");
fwrite ($status,$fp);
fclose($fp);
}
From the Phirehose googlecode wiki:
As of Phirehose version 0.2.2 there is
an example of a simple "ghetto queue"
included in the tarball (see file:
ghetto-queue-collect.php and
ghetto-queue-consume.php) that shows
how statuses could be easily collected
on to the filesystem for processing
and then picked up by a separate
process (consume).
This is a complete working sample of doing what you want to do. The rotation time interval is configurable too. Additionally there's another script to consume and process the written files too.
Now if only I could find a way to stop the whole sript, my log keeps filling up (the script continues execution) even if I close the browser tab :P