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();
Category >> PHP >> Web Services  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. cui on 2010-2-2

    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?

  2. admin on 2010-2-2

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

  3. nachi on 2010-5-10

    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?

  4. nachi on 2010-5-10

    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?

  5. Fabrizio Vettore on 2010-11-15
  6. admin on 2010-11-19

    Thanks for your reply.

  7. Anupama on 2011-1-20

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

    Thank You.

  8. Ken on 2011-10-20

    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!

  9. Admin on 2011-10-25

    I don’t remember that. It might work if you change PATH in the code above?

  10. Vanessa on 2012-12-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

  11. Fernando on 2013-11-20

    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?.

  12. Sahejin mansuri on 2013-12-24

    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.

  13. Anuradha on 2015-1-21

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

Leave a comment

*