我有用于上传工作正常的文件的代码。将文件上传到数据库时,我从另一个数据库中获取上传者的用户名。我还有一个页面,我可以显示所有上传的文件,它工作正常。现在我想要一个页面,用户只能显示他们自己上传的文件,并能够删除它们。我该怎么做呢?
文件上传页面:
<?php
include 'template/overall/header.php';
?>
<br><br>
<div class="row">
<div class="large-12 medium-12 small-12 columns" style="background-color: #000000; text-align: center">
<ul class="side-nav" style="background-color: #EEEEEE; border: 1px solid white;">
<h2>Ladda upp en fil</h2>
<br><br>
<form action="add_file.php" method="POST" enctype="multipart/form-data">
<div class="large-12 medium-12 small-12 columns" style="padding-left: 39%">
<input type="file" name="file" value="Bläddra">
</div>
<br><br>
<div class="large-12 medium-12 small-12 columns" style="padding-left: 41.5%; width: 60%">
<input type="submit" name="upload_file" value="Ladda upp">
</div>
</form>
<br><br><br><p>Gå tillbaka till <a href="filer.php">filer</a></p> <br><br>
</ul>
</div>
</div>
<?php
if(isset($_GET['success']))
{
?>
<br><br><br>
<h2 style="text-align: center; color: #FFFFFF">Din uppladdning lyckades!</h2>
<br>
<p style="text-align: center; color: #FFFFFF; font-size: 120%">Klicka <a href="filer.php">här</a> för att se din fil</a></p>
<?php
}
else if(isset($_GET['fail']))
{
?>
<br><br><br>
<h2 style="text-align: center; color: #FFFFFF">Problem vid uppladning av fil!</h2>
<?php
}
else
{
?>
<br><br><br>
<h2 style="text-align: center; color: #FFFFFF">Testa att ladda upp en fil(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</h2>
<p style="text-align: center; color: #FFFFFF; font-size:120%">Om du laddar upp en stor fil som en film tex vänligen komprimerar till rar</p>
<?php
}
include 'template/overall/footer.php';
?>
文件处理页面:
<?php
include 'core/init.php';
if(isset($_POST['upload_file']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="filer/mina_filer/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
/*if (isset($_POST['upload_file'])) {
$session_user_id = $_SESSION['id'];
$user_id = $user_data['id'];
$query = mysql_query("INSERT INTO file(user_name) SELECT `userName` FROM `users` WHERE `id` = $user_id");
} */
if (isset($_POST['upload_file'])) {
$folder = 'filer/mina_filer/';
$session_user_id = $_SESSION['id'];
$user_name = $user_data['userName'];
move_uploaded_file($file_loc,$folder.$final_file);
$sql="INSERT INTO file(file, user_name, type, size, folder) VALUES('$final_file', '$user_name', '$file_type', '$new_size', '$folder')";
mysql_query($sql);
?>
<script>
alert('uppladdning lyckades!');
window.location.href='ladda_fil.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('Error när filen laddades upp!');
window.location.href='ladda_fil.php?fail';
</script>
<?php
}
}
?>
我想要一个类似这样的页面来显示用户上传的文件,显示表将包含一个X用于删除文件。 我显示所有文件的页面:
<?php
include 'template/overall/header.php';?>
<div class="large-12 medium-12 columns">
<table width="60%" border="1" align="center">
<tr>
<th colspan="4"><label style="text-align: center"><b>Uppladdade filer</b></label><label style="text-align: center"><a href="ladda_fil.php"> Ladda upp en ny fil</a></label></th>
</tr>
<tr>
<td>Fil Namn</td>
<td>Fil Typ</td>
<td>Fil Storlek(KB)</td>
<td>Uppladdad av</td>
<td>Öppna</td>
</tr>
<?php
$sql="SELECT * FROM file";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td><?php echo $row['user_name'] ?></td>
<td><a href="filer/mina_filer/<?php echo $row['file'] ?>" target="_blank">Öppna fil</a></td>
</tr>
<?php
}
?>
</table>
</div>
<?php
include 'template/overall/footer.php';
?>
答案 0 :(得分:0)
步骤1)您必须使用SQL命令查找用户所有上传的文件:
$sql="SELECT * FROM file WHERE user_name=".$YOURUSERNAME;
步骤2)在单独的php文件(例如:php_del.php)中使用此PHP代码删除文件:
$filename=$_GET['filename'];
if (file_exists("filer/mina_filer/".$filename)
unlink("filer/mina_filer/".$filename);
步骤3)将超链接添加到第一页,如下所示:
<a href="php_del.php?filename=<?php echo $row['file'] ?>"</a>
注意:确保文件名在$ _GET metod中没有空格(“”)。