简体   繁体   English

如何让用户显示他们已上传的文件

[英]How to let users display the files they have uploaded

I have code for uploading files that is working fine. 我有用于正常上传文件的代码。 When uploading the file to the database I grab the username of the uploader from another database. 将文件上传到数据库时,我从另一个数据库获取上传者的用户名。 I also have a page where I desplay all the uploaded files and it works fine. 我也有一个页面,其中我显示所有上传的文件,并且工作正常。 Now I want to have a page where the user can display only the files they have uploaded them self and be able to delete them. 现在,我想拥有一个页面,用户可以在其中仅显示自己上传的文件并可以删除它们的文件。 How would I go about this? 我将如何处理?

File upload page: 文件上传页面:

<?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';
?>

File proccess page: 文件处理页面:

<?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
}
}
?>

I want a similar page like this that will show the users uploaded files and the display table will include a X for deleting file. 我想要一个类似的页面,该页面将显示用户上传的文件,并且显示表将包含一个X,用于删除文件。 The page where I display all the files: 显示所有文件的页面:

<?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'; 
?>

Step 1) You have to use SQL command to find all uploaded file by the user: 步骤1)您必须使用SQL命令来查找用户上传的所有文件:

$sql="SELECT * FROM file WHERE user_name=".$YOURUSERNAME;

Step 2) Use this PHP code in seperate php file (for example: php_del.php) to delete file: 步骤2)在单独的php文件中使用此PHP代码(例如:php_del.php)删除文件:

$filename=$_GET['filename'];
if (file_exists("filer/mina_filer/".$filename)
    unlink("filer/mina_filer/".$filename);

Step 3) Add Hyperlink to first page like this : 步骤3)像这样将超链接添加到首页:

<a href="php_del.php?filename=<?php echo $row['file'] ?>"</a>

NOTE: Make sure filename has not Space (" ") in $_GET metod. 注意:确保文件名在$ _GET方法中没有空格(“”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM