我想通过 Dreamweaver 将文件上传器添加到现有内容上传器 - 将 pdf 上传到特定文件夹,网址到数据库中生成


I want to add file uploader to existing content uploader via Dreamweaver - upload pdf to specific folder, url to pdf generated in database

所以我一直在按照本教程创建管理员 CMS 以通过以下方式将内容上传到 MySQL 数据库。织梦者,它似乎工作正常,没有问题,王牌。所以我想,"嗯,做一个类似的操作一定很容易,它允许我将文件上传到特定目录并在数据库中生成 URL"所以我克隆了"新闻"数据库和管理页面以用于"文件"——在数据库中添加一个名为"url"的新字段,它只是文本。我复制了Wordpress使用的方法,因此例如每个条目都将类似于"../library/test.pdf"(回到站点的根目录,然后回到"库"部分等)。我在数据库中手动添加了一个测试条目并将文件移动到"库"目录 - 并使用"htmlentities"对 CMS "名称"列中文本的"url"链接进行编码 - 它没有问题自动显示手动定义的 url - 到目前为止一切顺利?

试图看看是否有办法通过 Dreamweaver 做我想做的事情,但这似乎是不可能的。所以尝试遵循这个(这几乎准确地描述了我想做的事情!!...但是答案链接到一个 about.com 教程 - 我在线程中读到的是劣质的clode - 这解释了为什么它不起作用!:'(

所以,这是我的代码(好吧,我在第一段之后做了什么)......(不包括与DTAbase的连接器)

.PHP

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
 }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO library (name, `description`, url) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['url'], "text"));
  mysql_select_db($database_tectanet, $tectanet);
  $Result1 = mysql_query($insertSQL, $tectanet) or die(mysql_error());
  $insertGoTo = "manage_files.php";
  if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
 }

?>

无论如何,重要的"正文"位都是HTML

<body>
<h1>Add file</h1>
<p><a href="index.php">Admin Menu</a></p>
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data"    name="form1" id="form1">
    <p>
      <strong>
    <label for="name">name</label> 
    :
    <br />
<input name="name" type="text" class="titleinput" id="name" maxlength="150" />
  </strong> </p>
  <p><strong>desc</strong>:<br />
 <textarea name="description" id="description" cols="45" rows="5"></textarea>
  </p>
  <p>
    <input type="file" name="url" id="url" />
  </p>
<p>
  <input type="submit" name="insert" id="insert" value="Post" />
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>

如果有人能帮忙,非常感谢!虽然如果没有,我相信我可以在几个小时内弄清楚这一点。但这已经让我沮丧了一天,因为我认为这很容易做:P

您确实上传了文件,但不处理它。

每当使用表单上传文件时,都会将其放置在服务器的临时目录中。您必须将文件从临时目录移动到其最终位置(由您选择),否则它将在脚本执行后再次删除。

我认为您最好从一个简单的表单开始上传文件并将其移动到您想要的目录中。如果您设法让它正常工作,那么您可以根据文件放置的目录构建最终网址。

首先按照 php.net 的指示进行操作。在您的情况下,保存脚本将如下所示:

$target_directory = realpath('/path/to/http_root/files') . DIRECTORY_SEPARATOR;
$target_file = $target_directory . basename($_FILES['url']['name']);
if($_FILES['url']['error'] > 0) {
    echo 'An error occured while uploading your file... Check the error code ('. $_FILES['url']['error'] .') at <a href="http://nl1.php.net/manual/en/features.file-upload.errors.php">http://nl1.php.net/manual/en/features.file-upload.errors.php</a> to find out what went wrong.<br>';
} else {
    if (!move_uploaded_file($_FILES['url']['tmp_name'], $target_file)) {
        throw new Exception('Something went wrong while saving the uploaded file');
    }
    $target_directory_url = 'http://www.mywebsite.com/files/'; // url to the $target_directory
    $target_file_url = $target_directory_url . basename($_FILES['url']['name']);
    echo 'You can find your uploaded file at '. $target_file_url;
}

如果我可以建议你;只将文件名保存在数据库中。由于您知道它存储在哪个目录中,因此您不需要数据库中的完整 url 或路径。当你移动目录(以及url)时,你只需要改变为你构造url的函数,而不必做一些复杂的更新查询来替换数据库中的完整路径。

EDIT 添加了一行代码,以便在上传时出现问题时打印出相关的错误代码。当由于某种原因无法移动文件时,它仍然会引发异常。