基本上我创建了一组简单的php文件:
允许用户登录。 允许该用户上传文件。 然后允许用户查看他们上传的所有文件。
奇怪的是,当我通过1个用户名上传文件时,文件列表结果显示相同的结果4次然后我上传了第二个文件,它出现了5次。对于另一个用户,它会显示5次。
我检查了上传后文件存储的位置,每个文件只有一个副本。以下是我的代码,任何帮助?
index.php - 这有登录表单,文件上传表格,最后是下载列表
<?
break;
}
?>
<?php if ($_SESSION['username']): ?>
<h1>Welcome, <?php echo $_SESSION["username"] ?></h1></br>
<?php
//include ("config.php");
//Connect to mysql server
$link = mysql_connect($host, $dbuser, $dbpwd);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db($dbname);
if(!$db) {
die("Unable to select database");
}
?>
Select File To Upload
<div style="width:100%; margin:5px;">
<form action="uploadclientfile.php" method="post" enctype="multipart/form-data" name="upload" style="margin:5px;">
<label> File</label>
<input name="uploaded_file" type="file" class="input-xlarge" required/>
<input type="hidden" name="" value="<?php $_SESSION['username'] ?>" />
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><br /><br />
<input name="Upload" type="submit" value="Upload" class="btn" />
</form>
</div></br></br>
File list:</br>
<?php
$userfiles = mysql_query("SELECT filename, filelocation FROM cfiles WHERE userid='{$_SESSION['username']}'" );
while ($row = mysql_fetch_assoc($userfiles)) {
$filename = $row['filename'];
$filelocation = $row['filelocation'];
echo "<a href=".$filelocation .">" .$filename . "</a><br />";
} ?>
<?php endif; ?>
<a href="index.php?action=login">Log-in</a> | <a href="index.php?action=logout">Log-out</a><br />
</body>
</html>
还有upload.php
<?php
session_start();
echo( "<pre>" );
print_r( $_POST );
print_r( $_FILES );
echo( "</pre>" );
$target = "userfiles/";
$target = $target . basename( $_FILES['uploaded_file']['name']);
$new_file_name = str_replace(' ', '_', $target);
//This gets all the other information from the form
$userid = $_SESSION['username'];
$file = basename( $_FILES['uploaded_file']['name'] );
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
//Writes the information to the database
mysql_query("INSERT INTO `cfiles` VALUES ('{$userid}', '{$file}', '{$new_file_name}')") or die( mysql_error() ) ;
}
//Writes the file to the server
if( @move_uploaded_file( $_FILES['uploaded_file']['tmp_name'], $new_file_name ) )
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploaded_file']['name'] ). " has been uploaded, and your information has been added to the directory.";
}
else
{
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
因此,此过程中使用了2个主要文件。有关为什么我的文件多次出现在下载列表和mysql数据库中的任何帮助?它仅在存储的文件夹中出现一次。
答案 0 :(得分:3)
这部分代码:
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
//Writes the information to the database
mysql_query("INSERT INTO `cfiles` VALUES ('{$userid}', '{$file}', '{$new_file_name}')") or die( mysql_error() ) ;
}
循环浏览您拥有的用户数量,并为每个用户添加新的文件条目。因此,如果您有5个用户,则在cfiles
中为已登录的人员$userid
添加5个条目。这将随着更多用户而增加。
如果删除循环并用以下代码替换该代码:
mysql_query("INSERT INTO `cfiles` VALUES ('{$userid}', '{$file}', '{$new_file_name}')") or die( mysql_error() ) ;
您只能获得一个条目
答案 1 :(得分:1)
这段代码让我困惑:
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
//Writes the information to the database
mysql_query("INSERT INTO `cfiles` VALUES ('{$userid}', '{$file}', '{$new_file_name}')") or die( mysql_error() ) ;
}
你想在这做什么?您似乎正在多次将上载的文件插入数据库,每个用户都存在一次。你为什么这样做?这就是文件出现多次的原因吗? (似乎对我而言)