I have a text file with 10 paths to files saved on my server, that looks like this:
C:\pictures\123.jpg
C:\pictures\124.jpg
C:\pictures\125.jpg
I'd like to show the pictures from the text file on a website. I can't directly put the links into a php script because the file is dynamically generated and has different paths everytime.
Any ideas on how to do this?
This should work for you:
(Here i use file() to get all lines of the file in an array. After this i simple loop through them and print it as an image path)
<?php
$lines = file("test.txt");
foreach($lines as $line)
echo "<img src='" . trim($line) . "'>";
?>
You can try and draw a neat table of images from your file .
NOTE: I am assuming all your files in the folder are images.
$root = 'C:\pictures';
$files = scandir( $directory );
echo '<table>';
foreach( $files as $file )
{
if(is_file($file))
{
echo '<tr><td><img src="' . $root . $file . '" /></td><td>' . $file . '</td></tr>';
}
}
echo '</table>
Let me know if I helped :)
I don't have opportunity to test this. But I hope this help.
$handle = fopen("images.txt", "r");
if ($handle) {
while (($pathToImg = fgets($handle)) !== false) {
echo "<img src='".$pathToImg."'/>";
}
fclose($handle);
} else {
// error opening the file.
}
Related
How to write all the txt files contents in a folder to an html page, besides i don't know the files names, i tried this but it didn't work:
<?php
$files=scandir("D:\\new\places");
foreach (files as value)
echo (files);
?>
you can use following php code to get content of the files.
$path='../innovation';
$files=scandir($path);
//print_r($files);
foreach ($files as $key => $value) {
if($value!="." && $value!="..")
{
print_r(file_get_contents($path."/".$value));
}
}
readfile is ideal for this, since it streams the files directly to the output buffer, which will avoid memory issues for large files.
<?php
$path = 'myDirectory';
$files = glob("{$path}/*.txt");
foreach ($files as $file)
{
readfile($file);
}
If you want to list all the text files (.txt) from a directory to HTML using PHP you can use this code to do it:
<?php
echo '<ul>';
// set the path to the directory...
$dirname = "directory/";
$files = glob($dirname."*.txt");
foreach($files as $file) {
echo '<li>'.$file.'</li>';
}
echo '</ul>';
?>
You can try this:
$directory = "./foldername";
$files = glob($directory . "*.txt");
foreach($files as $file){
$file = fopen($directory . '/' . $file,'r');
while ($line = fgets($file)) {
echo($line);
}
fclose($fh);
}
Use glob() to get all *.txt files in a directory. After loop through every file with a foreach loop and get the content of each file using file_get_contents() and put the content into the target file with file_put_contents()
<?php
$files = glob("path/*.txt");
$output = "output.txt";
foreach($files as $file) {
$content = file_get_contents($file);
print_r($content);
}
?>
can anyone help me to get each file size individually from a local directory ?.
$files = scandir('soft');
foreach($files as $file) {
echo $file . "<br />";
}
From here
$files = scandir('soft');
foreach($files as $file) {
if (!in_array($file,array(".","..")))
{
echo $file . "<br />";
echo filesize('soft/'.$file) . ' bytes';
}
}
Just need to keep in mind that scandir gets only the filenames in that dir, and not the relative path to it. that's why you need to use 'soft/'.$file and not $file
<?
$files = scandir('.');
foreach($files as $file) {
echo filesize($file) . " bytes<br>";
}
?>
use filesize($filename) php function, it will give size in bytes
Can somebody help me? I have many pictures with current name like : "black_abc","black_bcd","black_cde","white_abc". How can I get only file with filename contains "Black"?
glob will help you find all files containing "Black" in their filename:
$folder = "images"; //the folder containing all your images
$pattern = "*Black*"; //the word you are looking for
$files = glob($folder. '/' . $pattern, GLOB_BRACE);
foreach($files as $filename) {
//Display all pictures
echo "<img src='"$folder . "/" . $filename . "' />";
}
strpos($filename, 'black') !== false
Something like this [Make use of stripos() [Case-Insensitive]
<?php
$files=array("black_1","white_2","black_3");
for($i=0;$i<count($files);$i++)
{
if(stripos($files[i],'black'))
{
echo "Filename is $files[$i]";
}
}
I'm trying to use PHP to bring in every image from a specific directory and turn them into HTML objects by wrapping some markup around them.
Right now I am just trying to get the images on the page. In the future I would also like to bring in a text file that has the same file name as the image(slide#4.jpg and slide#4.txt).
This is what I have so far.
<?php
$dir = "includes/pages/products/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
$file = basename($image);
$file = basename($image, ".jpg");
echo "<div class='"prod-cont"'>" . "<h4>" . $file . "</h4>" . "<img src'" . $image "' />" . "</div>";
endforeach;
?>
I'm not having much luck getting this going, any help is appreciated.
Remove the second $file = line, and make sure you're generating correct HTML.
echo "<div class='prod-cont'><h4>".$file."</h4><img src='".$image."' /></div>";
I have a number of text files held in directory
/results/...
All the text files are named with unixtime stamps, inside each of the following files there is:
#text¬test¬test1¬test2¬test3¬test4¬1262384177
Each piece of text is seperated by '¬'.
I'd then like to feed the contents of the text file into an array and output it, in for example a table, but for each of the files (Perhaps loop-like?)
If have this but it only works for one file and fixed file name:
$filename = "results/unixtime.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$array01 = explode("¬",$contents);
$count = count($array01);
echo "<table width = 500 border=1 cellpadding=4>";
$i=0;
for ($i=0;$i<$count;$i++) {
echo "<tr><td>";
echo $array01[$i];
echo "</td></tr>";
}
echo "</table>";
I suggest the fairly-unknown glob function to detect all your files. Then with all the filenames in a handy array, just iterate through and open up/read each one. Sort of like this:
$files = glob('*.txt');
while(list($i, $filename) = each($files)){
//what you have now
}
A couple of things:
Unless you're dealing with really large files just use file_get_contents() to load files. It's a one-liner versus three lines of code that you just don't need;
Loop over arrays using foreach unless you explicitly need a loop counter. The loop condnition/counter is just another area where you can make simple errors;
Use opendir(), readdir() and closedir() for reading directory contents; and
Directories will contain entries like "." and "..". Use filetype() and/or a check on the name and/or extension to limit it to the files you're interested in.
Example:
$directory = "results/";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if ($type == 'file') {
$contents = file_get_contents($filename);
$items = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
foreach ($items as $item) {
echo "<tr><td>$item</td></tr>\n";
}
echo '</table>';
}
}
closedir($dir);
You can get all the files located in "result" via opendir.
There is also an example ...
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
Grab the files in the directory and read each filename.
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$filename = $file;
//your code
}
}
closedir($handle);
}
?>
source: http://php.net/manual/en/function.readdir.php
Here is a more elegant way of writing brianreavis solution, also use file_get_contents instead of fopen, fread and fclose, it's faster and less verbose.
foreach (glob('*.txt') as $filename)
{
$contents = file_get_contents($filename);
}
Use this code, replace DOCROOT with directory you want to scan.
foreach (scandir(DOCROOT.'css') as $dir) {
echo $dir . "<br>";
echo file_get_contents(DOCROOT . 'css/' . $dir ) . "<hr />";
}