Apache Chemistry 在创建包含空格的文件夹时失败
Apache Chemistry fails when creating a folder with blank spaces
我正在使用 API_Alfresco,这是一个通过 Apache Chemistry 连接到 Alfresco 的库,但是当我尝试创建包含空格的文件夹时出现以下错误:
Uncaught CmisRuntimeException in C:\xampp\htdocs\API_Alfresco\cmis_repository_wrapper.php:176 Stack trace:
#0 C:\xampp\htdocs\API_Alfresco\cmis_repository_wrapper.php(207): CMISRepositoryWrapper->convertStatusCode(505, '')
#1 C:\xampp\htdocs\API_Alfresco\cmis_service.php(791): CMISRepositoryWrapper->doGet('...')
#2 C:\xampp\htdocs\API_Alfresco\APIAlfresco.php(98): CMISService->getObjectByPath('...', Array)
#3 C:\xampp\htdocs\phpCMISalfresco.php(33): APIAlfresco->setFolderByPath('...')
#4 {main} thrown in C:\xampp\htdocs\API_Alfresco
当我尝试创建一个没有空格的文件夹时,它工作正常。这是我的代码:
<?php
require 'APIAlfresco/APIAlfresco.php';
include 'connectAlfresco.php';
$parent_name = $_POST['parent']; //FAILS IF IT HAS BLANK SPACES
$uploaded_file = basename($_FILES['att_file']['name']); //Uploaded
$base_folder = "/path/to/site/folder/"
if (move_uploaded_file($_FILES['att_file']['tmp_name'], $uploaded_file)) {
$conexion->setFolderByPath($base_folder); //Set base folder where files will be uploaded
/*Check if dir already exists and create it if it doesn't */
if($conexion->existsFolder($parent_name)) echo "Parent".$parent_name." already exists";
else $conexion->createFolder($parent_name);
/*Move uploaded file into folder */
$conexion->setFolderByPath($base_folder."/".$parent_name); //ERROR HERE IF $parent_name HAS BLANK SPACES
$conexion->uploadFile($uploaded_file);
}
echo 'File infor:';
print_r($_FILES);
?>
我查看了错误跟踪并意识到 doRequest() 在 doGet() 中调用(在 cmis_repository_wrapper.php) 在 HTTP 请求中返回错误(仅当文件夹包含空格时失败)。由于我还没有对库进行编码,所以我似乎无法弄清楚可能出了什么问题。有什么线索吗?会不会是bug?
还进行了一些调试,发现 doRequest() 正在接受一个 URL,例如:
http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom/path?path=/Sites/mysite/documentLibrary/Folder WithBlankSpaces&filter=&includeAllowableActions=&includeACL=&includePolicyIds=&includeRelationships=&renditionFilter=
在这个 link 中,我注意到可能触发错误的两件事:
1) "Folder WithBlankSpaces" 文件夹将 URL 一分为二(很可能是实际问题)
2) url 最后一部分的变量未设置
我怀疑 1) 可能是真正的问题,因为如果我强制该文件夹没有空格(调用 str_replace(' ', '', $parent_folder)
)那么它将起作用很好。
我怎样才能让它与文件夹 和 空格一起使用?有什么办法可以修改 URL 使空格不会将其一分为二吗?当然,如果用户试图创建 "My Folder",他想看到的是 "My Folder" 目录而不是 "MyFolder" 目录。
提前致谢!
我自己想出来了。
这个想法是在发出 HTTP 请求之前将空格强制为“%20”,然后再次将“%20”替换为空格。
基本上 $conexion->setFolderByPath($parent_name)
发出一个 HTTP 请求(这就是当 $parent_name
包含空格时触发错误的地方)。
因此,在调用该方法之前,$parent_name
空格需要替换为“%20”,以便 HTTP 请求知道 url 中有一个 "blank space",表示为 % 20.
一旦 HTTP 请求完成(在调用 setFolderByPath() 之后),“%20”需要被替换回空格,然后调用 $conexion->createFolder($parent_name);
因此文件夹是用实际的空格创建的,并且不是“%20”。
编辑:我发布固定代码以使其清晰。
<?php
require 'APIAlfresco/APIAlfresco.php';
include 'connectAlfresco.php';
$parent_name = $_POST['parent'];
$parent_name = str_replace(' ', '%20', $parent_name); //PREPARE IT FOR THE HTTP REQUEST
$uploaded_file = basename($_FILES['att_file']['name']); //Uploaded file
$base_folder = "/path/to/site/folder/";
if (move_uploaded_file($_FILES['att_file']['tmp_name'], $uploaded_file)) {
$conexion->setFolderByPath($base_folder); //Set base folder where files will be uploaded
/*Check if dir already exists and create it if it doesn't */
if($conexion->existsFolder($parent_name)){
echo "Parent".$parent_name." already exists";
}else{
$parent_name = str_replace('%20', ' ', $parent_name); //CHANGE IT BACK TO BLANK SPACES
$conexion->createFolder($parent_name);
}
/*Move uploaded file into folder */
$final_folder = $base_folder."/".$parent_name;
$final_folder = str_replace('%20', ' ', $final_folder); //ANOTHER HTTP REQUEST IS NEEDED
$conexion->setFolderByPath($finalFolder); //ERROR HERE IF $parent_name HAS BLANK SPACES
$conexion->uploadFile($uploaded_file);
}
echo 'File infor:';
print_r($_FILES);
?>
几天后编辑:
以防万一有人感兴趣,在程序员的 PHP 代码中不再需要这样做。我克隆了库,解决了这个问题,将其推送到本地分支并启动了拉取请求。现在特殊字符和空格都在库中处理。你可以简单地使用 $conexion->setFolderByPath($parent_name)
和 $parent_name
可以包含各种特殊字符和空格,程序员不需要照顾它。
我正在使用 API_Alfresco,这是一个通过 Apache Chemistry 连接到 Alfresco 的库,但是当我尝试创建包含空格的文件夹时出现以下错误:
Uncaught CmisRuntimeException in C:\xampp\htdocs\API_Alfresco\cmis_repository_wrapper.php:176 Stack trace:
#0 C:\xampp\htdocs\API_Alfresco\cmis_repository_wrapper.php(207): CMISRepositoryWrapper->convertStatusCode(505, '')
#1 C:\xampp\htdocs\API_Alfresco\cmis_service.php(791): CMISRepositoryWrapper->doGet('...')
#2 C:\xampp\htdocs\API_Alfresco\APIAlfresco.php(98): CMISService->getObjectByPath('...', Array)
#3 C:\xampp\htdocs\phpCMISalfresco.php(33): APIAlfresco->setFolderByPath('...')
#4 {main} thrown in C:\xampp\htdocs\API_Alfresco
当我尝试创建一个没有空格的文件夹时,它工作正常。这是我的代码:
<?php
require 'APIAlfresco/APIAlfresco.php';
include 'connectAlfresco.php';
$parent_name = $_POST['parent']; //FAILS IF IT HAS BLANK SPACES
$uploaded_file = basename($_FILES['att_file']['name']); //Uploaded
$base_folder = "/path/to/site/folder/"
if (move_uploaded_file($_FILES['att_file']['tmp_name'], $uploaded_file)) {
$conexion->setFolderByPath($base_folder); //Set base folder where files will be uploaded
/*Check if dir already exists and create it if it doesn't */
if($conexion->existsFolder($parent_name)) echo "Parent".$parent_name." already exists";
else $conexion->createFolder($parent_name);
/*Move uploaded file into folder */
$conexion->setFolderByPath($base_folder."/".$parent_name); //ERROR HERE IF $parent_name HAS BLANK SPACES
$conexion->uploadFile($uploaded_file);
}
echo 'File infor:';
print_r($_FILES);
?>
我查看了错误跟踪并意识到 doRequest() 在 doGet() 中调用(在 cmis_repository_wrapper.php) 在 HTTP 请求中返回错误(仅当文件夹包含空格时失败)。由于我还没有对库进行编码,所以我似乎无法弄清楚可能出了什么问题。有什么线索吗?会不会是bug?
还进行了一些调试,发现 doRequest() 正在接受一个 URL,例如:
http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom/path?path=/Sites/mysite/documentLibrary/Folder WithBlankSpaces&filter=&includeAllowableActions=&includeACL=&includePolicyIds=&includeRelationships=&renditionFilter=
在这个 link 中,我注意到可能触发错误的两件事:
1) "Folder WithBlankSpaces" 文件夹将 URL 一分为二(很可能是实际问题)
2) url 最后一部分的变量未设置
我怀疑 1) 可能是真正的问题,因为如果我强制该文件夹没有空格(调用 str_replace(' ', '', $parent_folder)
)那么它将起作用很好。
我怎样才能让它与文件夹 和 空格一起使用?有什么办法可以修改 URL 使空格不会将其一分为二吗?当然,如果用户试图创建 "My Folder",他想看到的是 "My Folder" 目录而不是 "MyFolder" 目录。
提前致谢!
我自己想出来了。 这个想法是在发出 HTTP 请求之前将空格强制为“%20”,然后再次将“%20”替换为空格。
基本上 $conexion->setFolderByPath($parent_name)
发出一个 HTTP 请求(这就是当 $parent_name
包含空格时触发错误的地方)。
因此,在调用该方法之前,$parent_name
空格需要替换为“%20”,以便 HTTP 请求知道 url 中有一个 "blank space",表示为 % 20.
一旦 HTTP 请求完成(在调用 setFolderByPath() 之后),“%20”需要被替换回空格,然后调用 $conexion->createFolder($parent_name);
因此文件夹是用实际的空格创建的,并且不是“%20”。
编辑:我发布固定代码以使其清晰。
<?php
require 'APIAlfresco/APIAlfresco.php';
include 'connectAlfresco.php';
$parent_name = $_POST['parent'];
$parent_name = str_replace(' ', '%20', $parent_name); //PREPARE IT FOR THE HTTP REQUEST
$uploaded_file = basename($_FILES['att_file']['name']); //Uploaded file
$base_folder = "/path/to/site/folder/";
if (move_uploaded_file($_FILES['att_file']['tmp_name'], $uploaded_file)) {
$conexion->setFolderByPath($base_folder); //Set base folder where files will be uploaded
/*Check if dir already exists and create it if it doesn't */
if($conexion->existsFolder($parent_name)){
echo "Parent".$parent_name." already exists";
}else{
$parent_name = str_replace('%20', ' ', $parent_name); //CHANGE IT BACK TO BLANK SPACES
$conexion->createFolder($parent_name);
}
/*Move uploaded file into folder */
$final_folder = $base_folder."/".$parent_name;
$final_folder = str_replace('%20', ' ', $final_folder); //ANOTHER HTTP REQUEST IS NEEDED
$conexion->setFolderByPath($finalFolder); //ERROR HERE IF $parent_name HAS BLANK SPACES
$conexion->uploadFile($uploaded_file);
}
echo 'File infor:';
print_r($_FILES);
?>
几天后编辑:
以防万一有人感兴趣,在程序员的 PHP 代码中不再需要这样做。我克隆了库,解决了这个问题,将其推送到本地分支并启动了拉取请求。现在特殊字符和空格都在库中处理。你可以简单地使用 $conexion->setFolderByPath($parent_name)
和 $parent_name
可以包含各种特殊字符和空格,程序员不需要照顾它。