Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

want foldername and full filename in the hyperlink

Posted on 2013-11-07
9
Medium Priority
?
215 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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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 111

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 44

Expert Comment

by:Chris Stanyon
ID: 39629893
Suggestion from the voice of experience... You often don't have the choice ;)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

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 111

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 111

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

670 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