2012-07-07 51 views
0

对不起,如果这是愚蠢的,但只是寻找一些真正的帮助。为此奋斗。上传的文件不会从表(PHP,MySQL)打开(不正确的目录)

我有一个HTML脚本,上传了一个名为minegem.html文件,该文件提交电话minegem.php时,此脚本上传表单中的数据插入表,上传文件到一个目录,并为用户提供了一个表,可以看到上述数据。这一切都很好。

<?php 

//define variables to be used 
$db_host = 'localhost'; 
$db_user = 'root'; 
$db_pwd = ''; 

$database = 'minetech'; 
$table = 'minegem'; 
$directory = 'uploads/minegem/'; 

//This gets all the other information from the form 
$name=$_POST['docname']; 
$version=$_POST['docver']; 
$date=$_POST['docdate']; 
$type=$_POST['doctype']; 
$author=$_POST['docauth']; 

//target directory is assigned 
$target = $directory; 
$target = $target . basename($_FILES['uploaded']['name']) ; 

//if everything is ok upload the file 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename($_FILES['uploaded']['name']). " has been uploaded"; 
} 
else { 
echo "Sorry, there was a problem uploading your file."; 
} 

//connect to sql 
$con = mysql_connect("$db_host","$db_user","$db_pwd"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

//connect to database 
mysql_select_db("$database", $con); 

//insert data from form to database 
$sql="INSERT INTO $table (DocName, DocVer, DocDate, DocType, DocAuth, DocLoc) 
VALUES 
('$name','$version','$date','$type','$author','$target')"; 

//confirm data entry 
if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
echo " and new record added. How cool is that."; 


//the following script displays the data for test purposes 


//this script will show table data 
//retrieve tables values 
$result = mysql_query("SELECT * FROM {$table}"); 
if (!$result) { 
    die("Query to show fields from table failed"); 
} 

//build table and define headings 
echo "<table border='1'> 
<tr> 
<th>Name</th> 
<th>Version</th> 
<th>Upload Date</th> 
<th>Type</th> 
<th>Uploader</th> 
<th>Location</th> 
</tr>"; 


// printing table rows 
while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
echo "<td>" . $row['DocName'] . "</td>"; 
echo "<td>" . $row['DocVer'] . "</td>"; 
echo "<td>" . $row['DocDate'] . "</td>"; 
echo "<td>" . $row['DocType'] . "</td>"; 
echo "<td>" . $row['DocAuth'] . "</td>"; 
echo "<td>" . $row['DocLoc'] . "</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

mysql_free_result($result); 

//close connecition to database 
mysql_close($con) 
?>` 

位于C:/wamp/www/文件的两个,所以当我通过网络浏览器运行它们它显示为localhost/minegem.php

我有一个最终的脚本,这将是一个我实际运行表明最终用户结果。

<?php 

//define variables to be used 
$db_host = 'localhost'; 
$db_user = 'root'; 
$db_pwd = ''; 

$database = 'minetech'; 
$table = 'minegem'; 
$type = 'Guideline'; 

//connect to sql 
$con = mysql_connect("$db_host","$db_user","$db_pwd"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

//connect to database 
mysql_select_db("$database", $con); 

//this script will show table data 
//retrieve tables values 
$result = mysql_query("SELECT * FROM $table 
WHERE DocType='Guideline'"); 
if (!$result) { 
    die("Query to show fields from table failed"); 
} 

//build table and define headings 
echo "<table border='1'> 
<tr> 
<th>Document Name</th> 
<th>Version</th> 
</tr>"; 


// printing table rows 
while($row = mysql_fetch_array($result)) 
{ 
$docname=$row['DocName']; 
$docver=$row['DocVer']; 
$doctype=$row['DocType']; 
$docloc=$row['DocLoc']; 

echo "<tr>"; 
echo '<td><a href='.urlencode($docloc).'>'.$docname.'</a></td>'; 
echo "<td>$docver</td>"; 
echo "</tr>"; 


} 
echo "</table>"; 


mysql_free_result($result); 

//close connecition to database 
mysql_close($con) 

?> 

我的第一个表显示该文件的位置为uploads/minegem/test document.pdf 第二个表显示,作为一个链接显示在地址栏http://localhost/uploads%2Fminegem%2Ftest+document.pdf 并在页面是说 The requested URL /uploads/minegem/test+document.pdf was not found on this server.

我想这是一个愚蠢的文件结构问题,但其至关重要。我最终会把它放在服务器上,以便能够存储完整的文件补丁,并回想一下,作为一个链接会很好。我希望有人能帮助我指出正确的方向,设置正确的文件结构。谢谢。

+0

好,我已经得到了它的工作首先给我分配的变量$ a文件'$文件= $ _ FILES [“上传”] [“名”]。'然后我使用该变量在表中存储,而不是$ target。现在,当我打电话给我时,我可以rawcodeurl $ docloc这只是包含扩展名的文件名,我手动添加了文件夹位置。我可以简化这个并为此添加一个变量'echo'​​'.$docname.'';'现在我的文件正在链接并正确打开。感谢所有的帮助。 – Hux 2012-07-08 00:43:31

回答

0

改为使用rawurlencode。这用空格替换%20而不是加号。

+用于传输数据,%20用于显示用于点击的URL。尽管您始终可以使用%20进行数据传输,但如果浏览器不正确地支持,您只能使用+。所以坚持rawurlencode。

另外,rawurlencode仅在插入到数据库之前才是基本名称部分。这留下了未经过编码的路径。我不认为这是打破你的代码,但它是一个很好的。


回答Q)如何rawurlencode只是基地名。

A)只需将基准名称存储在数据库中。你知道路径($目录),所以你可以使用move_uploaded_file(...,$ directory。$ target);然后输出文件的位置时,使用A HREF = “HTTP://localhost/'.$directory.rawurlencode($目标)“。”


但一对夫妇说,还有其他的 “问题”很重要。

  1. 当您将文本输出到屏幕以进行显示时,请确保在其上使用了httpspecialchars()。

    [a href =“http://localhost/'.$directory。//使用尖括号替换方括号 - 不能在这里输入尖括号

  2. 保存到数据库至少使用“mysql_real_escape_string”来安全地进入数据库,但它是很强建议使用PDO或mysqli(mysql函数被折旧,其中PDO和mysqli在设计上都更安全)

+0

谢谢罗比。这听起来像它会做的伎俩。我怎么能rawurlencode只是基地名称? – Hux 2012-07-08 00:17:08

+0

在答案中增加了更多内容。包括一些提示,以便将数据输入数据库以及在屏幕上显示信息的安全性。 – Robbie 2012-07-08 01:58:14

0

将文件名作为test document.pdf是否至关重要?你也可以在PHP中重命名文件:

<?php 
//your upload script here 



$filename = $_POST['filename']; //change this to the variable which stores filename 
$new_filename = str_replace(' ', '_', $filename); 
rename('c:/wamp/uploads/minegem/'.$filename, 
     'c:/wamp/uploads/minegem/'.$new_filename); 

?> 

然后尝试再次访问它。当您在浏览器上访问它们时,文件名空间真的很麻烦。

+0

我想设置一个站点,文件等可以由用户(其封闭的网站)上传到服务器,并生成一个表格,显示链接到该文件的文件名。然后用户可以点击该链接并打开它。我是一个PHP等完整的noob,我觉得这只是一个目录问题。我不需要能够在浏览器中查看文件,只需从我的表格中的链接访问它们即可。 – Hux 2012-07-07 23:51:48

+0

我已经添加了一个额外的行到我的决赛桌显示这个; '回波 '​​'.$docname.'';' 现在,当我悬停在链接它显示'文件:/// C:/瓦帕/网络/ MINEGEM%2F /上传%2Ftest + document.pdf' 我会认为这是正确的,但它仍然没有加载文件。点击链接什么也不做。 – Hux 2012-07-08 00:11:23

+0

上面的代码应该放在上传脚本后面,以便将上传的文件重命名为文件名中没有空格的文件。然后,表中的文件名将全部具有下划线而不是空格。如果有什么不清楚的地方,请告诉我,以便我可以进一步解释。 – 2012-07-08 00:14:09