因此,我一直遵循this关于创建管理CMS的教程,该管理CMS用于通过CMS将内容上传到MySQL数据库。 Dreamweaver,看来工作正常,没有问题,ace。因此,我想:“嗯,一定很容易做一个类似的事情,使我可以将文件上传到特定目录并在数据库中生成URL。”因此,我将“新闻”数据库和管理页面都克隆为用于“文件”-在数据库中添加一个名为“ url”的新字段,即文本。我复制了Wordpress使用的方法,因此每个条目都将类似于“ ../ library / test.pdf”(先返回站点的根目录,然后再返回“ library”部分等)。我在数据库中手动添加了一个测试条目,并将文件移至“库”目录-并使用“ htmlentities”对CMS的“名称”列中文本的“ URL”链接进行了编码-没问题自动显示手动定义的网址-到目前为止效果好吗?
试图查看是否有一种方法可以通过Dreamweaver进行我想做的事情,但这似乎是不可能的。因此尝试了以下this(几乎完全描述了我想做的事情!)...但是答案链接到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> </p>
</body>
非常感谢有人可以提供帮助!虽然不是,但我确定我可以在几个小时内解决这个问题。.但是,这已经让我沮丧了一天,只是因为我认为这样做很简单:P
最佳答案
您确实上传了文件,但没有处理。
每当使用表单上传文件时,文件都会被放置在服务器的临时目录中。您必须将文件从临时目录移动到最终位置(由您选择),否则脚本执行后将再次将其删除。
我认为您最好从简单的表格开始,将文件上传并移动到所需的目录。如果您设法使其正常工作,则可以根据放置文件的目录来构造最终URL。
从遵循directions from 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会添加一行代码以打印出相关的错误代码。当由于某种原因无法移动文件时,它仍然会引发异常。
关于php - 我想通过Dreamweaver将文件上载器添加到现有内容上载器-将pdf上载到特定的文件夹,将url上载到数据库中生成的pdf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22781534/