Is there away to read a source code from another site?
Source code as HTML, when you right click on a site and then "Source code".
I've tried:
<? $f = fopen ("http://www.example.com/f", r);
echo $f;
?>
It did not work. How do I do this?
Try
<?php
$f = file_get_contents("http://www.site.com/f");
echo $f;
?>
You could use curl to grab the remote HTML, and when printing I'd be sure to sanitize it so the viewing browser doesn't try to render or execute script. (after all, you can't guarantee to control the contents of the remote HTML - it could be malicious)
Alternatively you could use shell commands to grab it to a temporary file, and read that file in. Wget or the curl binary itself could be prodded into doing this for you. (wget -O /tmp/sometempfile http://www.site.com/f) but note this is dangerous, and might set off "alarms" for sysadmins watching the system. Calling wget et al and dumping in /tmp/ is generally the first thing someone tries to do when they break into a PHP application.
That all depends on what you qualify as "source". To grab the output from a site, you could use curl:
$ch = curl_init('http://www.google.ca');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
echo $data;
If you're talking about the server-side source of the page, then no, there's no way that you can do that (I hope that's not what you're talking about :))
For reading contents (source) of a remote page, at some other website:
<?php
$linktopage = "http://www.google.com/index.html";
$sourcecode = file_get_contents( $linktopage );
echo $sourcecode;
?>
Related
I have a php file called sample.php with the following content:
<?php
echo "Hello World!";
?>
And what I want to do, is to run this php script using a second php script.
I think shell_exec could help me, but I don't know its syntax.
By the way, I want to execute this files with cpanel. So I have to execute the shell.
Is there any way to do this?
If you need to write a php file's output into a variable use the ob_start and ob_get_contents functions. See below:
<?php
ob_start();
include('myfile.php');
$myStr = ob_get_contents();
ob_end_clean();
echo '>>>>' . $myStr . '<<<<';
?>
So if your 'myfile.php' contains this:
<?php
echo 'test';
?>
Then your output will be:
>>>>test<<<<
You can use cURL for remote requests. The below is from php.net:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Here's a good tutorial: http://www.sitepoint.com/using-curl-for-remote-requests/
Consider watching this YouTube video here as well: http://www.youtube.com/watch?v=M2HLGZJi0Hk
You can try this:
Main PHP file
<?php
// change path/to/php according to how your system is setup
// examples: /usr/bin/php or /opt/lampp/bin/php
echo shell_exec("/path/to/php /path/to/php_script/script.php");
echo "<br/>Awesome!!!"
?>
Secondary PHP file
<?php
echo "Hello World!";
?>
Output when running Main PHP file
Hello World!
Awesome!!!
Hope it helps you.
Try this:
<?php
// change path/to/php according to how your system is setup
// examples: /usr/bin/php or /opt/lampp/bin/php
$output = shell_exec("/path/to/php /path/to/php_script/script.php");
print_r($output);
?>
This May Help you.
It's important to stress that including/executing user-generated code is dangerous. Using a system call (exec, shell_exec, system) instead of include helps separate the execution context, but it's not much safer. Consider proper sanitation or sand-boxing.
With that in mind, here is a working example including generating the (temporary) file, executing it, and cleanup:
<?php
// test content
$code = <<<PHP
echo "test";
PHP;
// create temporary file
$d=rand();
$myfile="$d.php";
file_put_contents($myfile,"<?php\n$code\n?>");
// start capture output
ob_start();
// include generate file
// NOTE: user-provided code is unsafe, they could e.g. replace this file.
include($myfile);
// get capture output
$result = ob_get_clean();
// remove temporary file
unlink($myfile);
// output result
echo "================\n" . $result . "\n================\n" ;
Output:
================
test
================
terminal view:
abgth#ubuntu:/var/www$ cat > test.php
<?php
echo shell_exec("php5 /var/www/check.php");
?>
abgth#ubuntu:/var/www$ cat > check.php
<?php
echo 'hi';
?>
abgth#ubuntu:/var/www$ php5 test.php
hi
I hope you are looking for this
Go to terminal and type
php filename.php
filename.php should be the name of file you want to execute!
I need to include the output/result of a PHP file in another file.
I found info online about using curl to do this, but it doesn't seem to work so well, and so efficiently.
This is my current code:
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://domain.com/file.php";
$output = curl_load($url);
echo "output={$output}";
Any recommendations on what I can use to make this more efficient/work better?
I'm looking for whichever method would be the fastest and most efficient, since I have a bunch of connections/users that will be using this file constantly to get updated information.
Thanks!
file_get_contents() may suitable for you
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
you can also use the file() or fopen()
$homepage = file('http://www.example.com/');
$homepage = fopen("http://www.example.com/", "r");
I ended up going with a PHP include statement, to include the other file.
I had forgotten to mention that the file was local, and this seems to make the most sense at this point - instead of echoing the result in the other PHP file, I'm just setting the result as a variable, and then pulling that variable in my other file.
how can i get php file contents with php or js file from other server
with allow_url_fopen ON ?
i try this first - ITS WORK but from my server !
<?php
$url = "index.php";
$json = file_get_contents($url);
echo $json;
echo 1;
?>
i its not work
<?php
$url = "http://mysite.com/index.php";
$json = file_get_contents($url);
echo $json;
echo 1;
?>
anyone know how get file content with allow_url_fopen from other server?
or other user from my server?
or get variable from that file?
How a file is opened depends on the stream wrapper, which depends on the full URL.
file_get_contents('index.php') defaults to the local file system wrapper, i.e. it opens the file directly from the hard disk and reads its contents.
file_get_contents('http://example.com/index.php') uses the HTTP stream wrapper. You cannot access the file on the hard disk of another server directly (hopefully you can't). Opening http://example.com/index.php is the same as putting that URL into your browser: it makes an HTTP request to that URL and the result is whatever the other server outputs after processing the PHP file, like with any regular web request.
This might sound really "nooby" but I need to find a way for PHP to download an XLS file to a server folder. This file is not stored in another server, it is dynamically generated with another PHP script.
This is what I got from browsing the web but it's not working:
<?php
$url = "http://localhost/ProyectoAdmin/admin/export_to_excel.php?id=1&searchtype_id=2";
$local_file_path = './xls_tmp/Report.xls';
$xlsFile = file_get_contents($url);
file_put_contents($file_path,$xlsFile);
?>
I'd really appreciate any hint.
You're missing an end quote on your second line.
It should be: $local_file_path = './xls_tmp/Report.xls';
I have a php-script that uploads files to an ftp server from a location with a very low bandwith, low reliability connection. I am currently using the ftp functions of php.
Sometimes the connection drops in the middle of a transfer. Is there a way to later resume the upload? How can this be done?
Edit:
People misunderstood that this is happening from a browser. However, this is not the case. It is a php cli script. So it is about a local file being uploaded to ftp, no user interaction.
Try getting the remote file's size and then issuing the APPE ftp command with the difference. This will append to the file. See http://webmasterworld.com/forum88/4703.htm for an example. If you want real control over this, I recommend using PHP's cURL functions.
I think all the previous answers are wrong. The PHP manage the resume, for that FTP_AUTODESK must be activated, and you have to activate the FTP_AUTORESUME during upload. The code should be something like this :
$ftp_connect = ftp_connect($host, $port);
ftp_set_option ($ftp_connect, FTP_AUTOSEEK, TRUE);
$stream = fopen($local_file, 'r');
$upload = ftp_fput($ftp_connect, $file_on_ftp, $stream, FTP_BINARY, FTP_AUTORESUME);
fclose($stream);
ftp_(f)put has a $startpos parameter. You should be able to do what you need using this parameter. (starts the transfer from the file size on your server).
However I never used it, so I don't know about the reliability. You should try.
EDIT:
Another Example: by cballou
Look here. A very simple and good example.
You could do something like this:
<?php
$host = 'your.host.name';
$user = 'user';
$passwd = 'yourpasswd';
$ftp_stream = ftp_connect($host);
//EDIT
ftp_set_option($ftp_stream, FTP_AUTOSEEK, 1);
if ( ftp_login($ftp_stream,$user,$passwd) ){
//activate passive mode
//ftp_pasv($ftp_stream, TRUE);
$ret = ftp_nb_put($ftp_stream, $remotefile, $localfile, FTP_BINARY,FTP_AUTORESUME);
while ( FTP_MOREDATA == $ret ) {
// continue transfer
$ret = ftp_nb_continue($ftp_stream);
}
if (FTP_FINISHED !== $ret){
echo 'Failure occured on transfer ...';
}
}
else {
print "FAILURE while login ...";
}
//free
ftp_close($ftp_stream);
?>
are you meaning you are going to allow user resume upload from you web interface ?
is this suit you ?
http://www.webmasterworld.com/php/3931706.htm
just use the filezilla can done everything for you ...i mean the uploading process...
it will keep the data and reopen after close you will be able to continue process q...
for automate and tracking , simply use our way...which is git and capistrano
there is an option for php
try google it...