Canman2005 Posted March 4, 2006 Share Posted March 4, 2006 Dear allI 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 calledhelpme.pdfin the database im simply holding the first part of the file, for the above example I hold in the sql databasehelpmeI 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 documentsSo 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 likehelpmeguideworkflowbookWhat I want to do is to put a link next to each result to link to the file, so it looks likehelpme - link to fileguide - link to fileworkflow - link to filebook - link to fileas 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.pdfThere will never be more then one file with the same name, ie. there is not ahelpme.pdfhelpme.xlshelpme.docthere 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 advanceEd Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted March 4, 2006 Share Posted March 4, 2006 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 Link to comment Share on other sites More sharing options...
Canman2005 Posted March 4, 2006 Author Share Posted March 4, 2006 [!--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?ThanksEd Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted March 4, 2006 Share Posted March 4, 2006 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)))." "; 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?ThanksEd[/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] Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted March 4, 2006 Author Share Posted March 4, 2006 [!--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)))." "; 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 getWarning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in c:\wamp\test.php on line 13Warning: reset() [function.reset]: Passed variable is not an array or object in c:\wamp\test.php on line 14Warning: current() [function.current]: Passed variable is not an array or object in c:\wamp\test.php on line 17Warning: Variable passed to each() is not an array or object in c:\wamp\test.php on line 24Any ideas?ThanksEd Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.