Solved

want foldername and full filename in the hyperlink

Posted on 2013-11-07
9
213 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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