?
Solved

include foldername in the href links

Posted on 2013-11-18
5
Medium Priority
?
327 Views
Last Modified: 2013-11-22
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>",urlencode($file), $file);
endforeach;
?>

Open in new window



I want code that is outside the folder.
www/file.php
www/folder/file1.wmv
www/folder/file2.wmv
www/folder/file3.wmv


the problem is that the links are
www/file1.wmv
www/file2.wmv
www/file3.wmv

which do not contain the wmv files because the wmv files are in the folder

I want
www/folder/file1.wmv
www/folder/file2.wmv
www/folder/file3.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
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 39656866
Is it that what you want ?

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

foreach ($files as $file){
$file="www/folder/".$file;
	printf("<p><a href='%s' target='_blank'>%s</p>",urlencode($file), $file);
}
?>

Open in new window

0
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 200 total points
ID: 39656887
you can use dirname(__FILE__) to display your existing full path name
and you change it around for your expect for $file

Hope it will help ;


<?php
chdir(dirname(__FILE__));
echo dirname(__FILE__)."==x=\n";
$folder    = '.';
$files = scandir($folder);

foreach ($files as $file){
$file=dirname(__FILE__)."/".$file;
	printf("<p><a href='%s' target='_blank'>%s</p>",urlencode($file), $file);
}
?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1800 total points
ID: 39656950
Obviously I am not going to leave this on my server, but you can install it and test it for yourself.  A sensible design might collect the list of directories and create a table with radio buttons to facilitate the client choice of the directory.  HTH, ~Ray

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);


// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297125.html
// http://www.php.net/manual/en/class.directoryiterator.php#88459
// PUT THIS SCRIPT IN THE WEB ROOT DIRECTORY


clearstatcache();

// IF THERE IS A GET-METHOD REQUEST
$dir = !empty($_GET['dir']) ? $_GET['dir'] : NULL;

if ($dir)
{
    $arr = scandir($dir);
    $hed = '<h3>' . $dir . '</h3>' . PHP_EOL;
    $dir .= DIRECTORY_SEPARATOR;
}
else
{
    $arr = scandir(getcwd());
    $hed = '<h3>WEB ROOT</h3>' . PHP_EOL;
}

foreach ($arr as $file)
{
    if ($file ==  '.') continue;
    if ($file == '..') continue;
    $url = $dir . $file;

    // SEE http://php.net/manual/en/function.is-file.php#107403
    if (!is_dir($url))
    $links[] = '<a target="_blank" href="' . $url . '">' . $file . '</a>' . PHP_EOL;
}

// SHOW THE LINKS
echo $hed;
foreach ($links as $link)
{
    echo "<br>$link";
}

// A FORM TO GET THE NAME OF THE DIRECTORY
$form = <<<EOD
<form>
Directory: <input name="dir" />
</form>
EOD;

echo $form;

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39669039
Duncanb7 had slashes that change direction in windows

C:\Users\Acer\Documents\NuSphere PhpED\Projects/array.php

and not proper href in linux

/home/content/27/89397/html/folder/001.Welcome.f4v

But I can not find errors in Ray's code

Thanks both.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39669053
Instead of using slashes, you want to use the context-aware predefined PHP constant:

DIRECTORY_SEPARATOR

If you find that you don't want to type that much when you're doing a lot of file system work, you can do something like this:

define('DS', DIRECTORY_SEPARATOR);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
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.
Suggested Courses

752 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