Okay so I have been doing a lot of research and really I am stumped. Basically what I am trying to do is crate something similar to to a browse button that can browse files and save their path. Now when I say browse, I mean browse the server instead of clients computer. I realize how unsecure this is, but the client who will be using this aspect will be an admin. I do not want the ability to upload files/ move/ store. I just need the file path on the server to save to a data base. The client basically will be choosing a file saved on our server and adding keywords to it. I just need to beable to save the path to this file as I got the keywords all working. I heard a suggestion to use ajax but anything I have found on that is very complex and way beyond what I need or like not free. Is there a way to use php and maybe some javascript? Like say the user clicks a button and like it relates to a javascript function that loads a listbox full of the folders and files of a documents directory and everytime they click on a folder in the list box it opens the sub directory. Once the user selects the file it saves this file path as a variable to be sent to post or even just add it to the database. I am completely stuck. I know no one can do this for me so I merely want suggestions as to what would be my best plan of attack. Should I try my listbox idea or does anyone know of a server file manager that is easy to add to a php page and that can be edited relitively easy? I have a bunch of ajax stuff I have been testing but they require like tons and tons of folders to run and like were developed for complete control. I just want to browse for a file path on the server side and save that path.
HTML File
Page 1 of 114 Replies - 1027 Views - Last Post: 07 June 2011 - 02:52 PM
Replies To: HTML File
#2
Re: HTML File
Posted 05 June 2011 - 10:32 PM
Yeah there's nothing like that for servers... You will have to basically create it yourself.
#3
Re: HTML File
Posted 06 June 2011 - 06:30 AM
#4
Re: HTML File
Posted 06 June 2011 - 08:17 AM
Thanks for the input guys. I was afraid of that
If I get it working I will for sure post a tutorial or at the very least an example.
#5
Re: HTML File
Posted 06 June 2011 - 11:05 AM
It doesn't have to use Ajax by the way. Ajax only would ensure that you don't have to refresh the page to view different directories.
#6
Re: HTML File
Posted 06 June 2011 - 11:42 AM
codeprada, on 06 June 2011 - 12:05 PM, said:
It doesn't have to use Ajax by the way. Ajax only would ensure that you don't have to refresh the page to view different directories.
Yeah true, but Ajax hasn't been too hard to learn so far. I have something working to an extent. Basically what I am doing is having a listbox that is populated with all the files and folders in say a directory called documents/ This works fine except I want to A-Bold the folders And B if it is a folder that is selected repopulate the listbox with its sub directories. I have run into a problem. I am using the is_file() and is_dir() function to check for this but it is getting unexpected results. SO I found a function that is able to list all the files and folders of a directory
function listDirs($where){
echo "<table border=\"1\"><tr><td><b>Name</b></td><td><b>Type</b></td>";
echo "<td><b>Invisible (Hidden)?</b></td></tr>";
$itemHandler=opendir($where);
$i=0;
while(($item=readdir($itemHandler)) !== false){
if(substr($item, 0, 1)!="."){
if(is_dir($item)){
echo "<tr><td>$item</td><td>Directory</td><td>No</td></tr>";
}else{
echo "<tr><td>$item</td><td>File</td><td>No</td></tr>";
}
$i++;
}else{
if(is_dir($item)){
echo "<tr><td>$item</td><td>Directory</td><td>Yes</td></tr>";
}else{
echo "<tr><td>$item</td><td>File</td><td>Yes</td></tr>";
}
$i++;
}
}
echo "</table>";
}
listDirs("../");
This created a nice table but a All the folders are considered files. Like the only directories are ./ and ../ and everything else is a file. I couldn't find any information if there is a is_folder() function so I think I will have to create this myself. Now after all this, my questions is does the opendir() return false if opening a file? I know it returns false on failure and generates a warning. So I figured I could make a is_folder() function that merely tries opening all the files and folders. If it tries opening a file such as new.txt I would think it would return false. Now if it tried opening "uploads/" then maybe it would return the filehandle. But like I said earlier it lists all my folders as files so I am unsure.
#7
Re: HTML File
Posted 06 June 2011 - 01:56 PM
Okay so while trying to figure out if I could use opendir()...my page is blank. I try running this php code
and the page loses all formatting and everything. I guess I am using the function opendir wrong or something. It says it produces an E_Warning meaning script execution is not halted. I don't get why it kill the formatting to the page.
if($dh=opendir("/../uploads/"))
{
print "here"
closedir($dh)
}else
{
print "fail"
}
and the page loses all formatting and everything. I guess I am using the function opendir wrong or something. It says it produces an E_Warning meaning script execution is not halted. I don't get why it kill the formatting to the page.
#8
Re: HTML File
Posted 06 June 2011 - 02:05 PM
if($dh=opendir("/../uploads/"))
Let's break this down. That path says:
/
go to the root directory
..
go back a directory
See the problem?
#9
Re: HTML File
Posted 06 June 2011 - 02:16 PM
Does this mean I can't check directories? like I thought it only opened a handle not like try to move the whole page. I tried
to no avail. I want to check if uploads/ is a folder not just an ordinary file. like my directories are kinda managed like this
--test/
-----manager/
--------index.php
-----uploads/
-------textfile.txt
and in index.php Is where this is being ran. Can I not check above directories?
if($dh=opendir("../uploads/"))
to no avail. I want to check if uploads/ is a folder not just an ordinary file. like my directories are kinda managed like this
--test/
-----manager/
--------index.php
-----uploads/
-------textfile.txt
and in index.php Is where this is being ran. Can I not check above directories?
This post has been edited by McSick: 06 June 2011 - 02:17 PM
#10
Re: HTML File
Posted 06 June 2011 - 07:54 PM
Why can't you?
<?php
$directory = opendir(".");
while($dir = readdir($directory)) {
if(substr($dir, 0, 1) != ".") {
if(is_dir($dir)) {
echo "<b>directory:</b> ";
}
echo $dir."<br />";
}
}
?>
#11
Re: HTML File
Posted 06 June 2011 - 08:50 PM
CTphpnwb, on 06 June 2011 - 08:54 PM, said:
Why can't you?
<?php
$directory = opendir(".");
while($dir = readdir($directory)) {
if(substr($dir, 0, 1) != ".") {
if(is_dir($dir)) {
echo "<b>directory:</b> ";
}
echo $dir."<br />";
}
}
?>
Ty so much =D this did list the sub directory correctly. Now I just need to mess with my ajax script.
#12
Re: HTML File
Posted 06 June 2011 - 09:32 PM
Just a tip
$dir = "/randDir"
That / in the front means root directory.
-- <- You are starting here
---uploads/
---pictures/
---scripts/
---randDir/ <- looking for this directory
$dir = "secret/";
Without that / in the front it means that you are looking for that directory/file in the same directory the script you are running resides in.
--
---uploads/
---pictures/
---scripts/ <- You are here
-----secret/ <- Looking for this
-----otherStuff/
---randDir/
However ../, means to look in the directory above the current one. (tip: you can combine them. Example: ../../directory)
$dir = "../uploads/";
--
---uploads/ <- Looking for this
---pictures/
---scripts/ <- You are here
-----secret/
-----otherStuff/
---randDir/
$dir = "/randDir"
That / in the front means root directory.
-- <- You are starting here
---uploads/
---pictures/
---scripts/
---randDir/ <- looking for this directory
$dir = "secret/";
Without that / in the front it means that you are looking for that directory/file in the same directory the script you are running resides in.
--
---uploads/
---pictures/
---scripts/ <- You are here
-----secret/ <- Looking for this
-----otherStuff/
---randDir/
However ../, means to look in the directory above the current one. (tip: you can combine them. Example: ../../directory)
$dir = "../uploads/";
--
---uploads/ <- Looking for this
---pictures/
---scripts/ <- You are here
-----secret/
-----otherStuff/
---randDir/
#13
Re: HTML File
Posted 07 June 2011 - 04:44 AM
Hi there,
What about using scandir function, I know it's php5 only but I think most of web hosts support it now.
in my attempt to create a file browser, I used my own ajax, but you can use yours by doing a few modifications to the AJAX part.
also, I'm returning the result in JSON format, you can modify this too if you update corresponding codes.
and instead of bolding the folders I added file types icons.
anyways, here's my code hoping it helps.
HTML and Javascript:
PHP:
Note:
in addition to above codes, I included the AJAX function, CSS, icons in the attached file.
filebrowser.zip (11.92K)
Number of downloads: 36
Hope it helps.
Edit: I just forgot to mention that you can add comma separated file extensions in the filter textbox to display only corresponding file types.
What about using scandir function, I know it's php5 only but I think most of web hosts support it now.
in my attempt to create a file browser, I used my own ajax, but you can use yours by doing a few modifications to the AJAX part.
also, I'm returning the result in JSON format, you can modify this too if you update corresponding codes.
and instead of bolding the folders I added file types icons.
anyways, here's my code hoping it helps.
HTML and Javascript:
<html>
<head>
<title> File Browser</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
function search(){
document.getElementById("pCurrentPath").innerHTML="Loading...";
var p=document.getElementById("hdnCurrentPath").value;
var a=new Ajax();
with (a){
Method="POST";
URL="core.php";
Data="path="+p+"&filter="+document.getElementById("txtFilter").value;
ResponseFormat="json";
ResponseHandler=showFiles;
Send();
}
}
function showFiles(res){
var p=document.getElementById("hdnCurrentPath").value;
document.getElementById("pCurrentPath").innerHTML=p;
document.getElementById("dvContents").innerHTML="";
var oddeven="odd";
for (i=0;i<res.length;i++){
var f=res[i];
var el=document.createElement("p");
el.setAttribute("fPath",f.fPath);
el.setAttribute("fType",f.fType);
el.className=oddeven;
el.innerHTML="<span class='icon ft_"+f.fType+"'> </span>"+f.fName;
document.getElementById("dvContents").appendChild(el);
oddeven=(oddeven=="odd")?"even":"odd";
el.onclick=selectItem;
}
}
function selectItem(){
var ftype=this.getAttribute("fType");
if(ftype=="folder"){
document.getElementById("hdnCurrentPath").value=this.getAttribute("fPath");
search();
}else{
alert("File Path: " + this.getAttribute("fPath")+"\n\nFile Type: "+ this.getAttribute("fType"));
}
}
</script>
</head>
<body onload="search()">
<div class="browser">
<p class="pfilter">File types filter
<input type="text" id="txtFilter" value="" />
<input type="button" value="Refresh" onclick="search()" />
<input type="hidden" id="hdnCurrentPath" value="" />
</p>
<p id="pCurrentPath">Loading...</p>
<div id="dvContents"> </div>
</div>
</body>
</html>
PHP:
<?php
define("BASE_DIR","../");
function searchDir($p="",$f=""){
$contents=array();
$p=trim($p);
$f=trim($f);
if($p=="")$p=BASE_DIR;
if(!is_dir($p))$p=dirname($p);
$filter=($f=="")?array():explode(",",$f);
$files=@scandir($p);
if (!$files)return "";
for ($i=0;$i<count($files);$i++){
$fName=$files[$i];
if(substr($p,-1)!="/")$p.="/";
$fPath=$p.$fName;
$isDir=is_dir($fPath);
$add=false;
$fType="folder";
if (!$isDir){
$ft=strtolower(substr($files[$i],strrpos($files[$i],".")+1));
$fType=$ft;
if ($f!=""){
if (in_array($ft,$filter))$add=true;
}else{
$add=true;
}
}else{
if($fName!=".")$add=true;
if($fName==".."){
$tempar=explode("/",$fPath);
array_splice($tempar,-2);
$fPath=implode("/",$tempar);
if($fPath=="..")$fPath="";
}
}
if ($add)$contents[]=array("fPath"=>$fPath,"fName"=>$fName,"fType"=>$fType);
}
return $contents;
}
$p=isset($_POST["path"])?$_POST["path"]:"";
$f=isset($_POST["filter"])?$_POST["filter"]:"";
echo json_encode(searchDir($p,$f));
?>
Note:
in addition to above codes, I included the AJAX function, CSS, icons in the attached file.
filebrowser.zip (11.92K)
Number of downloads: 36
Hope it helps.
Edit: I just forgot to mention that you can add comma separated file extensions in the filter textbox to display only corresponding file types.
This post has been edited by ahmad_511: 07 June 2011 - 06:37 AM
#14
Re: HTML File
Posted 07 June 2011 - 12:23 PM
@ahmad_511...I have searched countless hours for what your code does exactly! Thank you so much!!!!! Can I use this in a website? I can give you credit in the code no problem. I will do some slight modifying since I need to save the file path for a database but this is like perfect!!!!!
#15
Re: HTML File
Posted 07 June 2011 - 02:52 PM
You're welcome McSick,
It was only my attempt and my way to do it, so please feel free to modify it as needed
Regards
It was only my attempt and my way to do it, so please feel free to modify it as needed
Regards
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote









|