我开发了一个网站,除其他功能外,用户可以在单个“数据集”中上传文件。对于这个特性,我创建了一个特定的 php 页面('update.php'),用户可以在其中定义这个数据集的名称并上传他的文件。然后,这些文件存储在一个目录中,我用相关数据(数据集名称、他上传的每个文件的标志等)更新了一个简单的 MySQL 数据库。我已经在 Windows 8 上的 XAMPP 中开发和测试了该网站。问题是现在,我已经在 Apache 中迁移了整个项目(在 Ubuntu 10.04 LTS 上)我在创建数据集和上传时遇到了奇怪的问题的文件。最奇怪的问题是,当我在 PHP 变量中 POST 数据集的表单文本字段时,它始终为空,尽管我每次都有一个默认值。更重要的是,尽管我已授予文件夹和网站的适当权限(chmod 0777,将我登录的用户添加到“www-data”组等),但没有任何文件上传到目录。我已经在 Google 上搜索过这种奇怪的行为,但没有一个页面有帮助。此外,我确信这不是 PHP 错误,但为了您的帮助,这里有一个来自“update.php”的简单代码片段:
if($submit == "data") {
$old_ds_name = $_GET['name'];
$type = $_GET['type'];
$ds_name = $_POST['ds-name'];
$connect = mysql_connect("localhost", "user", "password");
if(!$connect) {
die('Connection error: ' . mysql_error());
}
$select = mysql_select_db("db", $connect);
if(!$select) {
die('Invalid DB: ' . mysql_error());
}
//Check names
if($old_ds_name != $ds_name) {
$query1 = "SELECT name FROM datasets WHERE name='$ds_name'";
$result1 = mysql_query($query1);
$do_name_check = mysql_num_rows($result1);
if($do_name_check > 0) {
?>
<script type="text/javascript">
var ds2 = '<?php echo $ds_name; ?>';
alert('Dataset '+ds2+' already exists. Please choose a different name.');
<?php
mysql_close();
?>
window.history.back();
</script>
<?php
}
}
$query2 = "SELECT * FROM datasets WHERE name='$old_ds_name'";
$result2 = mysql_query($query2);
$dir_name = mysql_result($result2,0, "dir_name");
if($type == 0) { //Normal dataset
$neo4j_f = mysql_result($result2,0, "neo4j");
if(isset($_POST['mentions']) && $_POST['mentions'] == "Yes") {
$mentions = "1";
}
mysql_close();
if($_FILES["neo4j"]["name"]) {
$filename = $_FILES["neo4j"]["name"];
$source = $_FILES["neo4j"]["tmp_name"];
$type = $_FILES["neo4j"]["type"];
$neo4j_f = uploadNeo4j($filename, $source, $type, $dir_name, $neo4j_f);
if($neo4j_f == -1) {
die('<script type="text/javascript">
alert("The Neo4j DB file you are trying to upload is not a .zip file.");
window.history.back();
</script>');
}
elseif($neo4j_f == -2) {
die('<script type="text/javascript">
alert("An error occured while uploading youe Neo4j DB file. Please try again.");
window.history.back();
</script>');
}
else {
$neo4j_f = "1";
}
}
$ds_name1 = mysql_real_escape_string($ds_name);
$old_ds_name1 = mysql_real_escape_string($old_ds_name);
if($old_ds_name1 != $ds_name1) {
$query = "UPDATE datasets SET name='$ds_name1', neo4j='$neo4j_f', mentions='$mentions' WHERE name='$old_ds_name1'";
} else {
$query = "UPDATE datasets SET neo4j='$neo4j_f', mentions='$mentions' WHERE name='$old_ds_name1'";
}
}
$connect = mysql_connect("localhost", "user", "password");
if(!$connect) {
die('Connection error: ' . mysql_error());
}
$select = mysql_select_db("evo_com", $connect);
if(!$select) {
die('Invalid DB: ' . mysql_error());
}
$result = mysql_query($query);
if(!$result) {
die('Invalid query 1: ' . mysql_error());
}
mysql_close();
}
'uploadNeo4j()' PHP 函数:
function uploadNeo4j($filename, $source, $type, $dir_name, $neo4j_f) {
include 'filepaths.php';
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
$okay = false;
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
return -1;
}
$target_path = "uploads/".$filename;
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$dir = $DEFAULT_ROOT_PATH.$DATA_PARENT_FOLDER.$DBs.$dir_name.$NEO_ROOT.$NEO_FOLDER;
rrmdir($dir);
$zip->extractTo($DEFAULT_ROOT_PATH.$DATA_PARENT_FOLDER.$DBs.$dir_name.$NEO_ROOT.$NEO_FOLDER);
$zip->close();
$lucene_neo = $DEFAULT_ROOT_PATH.$DATA_PARENT_FOLDER.$DBs.$dir_name.$NEO_ROOT.$NEO_FOR_LUCENE_FOLDER;
recursive_copy($dir, $lucene_neo);
unlink($target_path);
$neo4j_f = 1;
return $neo4j_f;
}
} else {
return -2;
}
}
以及相关的 HTML 代码:
<form id="editdata" enctype="multipart/form-data" action="update.php&action=edit&submit=data&name=<?php echo $_GET['name']; ?>&type=<?php echo $_GET['type']; ?>" method="post">
<table style="margin:inherit" cellspacing="15px">
<tr>
<td><div id="text-label">Name of dataset:</div></td>
<td><input type="text" id="ds-name" name="ds-name" value="<?php echo $_GET['name']; ?>" required="required"/></td>
</tr>
<tr>
<td><div id="text-label">Date of creation:</div></td>
<td class="desc"><?php $date = new DateTime($_GET['date']); echo $date->format('d.m.Y H:i:s');?></td>
</tr>
<?php
if($_GET['type'] != 1) {
?>
<tr>
<td><div id="text-label">Type of dataset:</div></td>
<td class="desc">Type 1</td>
</tr>
<tr>
<td><div id="text-label">Neo4j Graph DB:</div></td>
<?php
if($_GET['neo4j'] == "1") {
?>
<td><div id="text-label-exists">UPLOADED</div></td>
<?php
}else {
?>
<td><div id="text-label-not-exists">DOESN'T EXIST</div></td>
<?php
}
?>
<td><input type="file" id="neo4j" name="neo4j"/></td>
</tr>
<tr>
<td class="text-label">Relationships:</td>
<td>
<div class="desc"><input name="mentions" type="checkbox" <?php if(isset($_GET['mentions']) && $_GET['mentions'] == "1") { echo "checked"; }?> value="Yes"/>Mentions
</td>
</tr>
</table>
<br/>
<input style="margin-left: 15px;" id="saveChanges" class="button_text" type="submit" name="submit" value="Save changes"/>
</form>
我已将 Apache 配置为接受更大的文件并允许更大的执行时间,我已向我的 MySQL 用户授予完全权限,但问题并未解决。我所指的文本字段是$_POST['ds-name]
你可以看到有一个默认值的地方,但在 PHP 中是空的。echo
此外,当 PHP 执行进入if($_FILES['neo4j']['name'])
但从未显示时,我已经包含了一个函数。提前致谢。