cancel
Showing results for 
Search instead for 
Did you mean: 

Upload content using php api

testingit
Champ in-the-making
Champ in-the-making
Hi,

I couldn't find how can i upload file using the php API ?

can you please help ?

10x
12 REPLIES 12

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

I'm going to add some tutorials/examples to the Wiki.

I'll be sure to show how to upload content and will post here once its available.

Cheers,
Roy

testingit
Champ in-the-making
Champ in-the-making
can you post here a simple small example for uploading with the API ?

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

I've got a really simple example done now.  I just need to update the Wiki with some supporting documentation.

Should be available next week,

Cheers,
Roy

testingit
Champ in-the-making
Champ in-the-making
I managed to write something by myself 🙂

I'm posting it so it may be helpfull:


//Repository URL
$alfURL="http://xcm:80/alfresco/api";

//User
$alfUser="";
$alfPassword="";

//ID of the PARANT FOLDER
$parantID="f93f665c-508b-11dc-800c-d5564c95a581";



$fname=$_FILES['ALF']['name'];
$ftmp=$_FILES['ALF']['tmp_name'];
$ftype=$_FILES['ALF']['type'];
// Create the session
$repository = new Repository($alfURL);
$ticket = $repository->authenticate($alfUser, $alfPassword);
$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=$upload->cm_content;
$contentData->mimetype = $ftype;
$upload->cm_name="$fname";
$contentData->encoding = "UTF-8";
$contentData->writeContentFromFile("$ftmp");
$upload->cm_content=$contentData;
$mysession->save();
echo "done";

mchakravarty
Champ in-the-making
Champ in-the-making
I get an error as follows:
Fatal error: Uncaught exception 'Exception' with message 'A mime type for the content property {http://www.alfresco.org/model/content/1.0}content on node workspace://SpacesStore/sessionTICKET_6f1b6246bdcaee4e18b233349170ae8239e3bcac0 must be set' in /home/manish/alfresco-php-library/Alfresco/Service/ContentData.php:197 Stack trace: #0 /home/manish/alfresco-php-library/Alfresco/Service/Node.php(604): ContentData->onBeforeSave(Array, Array) #1 /home/manish/alfresco-php-library/Alfresco/Service/Session.php(156): Node->onBeforeSave(Array) #2 /home/manish/alfresco-php-library/Examples/fileUpload.php(35): Session->save() #3 {main} thrown in /home/manish/alfresco-php-library/Alfresco/Service/ContentData.php on line 197


=============

The actual script executed that i executed was ( verbatim)

<?php

require_once('Alfresco/Service/Repository.php');
require_once('Alfresco/Service/Session.php');
require_once('Alfresco/Service/SpacesStore.php');
//require_once('../config.php');
$alfURL="http://localhost:8080/alfresco/api";

//User
$alfUser="admin";
$alfPassword="admin";

//ID of the PARENT FOLDER
$parantID="67910128-d484-11dc-ad55-9fdf34d87024";



$fname=$_FILES['ALF']['name'];
$ftmp=$_FILES['ALF']['tmp_name'];
$ftype=$_FILES['ALF']['type'];
// Create the session
$repository = new Repository($alfURL);
$ticket = $repository->authenticate($alfUser, $alfPassword);
$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=$upload->cm_content;
$contentData->mimetype = $ftype;
$upload->cm_name="$fname";
$contentData->encoding = "UTF-8";
$contentData->writeContentFromFile("$ftmp");
$upload->cm_content=$contentData;
$mysession->save();
echo "done";
?>
==============


Could you please help me out here?

gfeguiguren
Champ in-the-making
Champ in-the-making
Hi  mchakravarty,

I'm new in alfresco, but I tried the code above and it works, the only one difference that I realize in the code is a missing include reference in the header:

require_once "Alfresco/Service/Node.php";

After that, all the example worked

I Hope it help you,

Gabriel

where we have to execute the code the php file 

can u please me in detail regarding this or mail me at gaurav.masand09@gmail.com

antonella
Champ in-the-making
Champ in-the-making
hi,
did you find some solutions at this problem? i use the code above but i have the same error!
i also add
require_once ('Alfresco/Service/Node.php');
could you help me please?

pokcoy
Champ in-the-making
Champ in-the-making
Dear aLL,

Here's mine.
Maybe will give another clue for the issue.

upload.html

<html>
   <head></head>
   <body>
      <form enctype="multipart/form-data" action="uploadContent.php" method="POST">
      <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
      Choose a file to upload: <input name="uploadedfile" type="file" /><br />
      <input type="submit" value="Upload File" />
      </form>
   </body>
</html>

uploadContent.php

<?php

   require_once 'Alfresco/Service/WebService/WebServiceFactory.php';
   require_once 'Alfresco/Service/Repository.php';
   require_once 'Alfresco/Service/SpacesStore.php';
   
   $repoURL = "http://localhost:8080/alfresco/api";
   $userName = "admin";
   $password = "admin";
   
   $repository = new Repository($repoURL);
   
   $ticket = $repository->authenticate($userName,$password);
   $session = $repository->createSession($ticket);
   
   $spacesStore = new SpacesStore($session);
   $companyHome = $spacestore->companyHome;
   
   $currentNode = null;

        $currentNode = $spacesStore->companyHome;
        $path = 'Company Home';
   
   $upload = $currentNode->createChild('cm_content', 'cm_contains', 'cm_'.$_FILES['uploadedfile']['name']);
   $pathFile = $_FILES['uploadedfile']['tmp_name'];
   
   $content_data = new ContentData($upload, $property, $mimetype, $encoding);
   $content_data->mimetype = $_FILES['uploadedfile']['type'];
   $content_data->encoding = "UTF-8";
   $content_data->writeContentFromFile($pathFile);
   
        $upload->cm_content = $content_data;

   $upload->cm_name = $_FILES['uploadedfile']['name'];

        echo $upload->cm_name;
        $session->save();
?>