cancel
Showing results for 
Search instead for 
Did you mean: 

Document upload with PHP

philippe07
Champ in-the-making
Champ in-the-making
Dear All,

At first thanks for taking time to read my message !

I've played around with ifresco PHP Library (great !!) and Alfresco 3.4. I arrive to upload document to companyhome. Now i would like to change to a specific SpaceStore BUT i don't arrive to find a way to do this (This one : workspace://SpacesStore/9c32b505-e69a-4e29-a857-874c27163ab6)

My code following :

function alfresco_upload($file)
{
        if (!file_exists($file))
        {
                echo $file;
                die('Pb de fichier');
        }

        $root_classes = '/home/XXX/LIBRARIES/';
        require_once $root_classes . "Alfresco/Service/WebService/AlfrescoWebService.php";
        require_once $root_classes . "Alfresco/Service/Repository.php";

        require_once $root_classes . "Alfresco/Service/Session.php";
        require_once $root_classes . "Alfresco/Service/SpacesStore.php";

        $repositoryUrl = "http://XXXX/alfresco/api";
        $userName = "admin";
        $password = "XXXX";



        $repository = new Repository($repositoryUrl);
        $ticket = $repository->authenticate($userName, $password);
        $session = $repository->createSession($ticket);

        $spacesStore = new SpacesStore($session);

        $companyHome = $spacesStore->companyHome;

        $fileName = basename($file);
        $finfo = finfo_open(FILEINFO_MIME,"/usr/share/misc/magic");
        $fileType = finfo_file($finfo,$file);
        finfo_close($finfo);

        $HomePath = "app_company_home";

        $contentNode = $companyHome->createChild("cm_content", "cm_contains", $HomePath);

        $contentNode->cm_name = $fileName;
        $contentNode->cm_title = "Title";
        $contentNode->cm_description = "Description";

        $contentData = $contentNode->setContent("cm_content", $fileType, "UTF-8");

        $contentData->writeContentFromFile($file);

        $session->save();

        echo $file . ' uploaded !' . "\r\n";
}

?>

Is anyone have some clue about doing this ?

Thanks for your reading,

Regards

Philippe
6 REPLIES 6

ddanninger
Champ in-the-making
Champ in-the-making
Well 😃

not many things todo:


….
….
$repository = new Repository($repositoryUrl);
$ticket = $repository->authenticate($userName, $password);
$session = $repository->createSession($ticket);

$spacesStore = new SpacesStore($session);

$companyHome = $spacesStore->companyHome;

// your node -> workspace://SpacesStore/9c32b505-e69a-4e29-a857-874c27163ab6
$nodeId = "9c32b505-e69a-4e29-a857-874c27163ab6";

$MainNode = $session->getNode($spacesStore, $nodeId);

….
….
….

$contentNode = $MainNode->createChild("cm_content", "cm_contains", "cm_".$fileName);
$contentNode->cm_name = $fileName;
$contentNode->cm_title = "Title";
$contentNode->cm_description = "Description";

$contentData = $contentNode->setContent("cm_content", $fileType, "UTF-8");

$contentData->writeContentFromFile($file);

$session->save();


So instead of using $companyHome you just search for a node with the session ($session->getNode) as you provide the $SpacesStore in this method theres no need to give the nodref (workspace://SpacesStore) just the nodeid (0000-0000-0000-0000) is enough.

And then just instead of adding a child to $companyHome we need to add it to our searched node which we saved to $MainNode

Hope it works fine

best regards

philippe07
Champ in-the-making
Champ in-the-making
Hi Dominik,

I've just fine this in the same time as you and it's rocks !!! Smiley Happy

Thanks a lot

Philippe

philippe07
Champ in-the-making
Champ in-the-making
An other thing regarding this script. It seems that when the file is already uploaded to Alfresco another time, the next uploading is simply blocked. Is it possible to force upload to replace the old one ? (without or with versioning)

Regards

Philippe

philippe07
Champ in-the-making
Champ in-the-making
just a small up! because i'm really blocked on this … Perhaps the solution is to check is somethinkg is already existing … but i don't find !!!!

Regards

Philippe

jomi
Champ in-the-making
Champ in-the-making
same here

Please help us Smiley Happy

philippe07
Champ in-the-making
Champ in-the-making
Hi !

For what do you need help ?

Regards

Philippe