Solved

want foldername and full filename in the hyperlink

Posted on 2013-11-07
9
196 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 42

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 108

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 42

Expert Comment

by:Chris Stanyon
ID: 39629893
Suggestion from the voice of experience... You often don't have the choice ;)
0
 
LVL 108

Expert Comment

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

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 108

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 108

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now