I'm using PHP/MySQL.
I have a folder called "docs" with PDF files.
I'd like to know if there exist an easy way, using only PHP, to show the content of the folder with a custom icon.
I mean, something like this:
[icon] File01.pdf
[icon] File02.pdf
[icon] File03.pdf
In order to allow visitors read or download the files.
Many thanks in advance
With glob() you can get the path to all pdf files and then loop through them with a foreach loop.
foreach(glob('*.pdf') as $file) {
echo '<img src="myicon.png"> ' . $file;
}
You can use pathinfo to get the file extention and show and png with the extention
$fileExt = pathinfo($filename, PATHINFO_EXTENSION);
echo '<img src="path-to-icons/"' . strtolower($fileExt) . '.png" />' . $filename . ''
This was the final solution:
<?php
foreach(glob('docs/*.pdf') as $file) {
echo '' . substr($file, 5, -4) . '</br>';
}
?>
I have used #Karl's and Martin's answers to get the result I was looking for, but all answers were really useful.
Many thanks to all
Related
I am working on a website of a client for which I didn't write the code. I have troubles making files downloadable.
It is about a subdomain where users can download course files.
The website files are contained in the folder "courses" (on the root level).
The file for displaying the downloadable course files is contained in
"courses/displayfiles.php".
The downloadable files are contained in a folder in "courses/downloadfolder". Inside this folder, each user has his own
files folder which as its name has the user id.
displayfiles.php: The following code successfully displays all files that can be downloaded by the logged-in user:
$path = "downloadfolder/" . $_SESSION['userId'] . "/";
$files = array();
$output = #opendir($path) or die("$path could not be found");
while ($file = readdir($output)) {
if (($file != "..") and ($file != ".")) {
array_push($files, $file);
}
}
closedir($output);
sort($files);
foreach ($files as $file) {
echo '<a class="imtext" href="downloadfolder/' . $_SESSION['userId'] . '/' . $file . '/">' . $file . '</a><br/>';
}
So what does not work about this code: When a user clicks on a file, I get a "404 Not Found" message that the file was not found. How can this be?
Why does displaying the files totally works fine, but at the same time I get a 404 error when clicking a file? The files path ($path) must be correct, or not? What further investigations do I need to take in order to solve this problem?
* UPDATE *
I decided to modify the files loop as followed (changing the href):
foreach ($files as $file) {
echo '<a class="imtext" href="http://'.$_SERVER['HTTP_HOST']. '/downloadfolder/' . $_SESSION['courseId'] . '/' . $file . '/">' . $file . '</a><br/>';
}
Still, when I click on a file, I get a 404 Not Found error. How can this be?
You have to look where the webroot of your page is, where the php file generating the list is located and wherer the files are.
Your generated link is relative to the php file generating the link, which might not be corresponding to the URL in the browser. I'd try to make this link relative to the webroot (note the leading slash!)
echo '<a class="imtext" href="/courses/downloadfolder/' . $_SESSION['userId'] . '/' . $file . '/">' . $file . '</a><br/>';
If that guessed solution doesn't work please provide the current URL of the page where this links are generated and one generated link, so we can help you better.
I am reading a folder with glob() function in php
foreach (glob("folder/*.*") as $filename) {
echo $filename."<br />"
}
My folder contains three kind of files pdf, image, videos. What I need here is to make a link for each file and open them on click.
How can I achieve this, any ideas?
This should work for you:
(This creates you a list of all files which you can click on it and you get it opened in the new tab)
$files = glob("folder/*.*");
foreach($files as $file)
echo "<a href='" . $file . "' target='_blank'>" . $file . "</a><br />";
I want to show my visitors the images in a folder and after then have seen it, I want all those files deleted!
This is what I tried, but It won't work. I think it's because PHP is generating a html file which tells the browser it must first get an image from a different place but the html file was already removed.
<?php
foreach (glob("files/*.*") as $prevpic) {
echo '<img src="' . $prevpic . '" />';
}
foreach (glob("files/*.*") as $file) {
unlink($file);
}
move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"]);
?>
You can do something like so ...
<?php
foreach (glob("files/*.*") as $file) {
echo '<img src="data:image/' . pathinfo($file, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($file)) . '" />';
unlink($file);
}
?>
... which is basically writing the image data into the html, and then discarding the image.
I would handle this by simply managing your images through a download script (php).
You track sessions and simply don't display the images requested, but fail gracefully with a response, or let your app handle it via session based tracking.
That way no images are deleted 'onview'.
I'm trying to get a thumbnail to link to a PDF of the same name if the PDF exists, but to not link to anything if the PDF doesn't exist. Here's the code I have:
<?php
if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) {
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full') ;
$pdf = substr_replace($full_image_url , 'pdf', strrpos($full_image_url[0] , '.') +1);
$filename = $pdf[0];
if (file_exists($filename)) {
echo '<a href="' . $pdf[0] . '" title="' . the_title_attribute('echo=0') . '" . target="_blank" >';
the_post_thumbnail('Full Size');
echo '</a>';
}
else {
echo "The file $filename exists";
}
}
?>
Currently, the else statement is just to prove whether or not it's finding the file. Which it seems to, as it displays The file http://localhost/AWAD/wp-content/uploads/2012/03/+D.pdf exists. And if I get rid of the conditional, the post thumbnail displays with a link to the PDF. I just can't get the conditional to work.
Can anyone spot why it's not working?
You should pass a path on your FS to file_exists, you are passing an URL now
I'm pretty sure file_exists wants a full file path, not a URL. So, you'll probably want to use the WordPress wp_uploads_dir function to get the base path to your uploads directory and then append the rest of the path to the end of that and then pass that string to file_exists. Hopefully that makes sense.
I'm making a page with some flash games, but, since in the past will be many games it will be very bored to add all those games one by one on the site. I know that using PHP we can get the list of the files in the directory and I know that, using this method, I can display all the games on the directory to the visitors page but, I was thinking if you can help me to add an image to each game that corespond with the name of the game. Ex:
In "games" folder we will have games: play1.swf, play2.swf, play3.swf
and, in "images" directory we will have images: play1.jpg, play2.jpg, play3.jpg.
To display the list of the games, can we make a combination of jpg files with swf files? Play1.jpg will be the image of play1.swf game and, when a visitor clicks on the image, will be redirect to a page named ex: playgames.php?play1
assuming this directory structure:
/foo/images
game1.jpg
game2.jpg
/foo/games
game1.swf
game2.swf
Something like this:
<?php
$dir = "/foo";
$files = new DirectoryIterator($dir . DIRECTORY_SEPARATOR . 'games');
$games = array();
foreach($files as $file){
if (!$file->isDot()) {
$potential_image_location = $file->getPath() . '/../images/' . $file->getBasename('.swf') . '.jpg';
if (file_exists($potential_image_location)) {
$games[$file->getBaseName()] = array(
'game_path' => $file->getPathname(),
'image_path' => realpath($potential_image_location),
);
}
}
}
if (!count($games)) {
die("Sorry, couldn't find any game / image combos");
}
?>
<table>
<?php
foreach ($games as $game_name => $info) {
echo '<tr><td><img src="' . htmlentities($info['image_path']) . '" alt="' . htmlentities($game_name) . '"></td>' . PHP_EOL;
echo '<td>' . $info['game_path'] . '</td></tr>';
}
?>
</table>
its all about using opendir in a clever way, heres the docs on it http://php.net/manual/en/function.opendir.php