【发布时间】:2011-11-07 11:39:54
【问题描述】:
好的。自 2011 年 8 月 24 日以来,我一直在此站点上搜索。(没关系)寻找一种显示用户已上传文件的方法。我在管理员端有我的表格,一切正常。我还有一个表格,显示用户在表格中填写的任何内容,该表格也可以使用。但我无法在表格中查看文件或其名称。
我的数据库表有一个主 ID auto_increment int(11) unsigned。
这是我写的代码:
//This gets all the other information from the form
$company=$_POST['company'];
$location=$_POST['location'];
$pic=($_FILES['userfile']['name']);
$query = "INSERT INTO user_DB VALUES ('','$company', '$location', '$userfile' )";
//target to the path of my files
$target_path = "uploads/post_id/";
if(!is_dir($target_path)) mkdir($target_path);
$uploadfile = $target_path . basename($_FILES['userfile']['name']);
//Move the uploaded file to $taget_path
(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile));
在您填写详细信息的表格上,我有以下信息:
<tr>
<td><label for="company_name">Company Name</label></td>
<td><input type="text" name="company" id="company" value="" size="38" /></td>
</tr>
<tr>
<td><label for="location">Location</label></td>
<td><input type="text" name="location" id="location" value="" /></td>
</tr>
<tr>
<td>Upload a File:</td>
<td><input name="userfile" id="userfile" type="file" /></td>
</tr>
显示查询结果的前端表是这样的,就是文件 feild。
echo "<td>";
echo "<a href=../admin/uploads/post_id/> $row'userfile'</a>";
echo "</td>";
如您所见,我正在尝试获取文件名以及文件本身。如果它是 pdf/jpg/doc,我应该能够在单击链接时查看/下载它。
蚂蚁的想法...
【问题讨论】:
-
还要确保限制允许的文件类型。如果用户要上传一个 PHP 文件,他们可以在您的服务器上执行它。
-
@freshwire 我怎么能做到这一点?我已经意识到我只想上传 doc/pdf 和 jpg 文件。你有链接吗?也许我可以访问?请帮忙。
-
这有been asked很多次。
标签: php mysql file-upload download