Hello all, I am using opendir script from php.net to view all the files in a directory and link to them. I am having 1 problem. The script Displays all the files in array but also uses extension at the end. I want that file name should be displayed but not the extension in anchor text. Can you help me. This is the script- <?php echo "<b>Individual Songs</b><br/>Right Click and Save Target as to Download<br/><ul>"; function getFiles($path) { //Function takes a path, and returns a numerically indexed array of associative arrays containing file information, //sorted by the file name (case insensitive). If two files are identical when compared without case, they will sort //relative to each other in the order presented by readdir() $files = array(); $fileNames = array(); $i = 0; if (is_dir($path)) { if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file == "." || $file == "..") continue; $fullpath = $path . "/" . $file; $fkey = strtolower($file); while (array_key_exists($fkey,$fileNames)) $fkey .= " "; $a = stat($fullpath); $files[$fkey]['size'] = $a['size']; if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-"; else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb"; else $files[$fkey]['sizetext'] = $a['size'] . " bytes"; $files[$fkey]['name'] = $file; $files[$fkey]['type'] = filetype($fullpath); $fileNames[$i++] = $fkey; } closedir($dh); } else die ("Cannot open directory: $path"); } else die ("Path is not a directory: $path"); sort($fileNames,SORT_STRING); $sortedFiles = array(); $i = 0; foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f]; return $sortedFiles; } $files = getFiles("/home/users/web/b84/ipw.amandeepmail/public_html/turboupload/php/$Movie/"); foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">$file[name]</a></b></li>"); echo "</ul>"; print("<b>Full Album (ZIP Format)-</b><br/><a href=\"http://www.turboupload.co.in/$Movie.zip\">Click Here</a>"); ?> Code (markup):
<?php function getFilenameWithoutExt($filename){ $pos = strripos($filename, '.'); if($pos === false){ return $filename; }else{ return substr($filename, 0, $pos); } } ?> PHP: OR $arr = explode(".", $allname); $filename = $arr[0]; PHP: OR, if you have a dotted filename: $filename = preg_replace( '/\.[a-z0-9]+$/i' , '' , 'dotted.file.Name' ); PHP: OR $FileNameTokens = explode('.', $allname); $fileName = implode(".", array_slice($FileNameTokens, 0, count($FileNameTokens) - 1)); PHP: JUST CHOOSE THE BETTER YOU FEEL ! I hope this will help you
OK I used the script in my file now how to implement it. I mean how can I display file name without extension (ie use this function in print?)
foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">$file[name]</a></b></li>"); PHP: I'm not sure how this script works, so try that: foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/" . preg_replace( '/\.[a-z0-9]+$/i' , '' , $file[name] ) . "', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">$file[name]</a></b></li>"); PHP: I hope thiw works
Sorry to say that but it didn't work. The 1st option in which you defined the function <?php function getFilenameWithoutExt($filename){ $pos = strripos($filename, '.'); if($pos === false){ return $filename; }else{ return substr($filename, 0, $pos); } } ?> Code (markup): Can you tell the way by which I can implement the value generated for filename by this funtion in print where there is anchor text.
Sorry ! I changed the path in the link and not the anchor text ! Try this: foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">" . preg_replace( '/\.[a-z0-9]+$/i' , '' , $file[name] ) . "</a></b></li>"); PHP:
If you would like to use the first option: Put the function at the beginning of the php code ( after the <?php ). then put this: foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">" . getFilenameWithoutExt($file[name]) . "</a></b></li>"); PHP: Hope this works
Nope not working neither the upper one. We are using the quotes so it stops the printing function I think. Files are at- http://www.turboupload.co.in/php/download/main.php (Main File which includes script.php) http://www.turboupload.co.in/php/download/download.php (This is the script)
I tested it now in my server and it's working. What do you see in the anchoer text ? or there are no anchor text ? Or all the print function don't work ? I tested it like that: <?php function getFilenameWithoutExt($filename){ $pos = strripos($filename, '.'); if($pos === false){ return $filename; }else{ return substr($filename, 0, $pos); } } echo "<b>Individual Songs</b><br/>Right Click and Save Target as to Download<br/><ul>"; function getFiles($path) { //Function takes a path, and returns a numerically indexed array of associative arrays containing file information, //sorted by the file name (case insensitive). If two files are identical when compared without case, they will sort //relative to each other in the order presented by readdir() $files = array(); $fileNames = array(); $i = 0; if (is_dir($path)) { if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file == "." || $file == "..") continue; $fullpath = $path . "/" . $file; $fkey = strtolower($file); while (array_key_exists($fkey,$fileNames)) $fkey .= " "; $a = stat($fullpath); $files[$fkey]['size'] = $a['size']; if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-"; else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb"; else $files[$fkey]['sizetext'] = $a['size'] . " bytes"; $files[$fkey]['name'] = $file; $files[$fkey]['type'] = filetype($fullpath); $fileNames[$i++] = $fkey; } closedir($dh); } else die ("Cannot open directory: $path"); } else die ("Path is not a directory: $path"); sort($fileNames,SORT_STRING); $sortedFiles = array(); $i = 0; foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f]; return $sortedFiles; } $files = getFiles("Y:/"); foreach ($files as $file) print("<li><b><a rel=\"nofollow\" target=\"_blank\" onClick=\"wopen('\http://www.turboupload.co.in/php/$Movie/$file[name]', 'popup', 500, 360); return false;\" href=\"http://www.turboupload.co.in/php/$Movie/$file[name]\">" . getFilenameWithoutExt($file[name]) . "</a></b></li>"); echo "</ul>"; print("<b>Full Album (ZIP Format)-</b><br/><a href=\"http://www.turboupload.co.in/$Movie.zip\">Click Here</a>"); ?> PHP: Try to copy it ( no problem to copy from Quote), don't forget to change you path !
Not working. Please help. It only prints the first echo and no array. The printing function needs a closing quote to stop printning and we have given a closing quote at ile[name]\"> " Code (markup): this stops the function and no printing takes place. Please help.
The code you posted worked perfectly my computer. I use $files = getFiles("c:/"); PHP: for the path. Could you, please, reply with the string you used for the path?
It is not working I use path- $files = getFiles("/home/users/web/b84/ipw.amandeepmail/public_html/turboupload/php/$Movie/"); PHP: which is fine and works fine if I remove that remove extension function.
Value of $Movie is Aksar which is given in other php file that is main.php please see my other post above. Now I have removed that and replaced it twith $file[name] and it has started working and also displays the expension .php My file main.php is at - http://www.turboupload.co.in/php/download/main.php Can you suggest some other way to remove that extension because this method dosen't work.