Solved

want foldername and full filename in the hyperlink

Posted on 2013-11-07
9
212 Views
Last Modified: 2013-11-18
Using php, I want to list all the filenames in a folder as hyperlinks


folder
<a href="file1.wmv" target="_blank">file1.wmv</a>
<a href="file2.wmv" target="_blank">file2.wmv</a>
<a href="file3.wmv" target="_blank">file3.wmv</a>

<?php 
$folder    = '.';
$files = scandir($folder);

foreach ($files as $file):
	printf("<p><a href='%s' target='_blank'>%s</p>",$file, $file);
endforeach;
?>

Open in new window


This code does not include foldername and does not work for filename with spaces (only includes the filename before the space





00 01.filename with spaces.wmv

00 02.another filename with spaces.wmv
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39629773
It will select all files and folders, but you will need to urlencode the href for the links to work:

<?php
$folder    = '.';
$files = scandir($folder);

foreach ($files as $file):
	printf("<p><a href='%s' target='_blank'>%s</p>",urlencode($file), $file);
endforeach;
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39629882
Suggestion from the voice of experience... Never leave a blank in a file name.  Use letters, numbers and the underscore, nothing else, and you will save yourself from a lot of strange "edge case" headaches!
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39629893
Suggestion from the voice of experience... You often don't have the choice ;)
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39630062
Yeah, that's part of what's wrong with the Obamacare web site!
0
 

Author Comment

by:rgb192
ID: 39630908
first.: I see
+ instead of space (which I think is %20%)


second:
the 'folder is not in the url'
which would force me to put the file in the directory

I want to put the file outside the directory
(I am not sure if I asked for this, so this may be another question.)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39630938
+ is a URL-Encoded space.  Can also be %20.

I may be able to give you a general purpose example later -- gotta run now.
0
 

Author Comment

by:rgb192
ID: 39647804
+ in browser url does not work for me
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39647811
Please post the SSCCE that demonstrates the issue, thanks.
0
 

Author Closing Comment

by:rgb192
ID: 39656740
Thanks for the url encode which is the requirement of the question.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question