Hey everyone,

So I have an issue that I'm not sure how to solve. As for most of my posts, I'm sure it's an easy fix that I'm just not fully grasping. Anyway, I want to have a data form upload and show an image of the type of data that's being submitted. For example, if I upload a PDF file, I want it to show the image of the pdf as it shows in the folder it's being saved to. Is there a way to do this? maybe something like echo '<div><a href="files/', $name, '"><img src="files/', $name,'" /> ', $name, '</a></div>';

Here is my upload.php file

<?php

if(!empty($_FILES["file"])){
    foreach($_FILES["file"]["name"] as $key => $name){
        if($_FILES["file"]["error"][$key] == 0 && move_uploaded_file($_FILES['file']['tmp_name'][$key], "files/{$name}")){
            $uploaded[] = $name;
        }
    }
}

?>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="upload.js"></script>
        <style type="text/css">
            #upload_progress{
                display: none;
            }
        </style>
        <title>File Upload</title>
    </head>
    <body>
        <div id="uploaded">
            <?php

                if(!empty($uploaded)){
                    foreach($uploaded as $name){
                        echo '<div><a href="files/', $name, '"><img src="files/', $name,'" /> ', $name, '</a></div>';
                    }
                }

            ?>
        </div>
        <div id="upload_progress"></div>
        <div>
            <form action="" method="post" enctype="multipart/form-data">
                <div>
                    <input type="file" id="file" name="file[]" multiple="multiple" />
                    <input type="submit" id="submit" value="Upload" />
                </div>
            </form>
        </div>
    </body>
</html>

Thanks for any advice :D

Recommended Answers

All 13 Replies

It would be easy to have icons for the standard file types and display a "PDF icon" if it's a PDF file.

This thread addresses the rendering of a PDF file but it seems that you may need more than that. You will need the ability to preview a variety of file types (maybe more than PDF's and image files) and then you need to manage how the preview is shown (location and size). This thread covers some of that.

Member Avatar for diafol

Be aware that "multiple" may not work with IE9 and earlier.

So I ended up not using an image and just kept it as a link. Should I just close this topic and post another one about having the link show up when I refresh the page or is it ok to ask that here?

1) I think when you upload, store its mimetype too separtely to identify its file type keep format standard (file table)
2) so while saving file on server and keeping its record in table save mime type also with other information.
3) then keep one table (2 columns) mimtype and icon_image_name columns (mime_table)
4) when displaying file list, join two table on mime to get icon_image_name that you can show in img tag

I hope this works for you.

@urtrivedi, What it sounds like to me is storing to a database. I want to store stricktly pdf's to a folder and be able to show the link that you can download from the folder but I want to link to stay on the page when you refresh the page but have nothing showing if there are no files in that folder..if that makes any sense

I don't want to use an icon for the link anymore. I just want the page to display the link like I have in my code but I want it to stay there even when I refresh the page and I'm assuming I need some function to check whether there is at least one file in the folder and if there is, to display the links of the files in that folder and if there is nothing, display nothing.

This is code I used to upload file to the selected folder and display and download file with link click

<?php

    session_start() ;
    if(is_uploaded_file($_FILES['userfile']['tmp_name']))
    {
        $currdirectory=$_SESSION['dirname'];
    }       
    else
    {
        if (trim($_POST['dirname'])=="")
            $currdirectory=dirname($_SERVER['DOCUMENT_ROOT']);
        else
            $currdirectory=$_POST['dirname'];
        $_SESSION['dirname']=$currdirectory;            
    }

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2900.2180" name=GENERATOR></HEAD>

<script lang=javascript>
    function link_click(dname)
    {
        document.frmdir.dirname.value=dname;
        document.frmdir.submit();
    }
    function btnsubmit_click()
    {

        document.frmcsv.submit();
    }

</script>
<BODY>

<?php

    $files = scandir($currdirectory);
    print "<b><BR>{$currdirectory}</b><br>";

    if (!(substr(strrev(trim($currdirectory)),0,1)=='/' ||substr(strrev(trim($currdirectory)),0,1)=='\\'))


    print "<a href=\"javascript:link_click('".str_replace('\\','',dirname($currdirectory))."')\">Up</a><br>";
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" id=frmcsv name=frmcsv >
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="8000000" />

<input name="userfile" type="file" class='button' id='userfile'>
<input type="button" value="Upload"  id=submit1 name=submit1 accesskey='1' onclick='btnsubmit_click()' class='button'>
</form>
<?php


    print "<form method='post' name=frmdir id=frmdir action='{$_SERVER['PHP_SELF']}'>";
    print "\n<table border=0>";
    for ($i=0;$i<count($files);$i++)
    {

        $content=$currdirectory."/".$files[$i];

        if (is_dir($content) && trim($files[$i])!="." && trim($files[$i])!="..")
        {
            print "\n<tr>";       
            print "\n\t<td valign=bottom><img src='../images/folder.jpg' border=0 >   </td>";
            print "<td><a href=\"javascript:link_click('".$content."')\">{$files[$i]}</td></a>";        
            print "\n</tr>";
        }

    }

    for ($i=0;$i<count($files);$i++)
    {
         $content=$currdirectory."/".$files[$i];
        if (is_file($content))
        {
            print "\n<tr>";
            print "\n\t<td valign=center align=right><img src='../images/file.jpg' border=0></td>";         
            print "\n\t<td><a  href='file.php?filename={$content}'>{$files[$i]}</a>";
            print "\n</td></tr>";
        }

    }
    print "\n</table>";
?> 
<input name=dirname id=dirname type=hidden>
</form>

</BODY></HTML>

where did you declare the variable for the location that should store the uploaded file?

it starts from the root folder, any where it can be store, if you want to fix
$_SESSION['dirname'] varible for ever and do not change it on any submit

I don't want it to store from the root of everything, just within the folder I am in. See, I have a root folder that has all the example web sites and web applications I've made and one of those folders is where the php file that has the form to upload and folder that stores the uploaded files. I want to pull the data from the "files" folder within that main folder that holds the form.php or upload.php file. Does that make sense?

set your path in following vairable permentely

$_SESSION['dirname']

I did and it still goes to the root directory in previous folders.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.