PHP/HTML - 在列表中显示上传的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12665840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP/HTML - Display Uploaded Files in List
提问by MrMedia715
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.
我正在尝试创建一个文件共享站点,我可以在其中显示所有上传图像的链接。用户上传一个文件后,他们将能够看到已上传的文件并上传另一个文件。但是,我目前有两个问题: 1. 一旦用户上传图像,我不知道如何重新显示主上传页面。2.我不知道如何根据存在的文件动态创建链接。
Here is the code that I have so far:
这是我到目前为止的代码:
HTML/PHPFile-Sharing Site
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
<?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";
}
?>
?>
回答by Timo K?hk?nen
You can add this to your main page to display uploaded files:
您可以将其添加到主页以显示上传的文件:
<?php
if ($handle = opendir('upload/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry<br>";
}
}
closedir($handle);
}
?>
Of course you have to redirect back to the main page to see the list of files and possibly upload more files. Or to use ajax to upload files, so that you are not forced to redirect back.
当然,您必须重定向回主页才能查看文件列表并可能上传更多文件。或者使用ajax上传文件,这样你就不会被迫重定向回来。
I have used ready made plugin to achieve this all: http://blueimp.github.com/jQuery-File-Upload/
我使用现成的插件来实现这一切:http: //blueimp.github.com/jQuery-File-Upload/
回答by Djomla
You can use Ajax file upload. Upload file with ajax, and then return file path as response... Then just print that response as link on page (in some div, field... )
您可以使用Ajax 文件上传。用ajax上传文件,然后返回文件路径作为响应......然后将该响应作为链接打印在页面上(在某些div,字段......)
$.ajaxFileUpload
(
{
url:'upload_file.php',
secureuri:false,
fileElementId:'file',
dataType: 'json',
success: function (response)
{
$("#div_where_you_print_response").html(response)
},
error: function (data, status, e)
{
alert(e);
}
}
);
回答by MrSil
1) Use Header functionheader('Location: ... ');for redirect user to main upload page
2) You need to use *sql solution to store file links and display them.
2a) Give unique file name to all uploaded files(i.e md5($file['name'])
1) 使用Header 功能header('Location: ... ');将用户重定向到主上传页面
2) 您需要使用 *sql 解决方案来存储文件链接并显示它们。
2a) 为所有上传的文件赋予唯一的文件名(即 md5($file['name'])

