FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP & FTP - How to detect the file type.





Diablosblizz
Hey, I'm making a FTP client for the web for my site. I like to edit files when I am at school, and for security reasons my schools IP is blocked on my firewall for port 21 and 3306. So, basically I need a command to figure out what type of file it lists.

For example, I have a few files and folders in my directory. I need to detect what type the files are. If they're a folder, it will chdir to the new folder, and if it's a editable file it will edit it.

It's possible, because net2ftp has it. Thanks for your support.
LukeakaDanish
This may be a stupid question...but why not just use the built in file system browser in directadmin?
Diablosblizz
I am not using DirectAdmin, if you re-read the post it says I have blocked ports on my firewall, I am not able to do that on FriHost.
sonam
You can try to find out frist is it file or directory.
Code:

$file = "somefile.ext";
if(is_file($file)) {
 some stuff for file
} else {
 some stuff for directory
}


To find type of file you can use mime_content_type function (it is deprecated) or better Fileinfo functions.

Sonam
Diablosblizz
For the code you posted, would that work without a file name? So would this work?

Code:
$file = ".ext";
if(is_file($file)) {
 some stuff for file
} else {
 some stuff for directory
}


EDIT: After reading there is apparently a PHP function called is_dir, which I think would benefit me more. Thanks for your support sonam.
rvec
you might want to take a look at this script:
http://www.bierkip.nl/test/ftp/S2SFTP.zip

It is a script to connect to some other ftp server and download the files to the server it is on. So you could get some ftp functions and checks to see if something is a dir from that script.
Diablosblizz
I still can't get it. I don't know why. If you want to look at it here is the file:

http://frihost.pastebin.com/m69e449f6

It's unedited, because with it trying to get it to work added so many lines, so this is the cleanest I can get it. Thanks for the source rvec.
rvec
try this one instead:
http://frihost.pastebin.com/m39988894

You had one } too many
Diablosblizz
There wasn't anything wrong with the code, I am just unable to get it to detect if it's a folder or not.
rvec
the code you pasted was wrong, there where too many }.
In my code there also a check to see if something was a dir, and if it was go through it.
This is what I use to check if it is a dir:
Code:
    function is_dir($dir) {
        //check if something is a dir by trying to open it
        if (@ftp_chdir($this->link,$dir)) {
            //if it does open we want to go back to our own dir, else we'll get an error on checking the next thing
            ftp_chdir($this->link, $this->dir);
            return true;
        }else{
            return false;
        }       
    }
kv
Use this site http://www.net2ftp.com. It allows you to access files over http, upload files/edit/delete/change permissions/etc using ftp in the backend. All you need is an internet connection and a browser.

EDIT: Ok, didn't read the post properly.

Here is the solution I use in my ftp client.

use ftp_size($filename). If it returns -1, it is a directory.
For files, use extension to determine the type.

For more info, check
http://sourceforge.net/projects/browseftp/

You can download and refer the source code.
Diablosblizz
Kv that worked perfectly! Thank you very much.
kv
You are welcome Smile
Diablosblizz
New question: is it possible to get the text from a file (either over FTP or via fopen) and echo it to a page, but not have it put into HTML. For example, I want to have a "view" function, with syntax highlighting, but whenever I echo out the variable it just adds the HTML in the page being viewed into the current page, which I don't want.

Also, is it possible to fopen a file (this may not apply if the above is available by FTP) with a directory? For example, I have my FTP script in techcrew/ftp/index.php, but I want to view a file that is in /techcrew/body.php. I get a file not found error, and can't seem to get around it, although I can fix it with ../body.php.

Thanks!
Star Wars Fanatic
Diablosblizz wrote:
New question: is it possible to get the text from a file (either over FTP or via fopen) and echo it to a page, but not have it put into HTML. For example, I want to have a "view" function, with syntax highlighting, but whenever I echo out the variable it just adds the HTML in the page being viewed into the current page, which I don't want.

Also, is it possible to fopen a file (this may not apply if the above is available by FTP) with a directory? For example, I have my FTP script in techcrew/ftp/index.php, but I want to view a file that is in /techcrew/body.php. I get a file not found error, and can't seem to get around it, although I can fix it with ../body.php.

Thanks!

Enclose the html in <pre></pre> tags, it will output it as it's typed in, with all characters outputted to the screen as seen. You can see it here:
http://www.w3schools.com/TAGS/tag_pre.asp

It's great for posting code.

As for the second problem, go from the root directory when trying to find the file, and it should work fine. Eg: Instead of fopen ( "/techcrew/body.php" ) ; use fopen ( $_SERVER["DOCUMENT_ROOT"] . "/techcrew/body.php" ) ;
I haven't tested that with fopen, but I've used it extensively with including files.
Diablosblizz
The $_SERVER['DOCUMENT_ROOT']; worked perfectly, but the <pre> did not. The <pre> is removing half of my code, so only some parts are actually showing, and it's not showing me any of my PHP scripts. I would give you the URL, but it's on a private server and I don't want bots to crawl it. If you want, you can PM me and I'll give you the link.
Star Wars Fanatic
Diablosblizz wrote:
The $_SERVER['DOCUMENT_ROOT']; worked perfectly, but the <pre> did not. The <pre> is removing half of my code, so only some parts are actually showing, and it's not showing me any of my PHP scripts. I would give you the URL, but it's on a private server and I don't want bots to crawl it. If you want, you can PM me and I'll give you the link.

Can you post the code please? And if you could pm me the link, I would be grateful, I'd like to take a look.
Diablosblizz
Both have been sent in the PM, but here is the code for others:

Code:
   case 'view':
   $filename = $_SERVER['DOCUMENT_ROOT'] . $_GET['filename'];
   $handle = fopen($filename, "r");
   $contents = fread($handle, filesize($filename));
   echo "<pre>$contents</pre>";
   break;
Star Wars Fanatic
Hey, took a closer look at the pre tag, and it doesn't always output html code, some code actually is run, so to output the code, you have to convert all the & < > to their html equivalents before outputting it.
You can do it with this code:

Code:
case 'view':
   $filename = $_SERVER['DOCUMENT_ROOT'] . $_GET['filename'];
   $handle = fopen($filename, "r");
   $contents = fread($handle, filesize($filename));
   $contents = htmlspecialchars($contents);
   echo "<pre>$contents</pre>";
   break;


To see what the added function does, check here:
http://www.php.net/manual/en/function.htmlspecialchars.php
Diablosblizz
Is there anyway I can get the highlight_string into this command? Currently, it just shrinks my text. I was sort of hoping to get syntax highlighting working for this.

Thanks!
Star Wars Fanatic
Diablosblizz wrote:
Is there anyway I can get the highlight_string into this command? Currently, it just shrinks my text. I was sort of hoping to get syntax highlighting working for this.

Thanks!


Yea, you should be able to do it like this:

Code:
case 'view':
   $filename = $_SERVER['DOCUMENT_ROOT'] . $_GET['filename'];
   $handle = fopen($filename, "r");
   $contents = fread($handle, filesize($filename));
   $contents = htmlspecialchars($contents);
   highlight_string($contents);
   break;


And after looking at the manual for highlight_string, it appears to do the same thing htmlspecialchars do, so try this code first, and let me know if it works:
That function also adds <code> tags, so you don't need the <pre>, just call highlight_string($contents); and it will output the code.

Code:
case 'view':
   $filename = $_SERVER['DOCUMENT_ROOT'] . $_GET['filename'];
   $handle = fopen($filename, "r");
   $contents = fread($handle, filesize($filename));
   highlight_string($contents);
   break;
Diablosblizz
That works perfectly, and I'm starting to get my code in place, but I've noticed something. I have a textarea to get the file content, but if the file contains </textarea> it ends the textarea, and continues the code below the textarea, which I don't want. Is it possible to stop this?

Thank you SO much for your support.
Agent ME
Diablosblizz wrote:
That works perfectly, and I'm starting to get my code in place, but I've noticed something. I have a textarea to get the file content, but if the file contains </textarea> it ends the textarea, and continues the code below the textarea, which I don't want. Is it possible to stop this?

Thank you SO much for your support.

Use htmlspecialchars() on the text before you echo it into the textarea tag? Think that would work.
rvec
I hope you're not going to put this script on a public place, because this way someone could use the script to read the source of php files and any other file with the $_GET['filename'] var.
Diablosblizz
The code is secure, so I am not worried about that. HTMLspecialchars worked perfectly, thank you Agent Me.
Star Wars Fanatic
Diablosblizz wrote:
The code is secure, so I am not worried about that. HTMLspecialchars worked perfectly, thank you Agent Me.


You still might want to password protect the directory that the ftp scripts are in, for more protection. If someone stumbles upon the url, then they can copy, or change any file on your site (depending on what you have allowed).
Related topics
[php scripts ] phpweather&email
HTTP AUTH with PHP and mySQL
Online file storage
Yararlư Siteler
CuteFTP Professional
simple php to mysql form doesn't work
php..free account help
Browser close run a php script.
PHP & MySQL auth system
is all the music file type is strictly forbidden?
php & html
AJAX and PHP contact form
php script for Changing file permissions & owner
Setup Website using wordpress offline for editing
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.