我想知道如何在PHP中上传文件,列出和下载文件?
我希望用户上传/列出/下载文件,例如:文件“Assignment Guide.doc”。
以下是我上传,列出,下载的代码。 以下代码成功让用户上传,列出和下载文件。但是,资源管理器中的数据库,列表页面和目录中显示的文件名是“AssignmentGuideline.doc”而不是“Assignment Guide.doc”。我想知道是否有任何代码使得数据库,列表页面和资源管理器中的目录显示的文件名是“Assignment Guideline.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');
}
?>
userpage.php - 其中显示上传的文件及其下载链接
<div class="box6">
<h3>File Lists</h3>
<?php
echo '<table border="1px">
<tr>
<td width="50px"><b>Name</b></td>
<td width="60px"><b>Size (bytes)</b></td>
<td><b>Date</b></td>
<td><b>Download</b></td>
<td><b>Sharing</b></td>
</tr>';
include_once("connection.php");
$sqlUpload = mysql_query("SELECT * FROM files WHERE UserName = '$username'");
while($row=mysql_fetch_array($sqlUpload)){
$filename = $row['FileName'];
$path = $row['Path'];
$size = $row['Size'];
$date = $row['Date'];
$string_space = str_replace('',' ',$filename);
echo '
<tr>
<td><b>'.$row['FileName'].'</b></td>
<td><b>'.$row['Size'].'</b></td>
<td><b>'.$row['Date'].'</b></td>
<td><b><a href='.$row['Path'].''.$string_space.'>Download</a></b></td>
<td><b> <form action="userpage.php" method="post">
<input name="user_name" placeholder="username"> <input type="submit" value="Share" /> </form> </b><td>
</tr>';
}
?>
<table width="650">
<tr>
<td>
</td>
</tr>
</table>
<?php
include_once("connection.php");
if(isset($_POST['user_name'])){
$username_friend = $_POST['user_name'];
$pathnew = 'users/'.$username_friend.'/';
copy(''.$path.''.$filename.'', 'users/'.$username_friend.'/'.$filename.'');
$sqlshare= "INSERT INTO files(UserName,FileName,Path,Size,Date) VALUES('$username_friend', '$filename', '$pathnew','$size',NOW())";
if(mysql_query($sqlshare)){
echo "success sharing";
}
else {
echo "Failed Sharing";
}
}
?>
</div>
答案 0 :(得分:0)
似乎您正在插入$ 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('',' ',$filename);
//by this one
$string_space = str_replace(' ',' ',$filename);