Solved

want foldername and full filename in the hyperlink

Posted on 2013-11-07
9
208 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
  • 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 109

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 109

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 109

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 109

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

830 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