0

Im trying to display the contents of the text file with no luck. I have tried php which looked like an easy way to do it with no luck

base.html

{% load i18n static %}

<!DOCTYPE html>
<html>
{% load static %}
<head>
    <head lang=en>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

</head>
<body>
    <div >
            <div class="jumbotron" style="background-color: white; padding: 1.5rem;margin-bottom:i 100.5rem">
                    <div class="row">
                            <div class="col-md-3"><a href="/"><img src="{% static 'css/images/hadoop.png' %}"</a></div>
                            <div class="col-md-6">
                                    <h2>Impala Query Metrics</h2>
                                    <hr class="my-2" href="{% url 'impala' %}">

                            </div>

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$f = fopen("pathtofile/test.txt", "r");
// Read line from the text file and write the contents to the client
echo fgets($f);
fclose($f);
?>

I have also tried

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
readfile("pathtofile/test.txt");
?>

Also:

<?php
include("pathtofile/test.txt");
?>

Im not getting any errors nor seeing any results

TheNewGuy
  • 559
  • 1
  • 10
  • 27
  • Are you getting any errors? Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Feb 27 '18 at 15:52
  • I'm not seeing any errors – TheNewGuy Feb 27 '18 at 15:54
  • https://stackoverflow.com/questions/9539849/how-to-echo-the-whole-content-of-an-html-file-in-php – nullqube Feb 27 '18 at 15:55
  • @nullqube that did not work for me unfortunately – TheNewGuy Feb 27 '18 at 16:02
  • I'm curious why this is tagged with "php" *and* "django". Are you using PHP or Python? – Goldentoa11 Feb 27 '18 at 16:16
  • @Goldentoa11 im using python. Should I still be able to use php in my base.html ? This is my first go at php – TheNewGuy Feb 27 '18 at 16:18
  • Ahh, okay. There are a few issues with that, which I'll try to point out in an answer. It may well still be doable, though I would question whether you should. – Goldentoa11 Feb 27 '18 at 16:18
  • your code doesn't quite makes sense to me, as other mentioned django is python and if you want to use php the solution would be a dedicated php page for that matter that show the content of the file with any structure you want but fully in php. since you named your file .html I think the problem is it count as a broken html and neither django nor php can recognize it . – nullqube Feb 27 '18 at 16:21
  • would you like me to give a snippet for that in python ? – nullqube Feb 27 '18 at 16:24
  • @nullqube sure, thank you – TheNewGuy Feb 27 '18 at 16:39

2 Answers2

0

There are a few issues here; hopefully this provides some thoughts on where to go.

First, it appears that you want to run PHP code inside an HTML file, as the name of the file is base.html. The PHP interpreter is not configured (by default) to allow this, as it will only run files with the php extension. The fix for this would be to tell your web server to associate html files with the application/x-httpd-php mime type, so html files are ran through the php interpreter.

Second, I noticed the question is tagged with django, which is a Python framework. Do you have a PHP interpreter installed on the server? In order to execute PHP code, you'll need a PHP interpreter, which Python is not. To fix this, you would need to install a PHP interpreter (https://secure.php.net/ - right sidebar)

Third, should you really do this? Python and PHP both serve the same need, to provide server side processing to a web site. I would argue that you should use one or the other, but not both. Since you're already running Django, I would advise that you look for the Python-esque way of accomplishing a file include.

Goldentoa11
  • 1,700
  • 2
  • 17
  • 29
  • Here's an answer showing how to print a file in python. https://stackoverflow.com/questions/18256363/how-do-i-print-the-content-of-a-txt-file-in-python – Goldentoa11 Feb 27 '18 at 16:27
  • Great. This makes much sense thank you for the explanation. I will try to find something with python – TheNewGuy Feb 27 '18 at 16:34
0

assuming your file is text-file and you want the a html file that your data wrapped inside a div or so then : I'm not a python fan but for python some links , not exactly same title but may good for beginning:

https://python-forum.io/Thread-read-text-file-using-python-and-display-its-output-to-html-using-django

Django displaying upload file content

https://djangobook.com/generating-non-html-content/

or in php: 0.php

<!DOCTYPE html>
<html>
<body>
 <div class="content-wrapper">
  <?php
   echo file_get_contents( "YOURFILE" ); // get the contents, and echo it out.
  ?>
 </div>
</body>
</html>
nullqube
  • 2,959
  • 19
  • 18
  • did u save it with an .php extension? and by the way how do address this file, by a link on some page or what? – nullqube Feb 27 '18 at 17:10
  • i have alot more going on in base.html its a local file on linux. I dont think i can just rename base.html to php since its a template – TheNewGuy Feb 27 '18 at 17:11
  • no then you can not use php if it goes through Django it's python. – nullqube Feb 27 '18 at 17:18