对不起,如果这是愚蠢的,但只是寻找一些真正的帮助。为此奋斗。上传的文件不会从表(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.
我想这是一个愚蠢的文件结构问题,但其至关重要。我最终会把它放在服务器上,以便能够存储完整的文件补丁,并回想一下,作为一个链接会很好。我希望有人能帮助我指出正确的方向,设置正确的文件结构。谢谢。
好,我已经得到了它的工作首先给我分配的变量$ a文件'$文件= $ _ FILES [“上传”] [“名”]。'然后我使用该变量在表中存储,而不是$ target。现在,当我打电话给我时,我可以rawcodeurl $ docloc这只是包含扩展名的文件名,我手动添加了文件夹位置。我可以简化这个并为此添加一个变量'echo''.$docname.'';'现在我的文件正在链接并正确打开。感谢所有的帮助。 – Hux 2012-07-08 00:43:31