I have a system I am developing to handle documents. Now everything process wise is working fine. The only thing I cannot seem to get is the file browser.
Whenever someone submits a document a folder is created with their name and the documents placed inside. I have a bit of code to view the folders but there is no way to add a button so the user can go back to the system.
see my code below:
<?php
$dir = opendir('SubmittedDocs/');
echo '<ul>';
while ($read = readdir($dir))
{
if ($read!='.' && $read!='..')
{
echo '<li>'.$read.'</li>';
}
}
echo '</ul>';
closedir($dir);
?>
Related
I want to open a server stored html report file on a client machine.
I want to bring back a list of all the saved reports in that folder (scandir).
This way the user can click on any of the crated reports to open them.
So id you click on a report to open it, you will need the location where the report can be opend from
This is my dilemma. Im not sure how to get a decent ip, port and folder location that the client can understand
Here bellow is what Ive been experimenting with.
Using this wont work obviously:
$path = $_SERVER['DOCUMENT_ROOT']."/reports/saved_reports/";
So I though I might try this instead.
$host= gethostname();
$ip = gethostbyname($host);
$ip = $ip.':'.$_SERVER['SERVER_PORT'];
$path = $ip."/reports/saved_reports/";
$files = scandir($path);
after the above code I loop through each file and generate a array with the name, date created and path. This is sent back to generate a list of reports in a table that the user can interact with. ( open, delete, edit)
But this fails aswell.
So im officially clueless on how to approach this.
PS. Im adding react.js as a tag, because that is my front-end and might be useful to know.
Your question may be partially answered here: https://stackoverflow.com/a/11970479/2781096
Get the file names from the specified path and hit curl or get_text() function again to save the files.
function get_text($filename) {
$fp_load = fopen("$filename", "rb");
if ( $fp_load ) {
while ( !feof($fp_load) ) {
$content .= fgets($fp_load, 8192);
}
fclose($fp_load);
return $content;
}
}
$matches = array();
// This will give you names of all the files available on the specified path.
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text($ip."/reports/saved_reports/"), $matches);
foreach($matches[2] as $match) {
echo $match . '<br>';
// Again hit a cURL to download each of the reports.
}
Get list of reports:
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/reports/saved_reports/";
$files = scandir($path);
foreach($files as $file){
if($file !== '.' && $file != '..'){
echo "<a href='show-report.php?name=".$file. "'>$file</a><br/>";
}
}
?>
and write second php file for showing html reports, which receives file name as GET param and echoes content of given html report.
show-report.php
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/reports/saved_reports/";
if(isset($_GET['name'])){
$name = $_GET['name'];
echo file_get_contents($path.$name);
}
I am aware that this kind of question has already been asked before, but I have a slightly different case.
foreach($dir as $file)
{
$file = '<li>'.basename($file).'</li>';
echo $file;
}
This is my script to display files in a folder and link to them. The way it is now, I use the $_GET['file'] on the other page to receive the information on the other page. The other page is supposed to display a photo/video with the file that has been linked, however I don't know how to use the $_POST or $_SESSION in this case, since it's a loop and I don't want the information about the file be in the link.
Also, I don't want any forms. I want to click the link with the name of the file and the other website to already have the information about the file and display the video or image.
Use like this by storing the media path in session and use the corresponding index to pass.
First file:
<?php
$i = 1;
session_start();
$dir = array('1.jpg', '2.jpg', '3.jpg');
echo '<ul>';
foreach($dir as $file)
{ //echo $i.'---'.$file."<br />";
echo '<li>'.$file.'</li>';
$_SESSION['media_'.$i] = $file;
$i++;
}
echo '</ul>';
?>
test.php
<?php
session_start();
echo $_SESSION['media_'.$_GET['file']];
?>
In a file called docs.php I have made the following code to display all the files inside uploads folder
The foreach() loops all the files there stored in a view of the website and has an hyperlink that let it be open in the browser (in my model file it has restriction's to only pds, png or jpg files)
What i'm not able to do is inside the foreach() loop for every iteration is display some sort of hyperlink (example: Delete me!) that permits the user to click and delete only that particular file physical from the server, the others not clicked must remain visible.
My php code inside a view to the forloop() is:
<?php
$files=\yii\helpers\FileHelper::findFiles('uploads/', ['except'=>['*.DS_Store']]);
if (isset($files[0])) {
foreach ($files as $index => $file) {
$nameFile = substr($file, strrpos($file, '/') + 1);
echo Html::a($nameFile, Url::base().'/uploads/'.$nameFile) . "<br/>" . "<br/>" ; // render do ficheiro no browser
}
} else {
echo "There are no files available for download.";
}
?>
As i assume you have concept of FileHelper, but to delete file you have to use php native function unlink(filePath). unlink()
I use php files in my website and in those file I include my html to show whatever i want the user to view. However i've ran into a problem. I need to show a list of available downloads from a specific folder from my website. The uploading downloadables from my website works. The reading files from directory works, this is done using php code. Now the thing is how can I show this in my html, how to I show this to my user in a fashion that i like most, like for example how Dropbox shows their file listing something like that.
The thing is to pass those files found within the PHP file and pass them to the html to be able to work with them however I want.
I hope i am clear, in case please just tell me so I can elaborate more.
Thanks.
Some code as requested, this is how I am supposedly extracting my files from my website's directory...
Ahh something like this, i get the idea, but here is my problem...
my code looks like this...
$directory_mine;
if ($directory_mine = opendir('/path/to/files')) {
//This is for testing.
echo "Directory: ". $directory_mine . "\n";
echo "Entries:\n";
while (false !== ($entry = readdir($directory_mine))) {
//should be writing each file name into the html here. at least thats my thinking.
}
closedir($directory_mine);
}
include("overall_header.html");
include("mobiledownloadview.html");
include("overall_footer.html");
See here is the problem, how can i add the data extracted by my php file to the mobiledownloadview.html???
I believe this is one way to do it, but if this is terrible please tell me. is there a better way to acomplish my goal?
From the manual on readdir:
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
/* This is the WRONG way to loop over the directory. */
while ($entry = readdir($handle)) {
echo "$entry\n";
}
closedir($handle);
}
?>
If you want to style it, wrap it in an (un-) ordered list and use css for that.
while ($entry = readdir($directory_mine)) !== false) {
echo "Entry: $entry ; filetype: " . filetype($directory_mine . $entry) . "\n";
}
If you're using PHP 5.3+, the PHP SPL class FilesystemIterator could be useful in this case, it easily allows iterating over a certain path and retrieving files as objects, including metadata.
http://www.php.net/manual/en/class.filesystemiterator.php
Example:
<?php
$it = new FilesystemIterator($directory_mine);
echo '<ul class="file_list">'
foreach ($it as $fileinfo) {
echo '<li>' . $fileinfo->getFilename() . '</li>' . PHP_EOL;
}
echo '</ul>'
You can add CSS to the page to style the unordered list how you want it to appear.
Normally if there is no htacces restriction is enabled it is possible to view the list of files under a folder hosted in web server using browsers. Except if there exist a index file like index.php it automatically go to the index page. (as far i know)
But is it possible to see the list of files though there exist an index file ?
thanks in advance
No, there is not. All web servers I'm aware of will only ever display a directory listing if there is no index page available (and, even then, only if directory listings are not disabled).
Build a file listing in PHP and display it in the index file.
Check out the info at http://php.net/manual/en/function.readdir.php . I used this for a client to display certain file types in a directory through the index.php file.
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
closedir($handle);
}
?>
Put this in the web root directory a sindex.php
<?php
$pngFolder = <<< EOFILE
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAA3NCSVQICAjb4U/gAAABhlBMVEX//v7//v3///7//fr//fj+/v3//fb+/fT+/Pf//PX+/Pb+/PP+/PL+/PH+/PD+++/+++7++u/9+vL9+vH79+r79+n79uj89tj89Nf889D88sj78sz78sr58N3u7u7u7ev777j67bL67Kv46sHt6uP26cns6d356aP56aD56Jv45pT45pP45ZD45I324av344r344T14J734oT34YD13pD24Hv03af13pP233X025303JL23nX23nHz2pX23Gvn2a7122fz2I3122T12mLz14Xv1JPy1YD12Vz02Fvy1H7v04T011Py03j011b01k7v0n/x0nHz1Ejv0Hnuz3Xx0Gvz00buzofz00Pxz2juz3Hy0TrmznzmzoHy0Djqy2vtymnxzS3xzi/kyG3jyG7wyyXkwJjpwHLiw2Liw2HhwmDdvlXevVPduVThsX7btDrbsj/gq3DbsDzbrT7brDvaqzjapjrbpTraojnboTrbmzrbmjrbl0Tbljrakz3ajzzZjTfZijLZiTJdVmhqAAAAgnRSTlP///////////////////////////////////////8A////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////9XzUpQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAB90RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3MgOLVo0ngAAACqSURBVBiVY5BDAwxECGRlpgNBtpoKCMjLM8jnsYKASFJycnJ0tD1QRT6HromhHj8YMOcABYqEzc3d4uO9vIKCIkULgQIlYq5haao8YMBUDBQoZWIBAnFtAwsHD4kyoEA5l5SCkqa+qZ27X7hkBVCgUkhRXcvI2sk3MCpRugooUCOooWNs4+wdGpuQIlMDFKiWNbO0dXTx9AwICVGuBQqkFtQ1wEB9LhGeAwDSdzMEmZfC0wAAAABJRU5ErkJggg==
EOFILE;
if (isset($_GET['img']))
{
header("Content-type: image/png");
echo base64_decode($pngFolder);
exit();
}
$projectsListIgnore = array ('.','..');
$handle=opendir(".");
$projectContents = '';
while ($file = readdir($handle))
{
if (is_dir($file) && !in_array($file,$projectsListIgnore))
{
$projectContents .= '<li>'.$file.'</li>';
}
}
closedir($handle);
?>
<ul class="projects">
<?php $projectContents ?>
</ul>