如何在PHP中列出文件

如何在PHP中列出文件,php,mysql,Php,Mysql,我想知道如何在PHP中上传文件、列出和下载文件 我希望用户上传/列出/下载文件,例如:文件“Assignment Guide.doc” 下面是我上传、列出、下载的代码。 下面的代码成功地允许用户上载、列出和下载文件。但是在资源管理器的数据库、列表页和目录中显示的文件名是“AssignmentGuide.doc”,而不是“Assignment Guide.doc”。我想知道是否有代码使数据库、列表页面和资源管理器目录中显示的文件名为“Assignment Guide.doc” 上传文件proces

我想知道如何在PHP中上传文件、列出和下载文件

我希望用户上传/列出/下载文件,例如:文件“Assignment Guide.doc”

下面是我上传、列出、下载的代码。 下面的代码成功地允许用户上载、列出和下载文件。但是在资源管理器的数据库、列表页和目录中显示的文件名是“AssignmentGuide.doc”,而不是“Assignment Guide.doc”。我想知道是否有代码使数据库、列表页面和资源管理器目录中显示的文件名为“Assignment Guide.doc”

上传文件process.php

<?php
    require("connection.php");
    session_start();
    $username = $_SESSION['UserName'];

    //This is the directory where files will be saved
    $filename = basename($_FILES['file']['name']);
    $filesize = intval($_FILES['file']['size']);
    $filenospace = str_replace(' ','',$filename); 
    $udir= "users/".$username."/";
    $ufile = $udir .$filenospace;

    //This gets all the other information from the form 
    $file = ($_FILES['file']['name']);
    $filenospace1 = str_replace(' ','',$file); 

    //Writes the information to the database 
    mysql_query("INSERT INTO files(UserName,FileName,Path,Size,Date) VALUES('$username', '$filenospace1', '$udir','$filesize',NOW())") ; 

    //Writes the file to the server 
    if(move_uploaded_file($_FILES['file']['tmp_name'], $ufile)){
        //Tells you if its all ok
        header('location:upload.php?feedback3=uploadsuccessful');
    }
    else {
        //Gives and error if its not
        header('location:upload.php?feedback3=uploaderror');
    }
?>

似乎您正在插入$filenospace1,但对于这一行,您将不使用空格替换空格:

$filenospace1 = str_replace(' ','',$file);
不要执行此“str_replace”操作,也不要在SQL语句中插入$file而不是$filenospace1:

INSERT INTO files(UserName,FileName,Path,Size,Date) VALUES('$username', '$file', '$udir','$filesize',NOW())
最后,要显示数据:

//Replace this line
$string_space = str_replace('','&nbsp;',$filename);
//by this one
$string_space = str_replace(' ','&nbsp;',$filename);

那么,我只使用第二行代码?不需要添加第一行吗?这一行怎么样:$filenospace=str_replace(“”,$filename);--别管了?顺便说一句,我已经试过你的建议了,而且很管用。但是,当我查看文件浏览器时,文件仍然是没有空间的文件名,而且当我单击“下载链接”时,它不起作用……知道吗?你说的“文件浏览器”是什么意思吗?我想太晚了,你的文件“Assignment Guide.doc”已经存储在数据库中,没有空间了。选中此项,并在必要时更新记录以添加空格
//Replace this line
$string_space = str_replace('','&nbsp;',$filename);
//by this one
$string_space = str_replace(' ','&nbsp;',$filename);