Jump to content


Photo

File types


  • Please log in to reply
4 replies to this topic

#1 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 04 March 2006 - 03:05 PM

Dear all

I have a question and im wondering if someone can help with a solution.

Basically I have a bunch of files, they are word, excel and pdf documents, I also have a sql database which holds the first part of all these files, the database does not hold the exension of each file as it could be one of 3 filetypes.

For example I have 1 file called

helpme.pdf

in the database im simply holding the first part of the file, for the above example I hold in the sql database

helpme

I don't hold the extension of each file as it could be one of 3 file types

.doc - for word documents
.xls - for exel documents
.pdf - for pdf documents

So there is no point in putting helpme.pdf as the file could be a word document (.doc) or a excel (.xls) document.

I then have a php page which performs a query and returns the list of files stored in the sql database, the sql I use is

<?
session_start();
include ("connect.php");
$sql ="SELECT * FROM files WHERE file = $file";

$result = @mysql_query($sql,$connection) or die(mysql_error());
$num = mysql_num_rows($result);

?>

I then print off the filenames using

<?php print "$rows[file]"; ?>

so a result page looks kind of like

helpme
guide
workflow
book

What I want to do is to put a link next to each result to link to the file, so it looks like

helpme - link to file
guide - link to file
workflow - link to file
book - link to file

as the database does not know what the file extension is, can I php statement be written which checks a specified folder (which the files are stored in) and then check to see if there is file matching the result which ends in either

.doc
.xls
.pdf

There will never be more then one file with the same name, ie. there is not a

helpme.pdf
helpme.xls
helpme.doc

there is only ever one file with the same name. The sql statement would need to find the file which just matches the first part of the filename.

Does that make any kind of sense to anyone? Can anyone help me?

Thanks in advance

Ed

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 04 March 2006 - 03:24 PM

I have two things you can try... the second will take a while to dig up... but have you thought about just using file_exists?

if(file_exists($myrow['filename'].".pdf")) ...
if(file_exists($myrow['filename'].".doc")) ...
if(file_exists($myrow['filename'].".xls")) ...

#3 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 04 March 2006 - 03:26 PM

[!--quoteo(post=351601:date=Mar 4 2006, 03:24 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Mar 4 2006, 03:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I have two things you can try... the second will take a while to dig up... but have you thought about just using file_exists?

if(file_exists($myrow['filename'].".pdf")) ...
if(file_exists($myrow['filename'].".doc")) ...
if(file_exists($myrow['filename'].".xls")) ...
[/quote]

Thanks for that.

I have never used file_exists before, could you explain how I could query with that?

Thanks

Ed

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 04 March 2006 - 03:38 PM

I wrote this little script to display the contents of a directory on a server with directory listings turned off. Maybe it will be useful.

    $dir = realpath("documents");
    if (is_dir($dir)) {
        if($dh=opendir($dir)) {
            while(($file=readdir($dh))!=false) {
                if(substr($file,0,1)!=".") {
                    $file_array[$i++]=$file;
                }
            }
            closedir($dh);
        }
    }
    array_multisort($file_array,SORT_ASC,SORT_STRING);
    reset($file_array);
    echo "<table>";
    do {
        if(current($file_array)!="") {
        echo "<tr><td>".current($file_array);
        echo "<td>".date("m/d/Y h:i a",filemtime("documents/".current($file_array)))."&nbsp;&nbsp;&nbsp;";
        echo "<td>";
        echo number_format(round(filesize("documents/".current($file_array)),1024));
        echo "k</tr>\n";
        }
    } while(each($file_array));

[!--quoteo(post=351602:date=Mar 4 2006, 09:26 AM:name=Canman2005)--][div class=\'quotetop\']QUOTE(Canman2005 @ Mar 4 2006, 09:26 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for that.

I have never used file_exists before, could you explain how I could query with that?

Thanks

Ed
[/quote]

file_exists just checks to see if a file exists, true if so, false if not.

so the link would just be something like "<a href=\"".$num['filename'].$type."\">";

you can get type tweaking the previous code:
if(file_exists($num['filename'].".pdf")) $type=".pdf";
if(file_exists($num['filename'].".doc")) $type=".doc";
if(file_exists($num['filename'].".xls")) $type=".xls";

or if it's tucked in directory:
if(file_exists("documents/".$num['filename'].".pdf")) $type=".pdf";


#5 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 04 March 2006 - 11:37 PM

[!--quoteo(post=351603:date=Mar 4 2006, 03:38 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Mar 4 2006, 03:38 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I wrote this little script to display the contents of a directory on a server with directory listings turned off. Maybe it will be useful.

    $dir = realpath("documents");
    if (is_dir($dir)) {
        if($dh=opendir($dir)) {
            while(($file=readdir($dh))!=false) {
                if(substr($file,0,1)!=".") {
                    $file_array[$i++]=$file;
                }
            }
            closedir($dh);
        }
    }
    array_multisort($file_array,SORT_ASC,SORT_STRING);
    reset($file_array);
    echo "<table>";
    do {
        if(current($file_array)!="") {
        echo "<tr><td>".current($file_array);
        echo "<td>".date("m/d/Y h:i a",filemtime("documents/".current($file_array)))."&nbsp;&nbsp;&nbsp;";
        echo "<td>";
        echo number_format(round(filesize("documents/".current($file_array)),1024));
        echo "k</tr>\n";
        }
    } while(each($file_array));
file_exists just checks to see if a file exists, true if so, false if not.

so the link would just be something like "<a href=\"".$num['filename'].$type."\">";

you can get type tweaking the previous code:
if(file_exists($num['filename'].".pdf")) $type=".pdf";
if(file_exists($num['filename'].".doc")) $type=".doc";
if(file_exists($num['filename'].".xls")) $type=".xls";

or if it's tucked in directory:
if(file_exists("documents/".$num['filename'].".pdf")) $type=".pdf";
[/quote]

Thanks for all the info.

When I try and use your script I get


Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in c:\wamp\test.php on line 13

Warning: reset() [function.reset]: Passed variable is not an array or object in c:\wamp\test.php on line 14

Warning: current() [function.current]: Passed variable is not an array or object in c:\wamp\test.php on line 17

Warning: Variable passed to each() is not an array or object in c:\wamp\test.php on line 24


Any ideas?

Thanks

Ed




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users