Using Alfresco PHP Web Service to upload files

The following is the PHP code for uploading files using Alfresco Web Service. I put some comments inside the code. If there is anything confuses you, please leave your comment and I hope I can help.

 if (isset($_SERVER["ALF_AVAILABLE"]) == false)
 {
 	require_once "./alfresco_sdk/remote/Alfresco/Service/Repository.php";
 	require_once "./alfresco_sdk/remote/Alfresco/Service/Session.php";
 	require_once "./alfresco_sdk/remote/Alfresco/Service/SpacesStore.php";
 	require_once "./alfresco_sdk/remote/Alfresco/Service/ContentData.php";
 }
 $repositoryUrl = "http://localhost:8080/alfresco/api";
 $userName = "admin";
 $password = "admin";
 
 // Authenticate the user and create a session
 $repository = new Repository($repositoryUrl);
 $ticket = $repository->authenticate($userName, $password);
 $session = $repository->createSession($ticket);
 
 // Create a reference to the 'SpacesStore'
 $spacesStore = new SpacesStore($session);
 
 $dir_name =  "s".$irbID;
 
 // Use a serach to get the guest home space we will use to place the new content in
 $nodes = $session->query($spacesStore, "PATH:\"app:company_home/cm:testing/cm:$dir_name\"");
 
 if(!empty($nodes)){
 	echo "good, no need to do anything.";
 }else{
 	$nodes = $session->query($spacesStore, "PATH:\"app:company_home/cm:testing\"");
 	$contentNode = $nodes[0];
 	$newSpace = $contentNode->createChild('cm_folder', 'cm_contains', 'cm_'.$dir_name);//here has to be cm_ as it is here.
 	$newSpace->cm_name = $dir_name;
 	$newSpace->cm_title = "This is a title";
 	$newSpace->cm_description = 'Description of Space';
 	$session->save();
 	unset($session);
 	$session = $repository->createSession($ticket);
 	$nodes = $session->query($spacesStore, "PATH:\"app:company_home/cm:testing/cm:$dir_name\"");
 }
 
 $guestHome = $nodes[0];
 
 // Get the name of the new node
 $name = $_POST['txt_docs_title'];
 
 $contentNode = $guestHome->createChild("cm_content", "cm_contains", "cm_".$name);//!!the folder's name has to begin with 'cm_' otherwise, using web service can not find it later.
 $contentNode->addAspect("cm_titled");
 
 // Set the name, title and description property values
 $contentNode->cm_name = $name;
 $contentNode->cm_title = $_POST['num_ct_ID'];
 $contentNode->cm_description = "testing description --program creek";
 
 // Set the content onto the standard content property for nodes of type cm:content.
 // We are going to assume the mimetype and encoding for ease
 $contentData = $contentNode->updateContent("cm_content", $_FILES['document']['type'], "UTF-8");
 
 // Set the content to be the content file uploaded from the client
 $contentData->writeContentFromFile($_FILES["document"]["tmp_name"]);
 
 // Save the new node
 $session->save();

13 thoughts on “Using Alfresco PHP Web Service to upload files”

  1. I couldn’t find these files in my alfresco installation folder.Repository.php, Session.php , SpacesStore.php ,ContentData.php

  2. Hi, I am new in alfresco.

    I am looking for PHP script which create content in alfresco. I have used above code. But I dont know which file i have to upload.

    Please help me. Thank you in advance.

  3. Hey I was looking into the code and I have a question how could I set the parameters to a custom model into the share?.

  4. Hi, i’m using your example to upload a file in to a file but i get an error when the directory doesn’t exists. Can you Help me?

    Thanks

  5. Hi,
    how do you specify the space to where the file will be uploaded?
    Example:
    Company Home
    – Articles
    – Music

    I created a space in the company home and named it articles and music. How do I specify that all uploaded files will go to Article space?

    Thanks!

  6. I want to know how can we fetch the files stored in alfresco server using alfresco php web service.

    Thank You.

  7. require_once “Alfresco/Service/Repository.php”;
    require_once “Alfresco/Service/Session.php”;
    require_once “Alfresco/Service/SpacesStore.php”;
    require_once “Alfresco/Service/Node.php”;

    Where can i downlaod the above files?

  8. require_once “Alfresco/Service/Repository.php”;
    require_once “Alfresco/Service/Session.php”;
    require_once “Alfresco/Service/SpacesStore.php”;
    require_once “Alfresco/Service/Node.php”;

    where can i download above files?

  9. I couldn’t remember now. But I remember there is a php code package which can be download through their official website.

  10. require_once “./alfresco_sdk/remote/Alfresco/Service/Repository.php”;

    i want to ask you about the above code. where stores the Repository.php,Session.php…, and where can i them download?

Leave a Comment