Jump to content

Lniks without file extensions


rawky1976

Recommended Posts

Hello

 

I have a MySQL database of document properties that is populated via an upload forms INSERT function.

 

I have a search results page that displays these properties in a dynamic repeating table.

 

I want the document title to be a link that opens the document itself.

 

This currently works if I enter into the document name field the actual document title INCLUDING FILE EXTENSION, but not if I dont add the file extension.

 

I have attached two files listing the INSERT function and the code for the search results table.

 

Hope someone can help!

 

Mark

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/
Share on other sites

You need to either:

 

1) Add a new column to the database for the file extension. Then you can display the file name and use the filename+extension in the actual link url.

 

OR

 

2) Include the entire file name (including extension) in the filename field and use some string manipulation to display everything up to the last period when displaying just the file name.

 

I would prefere option #1, but here is some sample code for option #2.

<?php

$file = "myfile.doc";

$filename = substr($file, 0, strrpos($file, "."));
$fileext  = substr($file, strrpos($file, "."));

echo "File Name & extension: " . $file . "<br>"; // mydoc.doc
echo "File Name: " . $filename . "<br>"; // mydoc
echo "File Extension: " . $fileext . "<br>"; // .doc

?>

Um, you would use pretty much the same code I posted above to take the file name and split it up into two parts. Using your existing code I made some modifications (you would need to add the column "document_ext" to the table.):

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadForm")) {

  $file = GetSQLValueString($_POST['docNameField'], "text");
  $filename = substr($file, 0, strrpos($file, "."));
  $fileext  = substr($file, strrpos($file, "."));

  $insertSQL = sprintf("INSERT INTO document (document_name, document_ext, relating_to, about, category_id, format_id, submitter_id) VALUES (%s, %s, %s, %s, %s, %s)",
        $filename,
        $fileext,
        GetSQLValueString($_POST['relatingToField'], "text"),
        GetSQLValueString($_POST['aboutField'], "text"),
        GetSQLValueString($_POST['categoryList'], "text"),
        GetSQLValueString($_POST['formatList'], "text"),
        GetSQLValueString($_POST['submitterList'], "text"));

  mysql_select_db($database_ConnKnowledgeBase, $ConnKnowledgeBase);
  $Result1 = mysql_query($insertSQL, $ConnKnowledgeBase) or die(mysql_error());
}

 

On a side note, I just don't understand why so many people use sprintf for creating their queries. IMHO, it makes it more difficult to read the code as you need to look back and forth to see which field each piece of data is going into.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.