Iain Simpson
Iain Simpson

Reputation: 8131

Link to file with dynamic name

I am trying to create a job application manager in php. The applicant fills in their details on the web form and presses submit, it is then added to the mysql database. As part of the form is an upload file box to add a covering letter, this will accept files in doc, docx, pdf, rtf, txt . When the file is uploaded it gets renamed to a hidden field in the form called 'applicationID', so a file called My covering letter.doc would become something like 124356456.doc.

The problem I am having is in my application manager I can use while($row = mysql_fetch_array($result)) and then pull out the applicants name etc, but im not sure how to deal with the files. If the file was just named 124356456 I could just link to that using the application id, but because the extension could be a combination of .doc, docx, pdf etc im not sure how to link to it. Ideally it would be something like :

<a href="/pathtofile/124356456.*">View Covering letter</a>

So what im asking is can I link to a file when I don't know what the extension is, but I know what the file name is ?.

Upvotes: 0

Views: 114

Answers (1)

fxbt
fxbt

Reputation: 2596

You have to store the extension in your database during the upload.

How to get the extension :

pathinfo($filename, PATHINFO_EXTENSION);

Another solution is to write a function to check if the file exists and return the correct extension.

<a href="<?php echo getFile('/pathtofile/124356456'); ?>">View Covering letter</a>

<?php
    function getFile($path) {
        if(file_exists($path.".doc"))
            return $path.".doc";
        else if(file_exists($path.".docx"))
            return $path.".docx";
        else if(file_exists($path.".pdf"))
            return $path.".pdf";
        else if(file_exists($path.".rtf"))
            return $path.".rtf";
        else if(file_exists($path.".txt"))
            return $path.".txt";
    }
?>

Upvotes: 2

Related Questions