我正在尝试创建一个文件共享站点,我可以在其中显示所有上传图像的链接。一旦用户上传了一个文件,他们将能够看到已经上传的文件并上传另一个文件。但是,我目前有两个问题: 1. 一旦用户上传图像,我不知道如何重新显示主上传页面。2.我不知道如何根据存在的文件动态创建链接。
这是我到目前为止的代码:
HTML/PHP 文件共享网站
<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";
}
?>