Embedding contents of a folder in a web page - php

I'm building a super simple CMS in PHP, and have what's probably a simple question...
I'm storing my news in .txt files (I know it should be a database, I'm just not focusing on that stuff for now...) and need to display these files on a page (news.php). It should basically print the contents of each file in reverse alphabetical order, for example:
Files:
12-11-11.txt
12-12-11.txt
12-13-11.txt
12-14-11.txt
Display:
Contents of 12-14-11.txt
Contents of 12-13-11.txt
Contents of 12-12-11.txt
Contents of 12-11-11.txt
I'd like to wrap each post in something like <div class="article"></div>.
I'm very new to PHP so a good code example would be lovely. I'm storing my files like this:
news page: /news.php
news files: /edit/news/

foreach (glob("/edit/news/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}

Related

How to read my php/txt file content which is stored in databases(in one of it's columns) and display it?

I have a search bar on my home page through which I am trying to show the contents of php/txt file present in info column of my database .
//this is from home page
if($khoj!=""){
$query=mysql_query("SELECT * from company_data where website like '%".$khoj."%' ");
$n=mysql_num_rows($query);
$row=mysql_fetch_assoc($query);
header("refresh:1;url=cbresult.php");
$_SESSION['website']=$website;
$_SESSION['info']=$row['info'];
}
//2.cbresult.php(other code)
<?php
session_start();
include 'database2.php';
echo $_SESSION['website']."<br>".$_SESSION['info'];//instead of $_SESSION['info'] ,what should be present?
?>
Showing the contents of a file from the info column.
$file = file_get_contents($row['info']);
echo $file;
This needs to be the path+filename to file if its not in the same directory.
Not sure exactly what you mean by php/txt, so...
If the file has PHP code in it that you want to be executed, you should include it like you did with that other database file.
include($_SESSION['info']);
If it's just plain text, you can use readfile.
readfile($_SESSION['info']);

File structure in database

I'm making a simple file listing function in PHP, just to recursively list all the files in a directory.
The function I have right now looks something like this:
<?php
function listFiles($dir){
echo "<ul class=\"filebrowser\">";
$dirCont = scandir($dir); // scan the whole directory
sortFoldersFirst($dirCont,$dir); // sort alphabetically, folders first
foreach ($dirCont as $value) {
if (!in_array($value,array(".","..",".deleted"))){ // don't list the parent folder, current folder, or the (hidden) .deleted folder
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){ // if it's a folder
echo "<h3 class=\"filebrowser\">" . $value . "</h3>"; // show the folder name
listFiles($dir . DIRECTORY_SEPARATOR . $value); // do the same again for the child folder
} else {
echo "<li>".$value."</li>"; // a link to the file
}
}
}
echo "</ul>";
}
?>
This gives me something like this:
This technique works just fine, however, I'd like to store a little more information about the files, like the author, comments, maybe a link to a thumbnail etc.
I'd like to get rid of the 'real' directory structure, and store everything in a database. (Just all files in 1 big folder, and links to the files in the database, not the actual files)
How can I store this information in the database, and how can I get it back out, without changing the result in the browser.
In other words: how can I get the tree structure in and out of the database, and how do I recursively loop through it.
I found this answer.
It shows how to make a database tree structure by saving the parent folder's id for every file/folder. This makes it easy to get the full path of a file, but it seems a bit less convenient to list all contents of a folder.
Is there a better solution to this problem?
Thanks in advance,
Pieter

yii rendering images stored in images folder in final view

In my web application I need to display the images uploaded when creating in the view. The uploaded images are stored in /images/xxx folder . I want in such a way that as soon as the user uploads the image, it should appear in the final view. I know we should create an array reference pointing to the images/xxx folder in config/main.php . But I dont know how to refer to the individual images in the folder and print them in the final view..
My code for params in config/main.php
'params'=>array(
'adminEmail'=>'webmaster#example.com',
'vegImageLoc'=>'images/xxx',
),
);
I dont know how to refer to the individual array elements in the final view and print them in the final view. Any body help me how to proceed.
I dont know why you think you need to change anything in config, maybe I'm missing something, but if you want to show all images in a folder like images/xxx , I recommend to do this :
foreach (glob("images/xxx/*") as $img) {
echo "<img src ='".Yii::app()->request->baseUrl."/".$img."' />";
}
Or if you want to get the images from database, get it and again make a foreach like above.
$image = $model->getImages(); // function to get image list stored in db. return as array
Pass $image to view file.
In your view file. you can do as below:
foreach ($image as $img) {
echo "<img src ='".Yii::app()->request->baseUrl."/".Yii::app()->params["vegImageLoc"]."/".$img."' />";
}

PHP txt File Manipulations

I'm trying to create a simple one page php website that does the following.
Displays data from a txt file and then converts it into links.
(A line from the text file would be so if the line is "TextEdit: 1.9" ApplicationName = TextEdit)
So basically I need to add the url strings together with the application names in the desired locations and then redirect to that page. Hopefully ending up with something like:
Link[urlportion+appname+urlportion]
I am getting my data from a txt file that is formatted in the format of:
TextEdit: 1.9
AppleScript Editor: 2.6
Arduino: 1.0.5
TextWrangler: 4.5.3
etc.
I can display the code in my page using:
<?php
foreach(glob("log.txt") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode("<br>",$contents);
echo $string;
echo "<br></br>";
}
?>
That works beautifully my question is how do I separate the txt file into pieces so I can concatenate it with my url and display it.
If it's simple you can just use a text file and delimit your strings. You're going to need a server-side technology to do this - PHP, ASP.NET, etc. You haven't posted what your server-side technology is, but this can't be done without it.

pass mysql value from php page to a joomla article and display relevant data inside of a joomla article

we are developing an application.website is being developed by joomla.Admin panel is being developed using a pure php.on index page(joomla), we are displaying some details from the backend.
my question is this, when we click on one of the records on that page can we display the relevant data inside of a article?
Hope i asked the question clearly.
please share your thoughts with us.
thanks in advance
Yes, you can do this, if I understand your question correctly.
Open up Joomla's main index.php. This is the index.php in the html root, not the index.php in one of the template folders.
Near the bottom of the file, or maybe the very last line you will see something like this:
// Return the response.
echo $app
Replace this line with the following:
// Return the response.
// parse $app for server side includes statements and execute them
// note: this will only work for executable code, it will not import text or html files
// we would need to check to see if the file were executable, then read it rather than execute it if it were not
$output = $app;
while(ereg('(<!--#include virtual="([^&]+)" -->)',$output,$groups)){ // extract the ssi command and the command
$i = 0;
while(!$inline){ // sometimes exec() fails for want of memory so we try a few times
exec($groups[2],$array); // get the output from the command
foreach ($array as $element) // concatenate the lines of output into a single string
$inline = $inline . $element . "\n"; // appending a new line makes the html source more readable
$i++;
if($inline | $i > 5)
break;
sleep(1);
}
$output = ereg_replace($groups[1],$inline,$output); // replace the ssi command with the output
}
echo $output;
This will allow you to place a standard server side includes statement in your article. Fore example if you want to execute a php file in the same directory as your index.php and the file is called dynamic_content.php you would type this in your article:
<!--#include virtual="dynamic_content.php"-->
The output of that script will then be included in the text of the article. You can have multiple ssi commands in the same article.

Categories