所以我看到了一个奇怪的行为,我询问了服务器故障,但它看起来是一个代码问题。问题是当我使用 MAMP 进行本地测试时,这段代码运行良好,一旦我把它放在 HostGator 上,我就会感到奇怪。
所以过程是
- 上传文件;
- 检查事物的状态;
- 解压文件;
- 读入一个数据文件;
- 复制和缩略图图像;
- 将数据转储到数据库中。
我知道 1 到 5 会发生,因为我可以看到缩略图。奇怪的是我从第 2 步得到一个错误,说文件没有上传。所以看起来整个过程都是从“空白” POST 数据开始的。
所以,第 1 步就是这段代码。发布我的表单时调用它:
function action_upload() {
$ownerName = $this->request->post('ownerName', '');
$ownerEmail = $this->request->post('ownerEmail', '');
$ownerPhone = $this->request->post('ownerPhone', '');
$username = $this->request->post('username', '');
$password = $this->request->post('password', '');
$treeName = $this->request->post('treeName', '');
$error = $this->util->process_datafile($ownerName, $ownerEmail, $ownerPhone, $username, $password, false, $treeName);
if ($error != "") {
echo json_encode(array(
'error' => $error,
));
} else {
echo json_encode(array(
'gotoURL' => "/" . $treeName,
));
}
exit;
}
该操作读取一些表单字段并调用一个process_datafile
处理上传文件的函数。下面是那个函数,我收到的错误来自第 9 行,“没有提供树名”。但我知道它在某些时候会超越那个错误。
public function process_datafile($ownerName, $ownerEmail, $ownerPhone, $username, $password, $update, $treeName) {
// Make sure we have a tree name
if ($treeName != "") {
$this->scriptPath = dirname(__FILE__);
$this->treePath = dirname(dirname($this->scriptPath)) . "/assets/trees/" . $treeName . "/";
$this->tempFilePath = dirname(dirname($this->scriptPath)) . "/assets/temp/" . $this->guid() . "/";
} else {
return "No tree name provided";
}
// Check to make sure the tree is in the expect condition
$treeExists = false;
if (file_exists($this->treePath)) {
$treeExists = true;
}
if ($treeExists && !$update) {
return "Tree name already exists " . $this->treePath;
} else if (!$treeExists && $update) {
return "Tree does not exists";
}
// Make sure there are no upload errors
if ($_FILES['treeFile']['error'] == '1' || $_FILES['treeFile']['error'] == '2') {
return "File size to large, try to upload your tree without media.";
} else if ($_FILES['treeFile']['error'] != '0') {
return "File upload error: " . $_FILES['treeFile']['error'];
}
// Move the uploaded file
if (!file_exists($this->tempFilePath)) {
mkdir($this->tempFilePath, 0700, true);
}
$name = $_FILES["treeFile"]["name"];
$tempfile = $this->tempFilePath . $name;
copy($_FILES['treeFile']['tmp_name'], $tempfile);
// Make sure it is something we can deal with
$finfo = finfo_open(FILEINFO_MIME);
$fileparts = explode(";", finfo_file($finfo, $tempfile));
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$filetype = $fileparts[0];
$valid = "text/plain,image/png";
if (($filetype != "text/plain" && $filetype != "application/zip") || ($ext != "ged" && $ext != "zip")) {
return "Only gedcom (.ged) or archive (.zip) files may be uploaded.";
}
$gedfile = $tempfile;
$archive_tmp = "";
if ($filetype == "application/zip" && $ext == "zip") {
$archive_tmp = $this->tempFilePath . "archive/";
if (!file_exists($archive_tmp)) {
mkdir($archive_tmp, 0700, true);
}
// Extract the archive
$zip = new \ZipArchive;
$res = $zip->open($tempfile);
if ($res === TRUE) {
$zip->extractTo($archive_tmp);
$zip->close();
} else {
$this->delTree($archive_tmp);
return "Error processing archive";
}
// Find the gedcom
$found = false;
$it = new \RecursiveDirectoryIterator($archive_tmp);
foreach(new \RecursiveIteratorIterator($it) as $file)
{
$file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (strtolower($file_ext) == "ged") {
$gedfile = $file;
$found = true;
}
}
if (!$found) {
$this->delTree($archive_tmp);
return "Could not find gedcom (.ged) file in archive.";
}
}
// Make the tree folder if needed
if (!file_exists($this->treePath)) {
mkdir($this->treePath, 0700, true);
}
$this->mediaPath = $this->treePath . "media/";
$this->delTree($this->mediaPath);
if (!file_exists($this->mediaPath)) {
mkdir($this->mediaPath, 0700, true);
}
if (file_exists($this->treePath . "tree.ged")) {
unlink($this->treePath . "tree.ged");
}
copy($gedfile, $this->treePath . "tree.ged");
// Deal with the database
if (!$this->create_database($ownerName, $ownerEmail, $ownerPhone, $username, $password, $update)) {
return "Could not open database";
}
// Process the gedcom
$this->process_gedcom($this->mediaPath, $archive_tmp);
// Remove the temp folder
$this->delTree($this->tempFilePath);
return "";
}
我知道在某些时候它会进入process_gedcom
缩略图发生的位置......我也知道它永远不会进入,foreach ($ged->people as $person)
因为数据库中没有条目。
private function process_gedcom($mediaPath, $archivePath) {
// Insert statements
$personInsert = "INSERT INTO people (id, gender, first, last, middle, title, suffix) VALUES (:id, :gender, :first, :last, :middle, :title, :suffix)";
$nameInsert = "INSERT INTO names (personID, `type`, first, last) VALUES (:id, :type, :first, :last)";
$familyInsert = "INSERT INTO families (id, personAID, personBID) VALUES (:id, :personAID, :personBID)";
$childInsert = "INSERT INTO children (familyID, `type`, personID) VALUES (:familyID, :type, :personID)";
$eventInsert = "INSERT INTO events (personID, familyID, `type`, date, place, description) VALUES (:personID, :familyID, :type, :date, :place, :description)";
$factInsert = "INSERT INTO facts (personID, name, value) VALUES (:personID, :name, :value)";
$mediaInsert = "INSERT INTO media (id, file, `type`, title) VALUES (:id, :file, :type, :title)";
$peopleMediaInsert = "INSERT INTO people_media (mediaID, personID) VALUES (:mediaID, :personID)";
$familyMediaInsert = "INSERT INTO family_media (mediaID, familyID) VALUES (:mediaID, :familyID)";
// Load in the gedcom file
$ged = new \App\Gedcom();
$ged->import($this->treePath . "tree.ged", array($this, 'log'));
// Add objects to the database
foreach ($ged->objects as $obj) {
$file = $this->findFile($obj->getFilename(), $archivePath);
if ($file !== false) {
$finfo = finfo_open(FILEINFO_MIME);
$fileparts = explode(";", finfo_file($finfo, $file));
$filetype = $fileparts[0];
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$hash = md5_file($file);
copy($file, $mediaPath . $hash . "." . $ext);
$this->makeThumb($mediaPath . $hash . "." . $ext, 200, 200, "thumb");
$this->makeThumb($mediaPath . $hash . "." . $ext, 1024, 768, "resized");
$this->database($mediaInsert, array(':id' => $obj->getId(),
':file' => $hash . "." . $ext,
':type' => $filetype,
':title' => $obj->getTitle()));
}
}
// Add people to the databsse
foreach ($ged->people as $person) {
$this->database($personInsert, array(':id' => $person->getId(),
':gender' => $person->getGender(),
':first' => $person->getFirstName(),
':last' => $person->getLastName(),
':middle' => $person->getMiddleName(),
':title' => $person->getTitleName(),
':suffix' => $person->getSuffixName()));
更多数据插入...
什么会导致事情重新启动,因为它看起来像是process_datafile
两次调用,一次是有效输入,第二次一切都是“空白”?