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();

You may also like:

  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?

Leave a comment