2

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?

Monacco Franze
  • 1,541
  • 1
  • 11
  • 12

3 Answers3

1

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) . "'>";

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • 1
    Nice :) check out my answer too ! – m0bi5 Feb 06 '15 at 17:50
  • @MonaccoFranze You're welcome! Glade that i could help you :D – Rizier123 Feb 06 '15 at 18:03
  • Sorry to bother you again, I have one last question. When i run the script, I don't see anything, butIn the source code, everything is as it should be. "" I have modified the script to add

    and

    to every path, but they still don't show up. Could you tell me what I am doing wrong? I'll try to fix it as far as I can by myself.
    – Monacco Franze Feb 06 '15 at 18:14
  • When I paste the echo'ed sourcecode from the page into a seperate php file and open it, it works. I guess that's just the way to go? – Monacco Franze Feb 06 '15 at 18:18
  • @MonaccoFranze This would only work for you. You have to use a path from your server. (e.g. `../../path/image.png`) – Rizier123 Feb 06 '15 at 18:19
  • I now put the images in my htdocs folder and changed the paths accordingly, but the pictures still won't show up. After I execute the php, i still get a blank page with source code looking like this: "

    It's not about user rights, because I can access the files directly (via link on server) from my other computer.
    – Monacco Franze Feb 06 '15 at 18:34
  • @MonaccoFranze Where is your script in `htdocs` located? (Also for your information why you can't do this: http://stackoverflow.com/q/4090712/3933332) And if your script is located e.g. in `htdocs/xy/` then your path has to be: `../DATABASE_PICTURES/P123.JPG` – Rizier123 Feb 06 '15 at 18:35
  • It's in the main htdocs folder. I modified the script to fit my paths : "; ?> – Monacco Franze Feb 06 '15 at 18:39
  • @MonaccoFranze Okay if your script is in the root folder use this: `$lines = file("results/result.csv");` And for the paths use this: `DATABASE_PICTURES/P123.JPG` and so on – Rizier123 Feb 06 '15 at 18:40
  • 1
    That did the trick, thank you for taking the time! If you pm me your paypal adress, I'll treat you to a beer (except if you are from Denmark, then half a beer has to do it) – Monacco Franze Feb 06 '15 at 18:47
  • @MonaccoFranze Nice, i'm happy that i could help you :D This is better then a beer – Rizier123 Feb 06 '15 at 18:49
  • @MonaccoFranze Canadian beer is apparently just as strong ;-) but I for one don't touch the stuff, but a double Espress would be nice. *Cheers!* – Funk Forty Niner Feb 06 '15 at 19:18
  • @MonaccoFranze One thing though. Seeing `C:/xampp/htdocs/DATABASE_PICTURES/` if you're using your text file with what seems to be DB-related, why use a text file in the first place; why not just use a DB exclusively? It's a lot less work than a text file and you'd need to protect that text file through `.htaccess` and not let anyone get a hold of it. – Funk Forty Niner Feb 06 '15 at 19:22
  • The text file gets automatically generated by a software that biometrically compares pictures, it contains the path and a percentage value. I then automatically sort the rows according to the percentage value, then cut the values out and also cut out the unnecessary parts of the path.(with a script) I thought about using a database, but as I have no experience at all with sql or anything like that, I'll stick with this method for now. When my prototype is running exactly as i want it to, i might switch. – Monacco Franze Feb 06 '15 at 19:29
  • @MonaccoFranze I understand. I too used to work with text files about 3-4 years ago, till I dived into and took the initiative to learn SQL. Once you've gotten a good grasp of it, you'd of wanted to have learned it a long time ago; as I did also ;-) – Funk Forty Niner Feb 06 '15 at 20:18
0

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 :)

m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • 1
    Sure, this would work, but the idea is to read from a file and not from a folder. *"I'd like to show the pictures from the text file on a website."* – Funk Forty Niner Feb 06 '15 at 17:52
0

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.
}
mineroot
  • 1,607
  • 16
  • 23