Use search output string to open a local network file - php

I have been trying for ages with all types of "file, file_get_contents, fopen, opendir and etc" to acomplish what I am triying to do, but just no can do for me, this goes beyong my understanding, sadly. But here I am to learn.
What I want to do? I work with LucidWorks, and I have built an Intranet search that searches the specific path given "C://example/example/..." and does a full text search through all the files. The output of the search on my intranet website is simple:
Document title
Body title with highlighted keyowrds
Path to the file
Now, that not being enough, my lazy fellow Companions would like to be able to click the Document title(which does indeed have a full path to the document behind it, just so you can picture it better "C:/Ex/ex/ex/docs/sap/text.txt(or any other termination)) and open it locally.
Here is the part of the code that I believe to be relevant for what I am trying to acomplish. The "solution" i have built in does not work, but it may give you an idea of what I am trying to accomplish here.
$searchParams = array(
'hl' => 'true',
'hl.fl' => 'body'
);
$response = $LWS->search($query, $offset, $limit, $searchParams);
if ($response->response->numFound > 0) {
foreach ($response->response->docs as $doc) {
?>
<div id="resultbox">
<span id="resulttitle"> <?php echo "<a href={$doc->id}'>{$doc->title}</a><br />"; ?> </span>
<?php
$content = (("{$doc->id}'>{$doc->title}"));
print_r( '<a href= ' . fopen(str_replace('%', ' ', $content), "r+") . '>Open File</a><br />');
?>
<SPAN ID="copytext" >
<?php echo substr($content, -100); ?>
<br></SPAN>
<div id="sbody">
<?php
echo "..." . $response->highlighting->{$doc->id}->body[0] . "...<br /><br />";
}
echo '<br />';
return;
} else {
echo "No results for \"" . $query . "\"";
return;
}
?>
There is a little bit more code above it, but it's irrelevant for the asked question.
So there you go folks, I am hoping for help, and to be able to learn something new :)

It looks as though you're trying to put the contents of the files into the href attribute of you anchor/link (<a>) tag.
What you need to be doing, instead, is using a url which links to the specified file. This could possibly look like this in your implementation;
print_r('Open File<br />');
and the output would look something like;
Open File<br />

Related

echo content data from url source

Hey guys I'm not too knowledgeable about this particular topic in PHP yet. Basically I wanted to get a certain content of the url source so for instance the code below will only echo that specific content from the source page. I wanted to do this for other websites and the script below has errors but that's just like a demo of what I want to accomplished.
<?php
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
$data = getBetween($data,'<p class="c"> <font size="+2">',"</font></p>");
echo $data;
?>
All the information of the script above is located here
Use Simple HTML DOM to do this.
Read the manual to do this from here.
Its pretty simple.
//include simple_html_dom.php file.
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.jokesclean.com/OneLiner/Random/');
foreach($html->find('p[class=c]') as $e)
echo $e;
Just tested on my local system and it worked perfectly generating a random joke everytime i refresh
here's what i got on last refresh of this code.
.
It'd be best to use domdocument but you can also do it using regular expressions like the following.
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
if ( preg_match('/\<font size="\+2"\>(.*?)\<\/font\>/', $data, $match) ) {
echo $match['1'];
}
else {
echo 'couldn\'t find a match';
}

PHP script inside of string?

Is this possible?
What I am trying to accomplish:
Create a html template in the form of a string
Inside the string, add a script, something like include 'php/countries.php';
Echo entire string to html page
Everything but the 2nd step works. I would like to see a php file echo the question being asked, onto the html page, including an echo from another php file.
EXAMPLE
echo "<div id=\"first\"><?php include \'countries.php\'; ?></div>";
I have tried the above, as well as the below:
EXAMPLE
echo "<div id=\"first\">".include 'countries.php'."</div>";
Would this require eval?
Any and all help is appreciated.
Seems a bit silly, but you could do the following:
echo "<div id=\"first\">" . file_get_contents('countries.php') . "</div>";
Or...
echo "<div id=\"first\">";
include "countries.php";
echo "</div>";
Or...
$externalfile = compileexternal('countries.php');
function compileexternal($file) {
ob_start();
require $file;
return ob_get_clean();
}
echo "<div id=\"first\">" . $externalfile . "</div>";
If none of these are what you need, please update the question. There are a dozen ways.
You can use
eval()
But it is not a good practice.
You can use a regular expression.
For example, your string could be;
<div id="first">{{countries.php}}</div>
You'd then do;
$string = "<div id='first'>{{test2.php}}</div>";
echo preg_replace_callback("/(\{\{.+\}\})/", function($matches) {
include_once( str_replace(array("{", "}"), "", $matches[0]));
}, $string);
Check the file exists if( file_exists() )
Check the file can be included (we don't want to include ../../../../../etc/passwd

unable to show image from external hd php

I'm new with programming and learning now php. I have installed xamp and have write a little bit code. I have some images on my external hd and I want to show some pictures.
The problem is I can get a list of path of my images with this code:
echo "<html><body>";
$outerDir = "x:\map\maps\more\\";
$total = count (array_diff (scandir ($outerDir), Array (".", "..")));
$dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
foreach ($dirs as $d) {
if (!is_dir($outerDir . $d)) {
echo $d . "<br>";
}
}
but if I want to show it as an image, i can't show the image. I tried this in the foreach:
if (!is_dir($outerDir . $d)) {
$file = $outerhDir . $d;
echo 'img src="' . $outerDir . $d . '> <br>';
echo "<img src='" . $outerDir . '\\' . $d . "'alt='" . $d . "'> <br>";
}
But none of them work. I also tried the header and file_get_content like this:
if (!is_dir($outerDir . $d)) {
$file = $outerDir . $d;
header('Content-type: image/jpeg');
header('Content-Length:' . filesize($file));
$image = file_get_contents('$file');
echo $image . "<br>";
}
Even a simple html code wont work! Like this:
echo '<img src="x:\map\maps\more\needwonder.jpg"> <br>';
BUTTTT!! When I store one of the image in the same folder as my php file it will work! Like this:
echo '<img src="needwonder.jpg"> <br>';
But I dont want to put all my files in the same folder as the php file, because of security, technicaly, comfortable reasons AND I want to learn programming not to avoid issues or problems with my code.
So I hope I defined my problem good and hope that one of you guys and/or girls know the solution to help me out
If your image files aren't in a folder that's set up to be accessible by the web server, you can't display them on a web page. Simple as that. Put them in a web directory to use them.
It is possible to write a PHP script that proxies image files (kind of like what you're trying with file_get_contents()), but doing that requires a separate PHP file for the images, and it's best avoided unless absolutely necessary, as it can easily introduce security vulnerabilities, and performs poorly. If you do want to do that, though:
Link to the PHP script in place of an image, and pass it an identifier for the image as a parameter (e.g, <img src="img.php?image=blah">).
In that script, use that identifier to send the appropriate Content-Type, then use readfile() to output the image. (It's equivalent to echo file_get_contents(...), but more concise and performs better in some situations.)
MAKE ABSOLUTELY SURE that the parameter to that script cannot be manipulated to load a file that you didn't intend to make available. This includes trickery like ?image=../../wrong/file.

Crafting HTML elements from dynamic PHP data

I apologise for the potentially nebulous title, but I hope that the question will clarify why I've called it that.
I've gotten into webdev from UI design/dev so I'm finding the programming and design aspects fairly easy to handle. However, I'm not used to using three/four different languages with a fair amount of overlap to do a single thing.
In this case, I've written up a PHP script to pull the latest $n tweets from $x user. Currently I'm loading the Twitter PHP via jQuery, crafting the HTML for the tweet in PHP, echoing in the PHP, this is then plopped into a timeline element in the jQuery's callback function.
This way of doing it seems a bit hacky? I was just hoping someone would be able to point me towards another way, or at least waylay my concerns regarding this method.
Here's some Code:
HTML:
<section id="twitterContainer">
<section id="glitchTimeline" class="timeline">
<!--Twitter articles go here-->
</section>
</section>
Javascript:
function getTweets(n,user)
{
//This is run when the page is loaded
//Will be changing this to an ajax call later, aware that it's a bit silly as is.
$('<article class="twitterPost">').load("http://xxxxxxxxx.com/php/index.php?user="+user+"&count="+n, function(data){
$(this).appendTo("#glitchTimeline");
});
}
PHP:
(This is called once the twitter API stuff has come back)
function craftTweet($jsonStr)
{
$json = json_decode($jsonStr);
for($i = 0; $i<count($json); $i++)
{
echo '<img class="twitterAvatar" src=' . $json[$i]->user->profile_image_url .' alt=' . $json[$i]->user->screen_name .' width="48">';
echo '<div class="tweetText">';
echo ' <b>' . $json[$i]->user->screen_name .'</b><span>#' . $json[$i]->user->screen_name . '</span><span> </span>';
echo ' <p>' . $json[$i]->text . '</p>';
echo ' <img class="twitterIconFav" src="img/utils/transBackground.png">';
echo ' <img class="twitterIconRetweet" src="img/utils/transBackground.png">';
echo ' <img class="twitterIconReply" src="img/utils/transBackground.png">';
echo ' <div class="clearfix"> </div>';
echo '</div>';
}
}

Adding A Dynamic Link In Php

I have been using the following to add a dynamic link on a page I am writing, it works ok and appears how it should on the page but I cant help but think that I am going a bit backwards with the way its written as it looks messy. What is the correct way to write it, as if I put it all in one line it doesn't work ?..
echo '<a href="./customer-files/';
echo $customerID;
echo '/';
echo $filename->getFilename();
echo '">';
echo $filename->getFilename();
echo '</a>';
Try with
echo "{$filename->getFilename()}";
Here there is the documentation with a lot of examples of how to concatenate output.
I'd approach it like this:
$safe_customer_id = htmlspecialchars(urlencode($customerID));
$safe_filename = htmlspecialchars(urlencode($filename->getFilename()));
$safe_label = htmlspecialchars($filename->getFilename());
echo "$safe_label";
I would go with this:
$fn = $filename->getFilename();
$link = $customerID . '/' . $fn;
echo ''.$fn.'';
If you're using a template layer, it is even better to break out into PHP only when you need to:
<a href="./customer-files/<?php
echo $customerID . '/' . $filename->getFilename()
?>">
<?php echo $filename->getFilename() ?>
</a>
This way, your IDE will correctly highlight your HTML as well as your PHP. I've also ensured that all PHP is in single-line blobs, which is the best approach for templates (lengthy statements should be banished to a controller/script).
Concatenation is your friend. Use a . to combine multiple string expression into one.
echo ''.$filename->getFilename()/'';
Even better way would be
$filename = $filename -> getFilename(); //cache the filename
echo "<a href='/$customerId/$filename'>$filename</a>";
// ^ On this echo NOTICE that variables can be DIRECTLY placed inside Double qoutes.

Categories