我目前正在建立一个网站,我可以上传文件,这个文件可以是图像或文档或应用程序或任何其他类型的文件, 所以一旦我上传文件,它将在我的网站界面上显示列出的文件,文件大小和上传日期。 但我不知道上传它的用户是谁, 但我不知道编码是什么,以便一起显示文件的大小和日期,上传的用户 如果有人帮助PHP代码甚至任何解决方案,将不胜感激 谢谢。
<?php
//Load the settings
require_once("Setting.php");
require_once("db.php");
$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{
$_FILES['file']['tmp_name'];
$target_path = Setting::$uploadFolder;
$target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']);
echo $target_path;
//Try to move the uploaded file into the designated folder
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
{
$message = "The file ". basename( $_FILES['file']['name']). " has been uploaded";
$query ="insert into upload (path) values ('$target_path')";
$dbresult = mysql_query($query,$dblink);
} else{
$message = "There was an error uploading the file, please try again!";
}
}
//Clear the array
unset($_FILES['file']);
if(strlen($message) > 0)
{
$message = '<p class="error">' . $message . '</p>';
}
/** LIST UPLOADED FILES **/
$uploaded_files = "";
//Open directory for reading
$dh = opendir(Setting::$uploadFolder);
//LOOP through the files
while (($file = readdir($dh)) !== false)
{
if($file != '.' && $file != '..')
{
$filename = Setting::$uploadFolder . $file;
$parts = explode("_", $file);
$size = formatBytes(filesize($filename));
$added = date("m/d/Y", $parts[0]);
$origName = $parts[1];
$filetype = getFileType(substr($file, strlen($file) - 3));
$uploaded_files .= "<li class=\"$filetype\"><a href=\"$filename\">$origName</a> $size - $added</li>\n";
}
}
closedir($dh);
if(strlen($uploaded_files) == 0)
{
$uploaded_files = "<li><em>No files found</em></li>";
}
?>
答案 0 :(得分:0)
简单步骤:
答案 1 :(得分:-1)
在W3Schools上看看这个关于PHP文件上传的基础教程: http://www.w3schools.com/php/php_file_upload.asp
与您的文件上传相关的信息将存储在$_FILES
变量
文件大小存储在$_FILES["file"]["size"]
中(其中&#34;文件&#34;是表单字段的名称)。
如果刚刚上传文件,您可以使用PHP date()函数和echo
来获取当前日期。如果您要查找文件的修改时间,可以使用filemtime()。
对于用户,这取决于您的具体应用。