PHP Code to upload a file to Alfresco document Library
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2013 10:00 AM
Hello all,
I am trying to write some php code to upload a file to my alfresco document library.
Below is the code I am trying to use but alas it is not uploading anything. I am new to PHP and so have no concept as to what is wrong.
The code below has been built from the tutorials and various other internet queries.
I am getting quite lost would appreciate someone help me find my way ?
Thanks in Advance
Conor M Hamill
I am trying to write some php code to upload a file to my alfresco document library.
Below is the code I am trying to use but alas it is not uploading anything. I am new to PHP and so have no concept as to what is wrong.
The code below has been built from the tutorials and various other internet queries.
I am getting quite lost would appreciate someone help me find my way ?
Thanks in Advance
Conor M Hamill
<?php require_once "Alfresco/Service/Repository.php"; require_once "Alfresco/Service/Session.php"; require_once "Alfresco/Service/SpacesStore.php"; $repositoryUrl = "http://alfrescodev.xxxxxxx.xx.xx:8080/alfresco/api"; $userName = "admin"; $password = "xxxxxxxxx"; $parantID="2a113503-7ed5-4d8f-ad3f-9f96e20f054b"; $repository = new Repository($repositoryUrl); $ticket = $repository->authenticate($userName, $password); $mysession = $repository->createSession($ticket); $store=new SpacesStore($mysession); $node=$mysession->getNode($store,$parantID); $upload=$node->createChild('cm_content','cm_contains',"$fname"); //$contentData=new ContentData($upload,"{http://www.alfresco.org/model/content/1.0}content"); $contentData=new ContentData($upload,'{http://http://www.alfresco.org/model/content/1.0}content'); ?> <html><head> <title>Upload</title></head> <body> <?php $fname = $_FILES['uploadFile']['name']; if (isset($_POST["Submit"])) { $fname=$_FILES['uploadFile']['name']; $ftmp=$_FILES['uploadFile']['tmp_name']; $ftype=$_FILES['uploadFile']['type']; $fsize=$_FILES['uploadFile']['size']; $contentData->encoding = 'UTF-8'; $contentData->writeContentFromFile($ftmp); $contentData->size = $fsize; $contentData->mimetype = $ftype; $upload->cm_name = $fname; $upload->cm_description = 'File Description'; $upload->cm_content = $contentData; echo "ContentData = ".$contentData."<br>"; $mysession->save(); echo "done"; } else { ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="uploadFile"> <input type="submit" value="Upload" name="Submit"> </form> <?php } ?> </body> </html>
Labels:
- Labels:
-
Archive
3 REPLIES 3

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2013 10:37 AM
Hi,
I'm new to Alfresco. But there is a nice "BasicTutorial5-Exercise1" in "/Tutorials" folder. It doing what you are looking for.
I'm new to Alfresco. But there is a nice "BasicTutorial5-Exercise1" in "/Tutorials" folder. It doing what you are looking for.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2013 05:23 AM
Thank you very much for your response but that was my starting point and I am unable to get it to work.
I have got a bit further now using this tutorial and a completed script that I found online.
I can now create the node with title & description and name but it is always 0 Bytes size. it seems my content is not being uploaded to the created node.
I have added the code I am using below if anyone can help would appreciate it greatly
<blockcode>
<?php
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Basic Tutorial Five - Exersice 1 (Solution)
*
* Change the creation of the object to take its content from a local file.
*
* See http://wiki.alfresco.com/wiki/PHP_Tutorial_Five#Exersice_One
*
* Note: any changes to this file should be uploaded to the wiki
*/
// Include the required Alfresco PHP API objects
if (isset($_SERVER["ALF_AVAILABLE"]) == false)
{
require_once "Alfresco/Service/Repository.php";
require_once "Alfresco/Service/Session.php";
require_once "Alfresco/Service/SpacesStore.php";
require_once "Alfresco/Service/ContentData.php";
}
// Specify the connection details
$repositoryUrl = "http://alfrescodev.xxxxxxx.co.uk:8080/alfresco/api";
$userName = "admin";
$password = "xxxxxxxxx";
// 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);
// 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/app:guest_home\"");
$guestHome = $nodes[0];
// Update the property if the form has been posted with the correct details
if (isset($_REQUEST["create"]) == true)
{
// Get the name of the new node
$name = $_REQUEST["name"];
echo($name."<br>");
// Create the new content node
// - parameter one is the type of node we want to create
// - parameter two is the association type, this should be contains for normal files and folders
// - parameter three is the association name, this should be the name of the file or folder under normal
// circumstances
$contentNode = $guestHome->createChild("cm_content", "cm_contains", "cm_".$name);
echo($contentNode."<br>");
// Add the titles aspect to the new node so that the title and description properties can be set
$contentNode->addAspect("cm_titled");
// Set the name, title and description property values
$contentNode->cm_name = $name;
$contentNode->cm_title = $_REQUEST["title"];
$contentNode->cm_description = $_REQUEST["description"];
// 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["file"]["type"], "UTF-8");
$contentData->size = $_FILES['file']['size'];
echo($contentData."<br>");
// Set the content to be the content file uploaded from the client
//$contentData->writeContentFromFile($_FILES["file"]["tmp_name"]);
$contentData->writeContentFromFile($_FILES["userfile"]["name"]);
echo($contentData."<br>");
echo($_FILES['userfile']['size']."<br>");
// Save the new node
$session->save();
}
?>
<html>
<head>
<title>Basic Tutorial Five - Create Content Node</title>
</head>
<body>
<big>Basic Tutorial Five - Create Content Node</big><br><br>
<form action="tr-kb-upload-test.php" method="post" enctype="multipart/form-data">
<input type=hidden name=create value="true">
<table border=0 cellpadding=2 cellspacing=3>
<tr>
<td>Name:</td>
<td><input name="name" type=edit size=50></td>
</tr>
<tr>
<td>Title:</td>
<td><input name="title" type=edit size=50></td>
</tr>
<tr>
<td>Description:</td>
<td><input name="description" type=edit size=50></td>
</tr>
<tr>
<td>File:</td>
<td><input name="file" size="50" type="file"></td>
</tr>
<tr>
<td></td>
<td align=right><input type=submit value="Create Node"></td>
</tr>
</table>
</form>
<?php
if (isset($_REQUEST["create"]) == true)
{
// Calculate the url to the properties of the new node
$uiUrl = "http://'.$repository->host.":".$repository->port."/alfresco/n/showDocDetails/workspace/SpacesStore/".$contentNode->id;
?>
<p style="color:red">The new content node has been created. Go to <a target="new" href="<?php echo $uiUrl ?>">here</a> to view its properties.</p>
<?php
}
?>
</body>
</html>
</blockcode>
I have got a bit further now using this tutorial and a completed script that I found online.
I can now create the node with title & description and name but it is always 0 Bytes size. it seems my content is not being uploaded to the created node.
I have added the code I am using below if anyone can help would appreciate it greatly
<blockcode>
<?php
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Basic Tutorial Five - Exersice 1 (Solution)
*
* Change the creation of the object to take its content from a local file.
*
* See http://wiki.alfresco.com/wiki/PHP_Tutorial_Five#Exersice_One
*
* Note: any changes to this file should be uploaded to the wiki
*/
// Include the required Alfresco PHP API objects
if (isset($_SERVER["ALF_AVAILABLE"]) == false)
{
require_once "Alfresco/Service/Repository.php";
require_once "Alfresco/Service/Session.php";
require_once "Alfresco/Service/SpacesStore.php";
require_once "Alfresco/Service/ContentData.php";
}
// Specify the connection details
$repositoryUrl = "http://alfrescodev.xxxxxxx.co.uk:8080/alfresco/api";
$userName = "admin";
$password = "xxxxxxxxx";
// 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);
// 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/app:guest_home\"");
$guestHome = $nodes[0];
// Update the property if the form has been posted with the correct details
if (isset($_REQUEST["create"]) == true)
{
// Get the name of the new node
$name = $_REQUEST["name"];
echo($name."<br>");
// Create the new content node
// - parameter one is the type of node we want to create
// - parameter two is the association type, this should be contains for normal files and folders
// - parameter three is the association name, this should be the name of the file or folder under normal
// circumstances
$contentNode = $guestHome->createChild("cm_content", "cm_contains", "cm_".$name);
echo($contentNode."<br>");
// Add the titles aspect to the new node so that the title and description properties can be set
$contentNode->addAspect("cm_titled");
// Set the name, title and description property values
$contentNode->cm_name = $name;
$contentNode->cm_title = $_REQUEST["title"];
$contentNode->cm_description = $_REQUEST["description"];
// 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["file"]["type"], "UTF-8");
$contentData->size = $_FILES['file']['size'];
echo($contentData."<br>");
// Set the content to be the content file uploaded from the client
//$contentData->writeContentFromFile($_FILES["file"]["tmp_name"]);
$contentData->writeContentFromFile($_FILES["userfile"]["name"]);
echo($contentData."<br>");
echo($_FILES['userfile']['size']."<br>");
// Save the new node
$session->save();
}
?>
<html>
<head>
<title>Basic Tutorial Five - Create Content Node</title>
</head>
<body>
<big>Basic Tutorial Five - Create Content Node</big><br><br>
<form action="tr-kb-upload-test.php" method="post" enctype="multipart/form-data">
<input type=hidden name=create value="true">
<table border=0 cellpadding=2 cellspacing=3>
<tr>
<td>Name:</td>
<td><input name="name" type=edit size=50></td>
</tr>
<tr>
<td>Title:</td>
<td><input name="title" type=edit size=50></td>
</tr>
<tr>
<td>Description:</td>
<td><input name="description" type=edit size=50></td>
</tr>
<tr>
<td>File:</td>
<td><input name="file" size="50" type="file"></td>
</tr>
<tr>
<td></td>
<td align=right><input type=submit value="Create Node"></td>
</tr>
</table>
</form>
<?php
if (isset($_REQUEST["create"]) == true)
{
// Calculate the url to the properties of the new node
$uiUrl = "http://'.$repository->host.":".$repository->port."/alfresco/n/showDocDetails/workspace/SpacesStore/".$contentNode->id;
?>
<p style="color:red">The new content node has been created. Go to <a target="new" href="<?php echo $uiUrl ?>">here</a> to view its properties.</p>
<?php
}
?>
</body>
</html>
</blockcode>
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2013 04:19 AM
Hi, I used the example five of php sdk but now I would like upload a file with custom content fe
fferta. If I Change the row
<cite>$contentNode = $guestHome->createChild("cm_content", "cm_contains", "cm_".$name);</cite>
and the row
<cite> $contentData = $contentNode->updateContent("cm_content", $_FILES["file"]["type"], "UTF-8");</cite>
in
<cite>$contentNode = $guestHome->createChild("fe_offerta", "cm_contains", "fe_".$name);</cite>
and
<cite> $contentData = $contentNode->updateContent("fe_offerta", $_FILES["file"]["type"], "UTF-8");</cite>
php generate the follow error:
<cite> Fatal error: Uncaught SoapFault exception: [soapenv
erver.generalException] in /var/www/Alfresco/Service/WebService/AlfrescoWebService.php:59 Stack trace: #0 /var/www/Alfresco/Service/WebService/AlfrescoWebService.php(59): SoapClient->__soapCall('update', Array, Array, Array, Array) #1 /var/www/Alfresco/Service/WebService/AlfrescoWebService.php(41): AlfrescoWebService->__soapCall('update', Array) #2 /var/www/Alfresco/Service/Session.php(162): AlfrescoWebService->__call('update', Array) #3 /var/www/Alfresco/Service/Session.php(162): AlfrescoWebService->update(Array) #4 /var/www/index.php(96): Session->save() #5 {main} thrown in /var/www/Alfresco/Service/WebService/AlfrescoWebService.php on line 59 </cite>.
Is there a solution or PHP not support custom content? Thanks

<cite>$contentNode = $guestHome->createChild("cm_content", "cm_contains", "cm_".$name);</cite>
and the row
<cite> $contentData = $contentNode->updateContent("cm_content", $_FILES["file"]["type"], "UTF-8");</cite>
in
<cite>$contentNode = $guestHome->createChild("fe_offerta", "cm_contains", "fe_".$name);</cite>
and
<cite> $contentData = $contentNode->updateContent("fe_offerta", $_FILES["file"]["type"], "UTF-8");</cite>
php generate the follow error:
<cite> Fatal error: Uncaught SoapFault exception: [soapenv

Is there a solution or PHP not support custom content? Thanks
