Jump to content

File types


Canman2005

Recommended Posts

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

[code]
<?
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);

?>[/code]

I then print off the filenames using

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

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
Link to comment
Share on other sites

[!--quoteo(post=351601:date=Mar 4 2006, 03:24 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Mar 4 2006, 03:24 PM) [snapback]351601[/snapback][/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
Link to comment
Share on other sites

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.

[code]    $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));
[/code]

[!--quoteo(post=351602:date=Mar 4 2006, 09:26 AM:name=Canman2005)--][div class=\'quotetop\']QUOTE(Canman2005 @ Mar 4 2006, 09:26 AM) [snapback]351602[/snapback][/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:
[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"; [/code]

or if it's tucked in directory:
[code]if(file_exists("documents/".$num['filename'].".pdf")) $type=".pdf";[/code]
Link to comment
Share on other sites

[!--quoteo(post=351603:date=Mar 4 2006, 03:38 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Mar 4 2006, 03:38 PM) [snapback]351603[/snapback][/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.

[code]    $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));
[/code]
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:
[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"; [/code]

or if it's tucked in directory:
[code]if(file_exists("documents/".$num['filename'].".pdf")) $type=".pdf";[/code]
[/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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.