Index Page with upload option and directory images displaying

<?php
session_start();
if(!isset($_SESSION["username"])){
    header("location:login.php");
}
?>
<html>
<body>
<a href="logout.php">logout</a>
<h1>WELCOME</h1>
<form enctype="multipart/form-data" action="#" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit"name="submit" value="Upload File" />
</form>
<?php
if(isset($_POST['submit'])){
$target_path = $_SESSION['username']."/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else
{
    echo "There was an error uploading the file, please try again!";
}
}
?>
<?php
$myDirectory = opendir($_SESSION['username']."/");
while($entryName = readdir($myDirectory))
{
  $dirArray[] = $entryName;
}
// closedir($myDirectory);
$indexCount    = count($dirArray);
echo $_SESSION['username']."/";
echo "Your files are :";
sort($dirArray);
print("<TABLE border=1 cellpadding=5 cellspacing=0>\n");
print("<TR><TH>Filename</TH><th>Filetype</th></TR>\n");
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != ".")
    {
        // print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<TR><TD><img src='".$_SESSION['username']."/".$dirArray[$index]."'/></td>");
        print("</TR>\n");
    }
}
print("</TABLE>\n");
?>
</body>
</html>

Post a Comment

Analytics