Upload from PHP

cancel
Showing results for 
Search instead for 
Did you mean: 
claude22
Member II

Upload from PHP

Hello,

I would like to upload file in Alfresco using the REST API /alfresco/service/api/upload but I have always an error 500 with this message (06250305 Unexpected error occurred during upload of new content)

This is my code : 
    $destinationNodeRef = $this->getNodeRef($destinationPath);

    $type = "image/jpeg";

    $cfile = new CURLFile($filePath, $type);

    $curl = new Curl();  
    $response = $curl->post(
        $this->_alfUrl . '/alfresco/service/api/upload?alf_ticket=' . $_SESSION['alf_token'], array(     
        'filedata' => $cfile,
        'contenttype' => "cm:content",  
        'fileName' => $fileName,
        'destination' => $destinationNodeRef,
        'nodeRef' => $destinationNodeRef
        )
    );

$destinationNodeRef is something like : workspace://SpacesStore/f8f2fcb5-b64f-47f7-9b1c-f95b85460a58 

If someone could help me, it would be marvelous Smiley Happy

Thanks

C.

3 Replies
gmasand09
Active Member

Re: Upload from PHP

‌ It would be better if you send me the whole code ,  thn i can go through that and will solve your problem 

Thanks ,

Gaurav Singh 

claude22
Member II

Re: Upload from PHP

Hello,

I cannot send you all the code Smiley Happy it's too big. But I can add this part. It's the function called to upload the file on my php server (it works, the file is uploaded in the directory UPLOAD_DIR).

function uploadContent($file, $nodeRef, $filenameAlf) {
    $alfresco = new Alfresco(ALFRESCO_URL, "http://localserver.com/");

    if ($alfresco->validSession() && $this->checkPermission(CUSTOMER)) {
        $filename = $_FILES['file']['name'];
        $type = $_FILES['file']['type'];

        $destination = UPLOAD_DIR . $filename;
        move_uploaded_file($_FILES['file']['tmp_name'], $destination);

        $fileToUpload = $destination; //.$_FILES['file']['tmp_name'];

        $res = $alfresco->uploadFile($fileToUpload, $filenameAlf, $nodeRef, $type );
         return new Response('', 200);
    }
    return new Response('Unauthorized', 401);
    }

and my function uploadFile in the class Alfresco (for the moment I only work with picture, so I initialize the type with "image/jpeg").

 public function uploadFile($filePath, $fileName, $destinationPath, $type) {
    if (empty($filePath)) {
        return false;
    }
    if (empty($fileName)) {
        return false;
    }
    if (empty($destinationPath)) {
        return false;
    }
    $destinationNodeRef = $this->getNodeRef($destinationPath);

    $type = "image/jpeg";

    $cfile = new CURLFile($filePath, $type);

    $curl = new Curl();
    
    $response = $curl->post(
        $this->_alfUrl . '/alfresco/service/api/upload?alf_ticket=' . $_SESSION['alf_token'], array(
        'filedata' => $cfile,
        'contenttype' => "cm:content",
        'type' => "cm:content",
        'fileName' => $fileName,
        'destination' => $destinationNodeRef,
        'nodeRef' => $destinationNodeRef
        )
    );

    return $response->headers['Status-Code'] == 200;
    }


Thank you for your help

C.

gmasand09
Active Member

Re: Upload from PHP

Have you done it using POSTMAN ?
I did it using postman and it's easy, if you want to know how i did it then i can help you with that.

Which method are you using for uploading ?