I'm trying to create a file sharing site where I can display the links of all uploaded images. Once a user uploads one file, they will be able to both see the files already uploaded and upload another file. However, I'm having two problems at the moment: 1. I don't know how to re-display the main upload page once a user uploads an image. 2. I don't know how to dynamically create links based on the files present.
Here is the code that I have so far:
HTML/PHP File-Sharing Site
<body>
<h2>File-Sharing Site</h2>
<h3>Upload file</h3>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
Search for file: <br />
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
PHP
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 800000)
&& in_array($extension, $allowedExts))
{
if($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>