PHP & HTML: Download file link not working - php

The following code lists all the files contained in a certain folder in my local machine, as you can see I'm echoing the file path inside the tag using the href attribute correctly.
So the problem is that when I click on the tag, it doesn't take me to the file download unless I copy the link and paste it to another tab of the browser, why is this happening? How can I fix it?
<h5>Attached Files</h5>
<?php
$date = $dateCreated->format('d-m-Y');
$thepath = public_path().'/uploads/binnacle/'.$date.'/'.$activity->activity_id;
$handle = opendir($thepath);
$x = 1;
while(($file = readdir($handle))!= FALSE) {
if($file != "." && $file != ".." && $file != "thumbs" && $file != "thumbs.db") {
echo $x.".- "."<a href='$thepath/$file' target='_blank'>$file</a><br>";
$x++;
}
}
closedir($handle);
?>
NOTE: This happens with all file types including images, excel files, text documents, etc.
SOLUTION BY #WereWolf - The Alpha:
<?php
$date = $dateCreated->format('d-m-Y');
$thepath = "/uploads/binnacle/".$date."/".$activity->activity_id;
$handle = opendir(public_path().$thepath);
$x = 1;
while(($file = readdir($handle))!= FALSE) {
if($file != "." && $file != ".." && $file != "thumbs" && $file != "thumbs.db")
{
echo $x.'.- '.''.$file.'<br>';
$x++;
}
}
closedir($handle);
?>

Make some changes:
// Remove the public_path()
$thepath = 'uploads/binnacle/'.$date.'/'.$activity->activity_id;
Then in the link:
"<a href='" . asset($thepath/$file) . "' target='_blank'>$file</a><br>";
Note: Laravel has a File component, You may use that, check it in the source.

Related

How to use the PHP unlink function?

I try to delete files with PHP. First I try to make a function to delete files but I want to delete one specific file and not all in the folder.
My function:
<?php
function del_tmp($file_name)
{
$dir = "mod_download/";
$verz = opendir($dir);
while ($file_name = readdir ($verz))
{
if($file_name != "." && $file_name != "..")
{
unlink($dir.$file_name);
}
}
closedir($verz);
}
?>
I think the problem is in this line: if($file_name != "." && $file_name != "..") but I have no idea how can i fix it.
Rather than processing over a whole directory as you only want to delete one file would it not be simpler and quicker to do
<?php
function del_tmp($file_name)
{
$dir = "mod_download/";
if ( file_exists($dir . $filename) ) {
unlink($dir . $file_name);
}
}
?>

Ordering files alphabetically when using readDir in PHP

I am using the following php to scan a directory and output the files.
I need to order these alphabetically but not sure how to do this.
This is what I have so far:
<?php
// path to directory
$directory = "gallery/photos/";
// open the directory
$handle = openDir($directory);
// Read the directory
while ($file = readDir($handle)) {
// filter the directory
if ($file != "." && $file != ".." && !is_dir($file)) {
// Allow only images (filter)
if (strstr($file, ".gif") || strstr($file, ".png") || strstr($file, ".PNG") || strstr($file, ".jpg")) {
// Path to the actual file
$directory_file = $directory . $file;
// Get image information (width, height)
$info = getImageSize($directory_file);
// show the picture
echo "<img src=\"$directory_file\" data-title=\"$file\"";
echo " width=\"$info[0]\" height=\"$info[1]\"> <br>\n";
}
}
}
// Close the directory
closeDir($handle);
?>
Use scandir instead which returns the files in alphabetical order
$arr = array_diff( scandir ( $directory ), ['.', '..'] );
// We are remmoving . and .. at this place
foreach($file in $arr){
// do your stuff
}

Read a file with php on php hosting

I am trying to read a file (ex: pdf file) on php hosting.
My code:
$myarray = array();
$path = "/home/mywebsite.com/httpdocs";
$open = opendir($path);
while ($x = readdir($open)) {
if ($file != "." && $file != "..") {
$myarray[] = $file;
}
}
sort($myarray);
closedir($open);
echo json_encode($myarray);
This code is running but if I change the value of $path : "/home/mywebsite.com/mydirectory/" instead of "/home/mywebsite.com/httpdocs", this code does not run.
I want to store my documents in my directory. Any suggestions?

PHP: endless loop of reading folders?

i wonder how i can solve the following problem:
I'm reading a folder on my server and i'm displaying the contents as follows. if there's an image i'll print an image, if there's folder i'll print a div with a folder icon. i wonder if it's possible click on one of those subfolders and then display the contents of this folder the exact same way I'm currently displaying the contents of the parent folder. it should work as kind of an endless loop if there are folders inside of folders.
$path = 'files';
if (($retval = scandir($path)) !== false) {
$retval = array_filter($retval, 'filter_files');
shuffle($retval);
}
function filter_files($file) {
return ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db');
}
//loop through shuffled files
foreach ($retval as $value) {
$ext = pathinfo($value, PATHINFO_EXTENSION); //file extension
if ($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
print "<img class='thumb' src='$path/$value'/>";
} else if ($ext == "") { //must be a folder
print "<div class='thumb folder'><a href=''>link_to_subfolder</a></div>";
} else {
//not supported
}
}
is that even possible?
For folder, put a GET parameter in the link:
echo 'foo';
Then in your script, read the $_GET['dir'] variable to know which folder to use. You would paste this variable to your $path.
Note that you must keep in mind that if you let anyone use the script, they will be able to open any folder by changing the dir parameter. E.g. if they pass ../../../../etc, they may access the servers passwd file.
On your folders where you have the link set a get variable for the folder path, then if that path exists set the load path to it:
print "<div class='thumb folder'><a href='yourfile.php?path=".$value."'>link_to_subfolder</a></div>";
In file:
if(isset($_GET['path'])) $path = $_GET['path'];
else $path = 'files';
That example is very basic, you need to consider whether the user can view the folder they are requesting. For example, if I knew you just used the code above I could enter the folder path to the default password directory and view the files. You need add some validation in to check.
You may want to have an AJAXified page, where if the user clicks on a folder it sends an AJAX request to the server and gets the contents of that folder and displays them. That would make the PHP simpler but you'd have to write some JavaScript for the page.
Also, you may want to look into using is_dir() to decide whether something is a directory. Testing whether there is a file extension isn't foolproof (files do not have to have extensions).
Without going into too much detail about recursion, here's a minor edit that'll do what you want something akin to what you want (assuming your code works).
$base = 'files';
function filter_files($file) {
return ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db');
}
function show_directory($path) {
if (($retval = scandir($path)) !== false) {
$retval = array_filter($retval, 'filter_files');
shuffle($retval);
}
//loop through shuffled files
foreach ($retval as $value) {
$ext = pathinfo($value, PATHINFO_EXTENSION); //file extension
if ($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
print "<img class='thumb' src='$path/$value'/>";
} else if (is_dir($path . '/' . $value)) { // *is* a folder
print "<div class='thumb folder'><a href=''>link_to_subfolder</a>";
show_directory($path . '/' . $value);
print "</div>";
} else {
//not supported
}
}
}
show_directory($base);
Some reference material:
Wikipedia
Recursion tutorial
EDIT: I think I misread the question, though this is still useful information for the OP, I'm sure, so I'll leave it for now.

problem in showing all files of a directory

hey guys im looking for a way to show all mp3 files in a directory
this is my code to get that :
if ($handle = opendir($dirPath)) {
while (false !== ($file = readdir($handle))) {
if ($file = ".mp3" && $file = "..") {
echo '
<track>
<location>'.$dirPath.$file.'</location>
<creator>'.$file.'</creator>
</track>
';
}
}
closedir($handle);
}
now i know that this script will only show mp3 files in parent directory , but i need to show all mp3 files in all directory inside parent directory
problem is this code cant show files inside sub directories !
That code won't work at all. As you are setting the $file variable to ".." the result will be a lot of xml containing $dirPath and "..".
This is what you are looking for :)
$it = new RecursiveDirectoryIterator('path/to/files/');
foreach (new RecursiveIteratorIterator($it) as $file)
{
echo $file->getPathname() . '<br />';
}
You'll have to make a recursive function to search for all the MP3s.
Also, you probably meant if ($file == ".mp3" && $file == "..") { instead of if ($file = ".mp3" && $file = "..") {, and after that's changed, you get a condition that's always false. What are you trying to do there?
like icktoofay said, you'll have to make a recursive function. also, your code has an error:
if ($file = ".mp3" && $file = "..") {
won't work (and if ($file == ".mp3" && $file == "..") { is wrong, too). that line should look like this:
if (substr($file,-4) == ".mp3" || $file == "..") {
if you want to show the ".." - else it's just like this:
if (substr($file,-4) == ".mp3") {

Categories