PHP & HTML: Download file link not working -
the next code lists files contained in folder in local machine, can see i'm echoing file path within tag using href attribute correctly.
so problem when click on tag, doesn't take me file download unless re-create link , paste tab of browser, why happening? how can prepare 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: happens file types including images, excel files, text documents, etc.
solution @werewolf - 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.'.- '.'<a href="'.asset($thepath.'/'.$file).'" target="_blank">'.$file.'</a><br>'; $x++; } } closedir($handle); ?>
make changes:
// remove public_path() $thepath = 'uploads/binnacle/'.$date.'/'.$activity->activity_id; then in link:
"<a href='" . asset($thepath/$file) . "' target='_blank'>$file</a><br>"; note: laravel has file component, may utilize that, check in source.
php html laravel hyperlink download
No comments:
Post a Comment