# Adjuntar imagen (1) - [uploadFileErp]

### 🧾 Descripción

Este servicio permite **subir un archivo al ERP** utilizando el endpoint interno `method/upload_file`.

Funciona enviando el archivo recibido desde el request PHP (`$_FILES["file"]`) mediante **cURL** hacia el servicio de carga del ERP y devuelve los metadatos del archivo subido:

- URL pública
- Fecha de creación
- Nombre del archivo
- Tamaño del archivo

Es un servicio auxiliar utilizado por funcionalidades que requieren cargar documentos o imágenes hacia el servidor del ERP.

---

# 🚀 Endpoint

### POST `/upload-file-erp`

📌 **Requiere enviar un archivo en el campo `file` del formulario.**

---

# 🔐 Seguridad

No utiliza el token del usuario.  
El servicio realiza la subida como **Guest**, usando los headers de cookie:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-cookie%3A-full_name%3Dgu"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-section">Cookie: full_name=Guest; sid=Guest; system_user=no; user_id=Guest; user_image=</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>➡️ Esto significa que la API de ERP ya está configurada para permitir carga de archivos desde clientes externos bajo permisos Guest.

---

# 🧠 Flujo del Servicio (resumen)

1. Captura el archivo enviado en `$_FILES["file"]`.
2. Crea un objeto `CURLFile` con:
    
    
    - ruta temporal (`tmp_name`)
    - tipo MIME
    - nombre original
3. Envía el archivo al endpoint:
    
    <div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
    </div></div></div><div class="overflow-y-auto p-4" dir="ltr">`POST /method/upload_file`</div></div>
4. Espera la respuesta del ERP.
5. Decodifica la respuesta JSON.
6. Retorna un objeto con:
    
    
    - url del archivo
    - fecha de creación
    - nombre
    - tamaño

---

# 📥 Request Body

Debe enviarse como `multipart/form-data`.

### Ejemplo (form-data):

<div class="_tableContainer_1rjym_1" id="bkmrk-campo-tipo-descripci"><div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1"><table class="w-fit min-w-(--thread-content-width)" data-end="1779" data-start="1683"><thead data-end="1713" data-start="1683"><tr data-end="1713" data-start="1683"><th data-col-size="sm" data-end="1691" data-start="1683">Campo</th><th data-col-size="sm" data-end="1698" data-start="1691">Tipo</th><th data-col-size="sm" data-end="1713" data-start="1698">Descripción</th></tr></thead><tbody data-end="1779" data-start="1746"><tr data-end="1779" data-start="1746"><td data-col-size="sm" data-end="1753" data-start="1746">file</td><td data-col-size="sm" data-end="1760" data-start="1753">File</td><td data-col-size="sm" data-end="1779" data-start="1760">Archivo a subir</td></tr></tbody></table>

</div></div>Ejemplo en HTML:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%3Cform-method%3D%22post%22-"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-tag"><<span class="hljs-name">form</span></span> <span class="hljs-attr">method</span>=<span class="hljs-string">"POST"</span> <span class="hljs-attr">enctype</span>=<span class="hljs-string">"multipart/form-data"</span>>   <span class="hljs-tag"><<span class="hljs-name">input</span></span> <span class="hljs-attr">type</span>=<span class="hljs-string">"file"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"file"</span>><span class="hljs-tag"></<span class="hljs-name">form</span></span>>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div>---

# 📤 Response 200 – Ejemplo

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22url%22%3A-%22%2Ffiles%2Fdoc"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/files/documento.pdf"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"creation"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"2025-01-14 09:43:55.12893"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"file_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"documento.pdf"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"file_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">120394</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>---

# ❗ Posibles Errores

### 1. Error al procesar el archivo

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22msn%22%3A-%22failed-to-"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Failed to open stream..."</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### 2. Error en cURL

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22msn%22%3A-%22curl-error"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"cURL Error #:Timeout..."</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### 3. El ERP no devuelve estructura válida

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22msn%22%3A-%22error-al-p"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error al procesar respuesta del ERP"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>---

# 📚 Estructura de respuesta del ERP

El ERP retorna algo como:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22message%22%3A-%7B-%22file"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>    <span class="hljs-attr">"file_url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/files/image.png"</span><span class="hljs-punctuation">,</span>    <span class="hljs-attr">"creation"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"2025-01-15 10:45:00.12584"</span><span class="hljs-punctuation">,</span>    <span class="hljs-attr">"file_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"image.png"</span><span class="hljs-punctuation">,</span>    <span class="hljs-attr">"file_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">48204</span>  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>---

# 🗃 Lógica en pseudo-código

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-try%3A-file-%3D-new-curl"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`try:    file = new CURLFile(tmp, type, name)catch error:    return errorsend file to ERP via cURL → POST /method/upload_fileif error in curl:    return errorparse responsereturn {    url: response.message.file_url,    creation: response.message.creation,    file_name: response.message.file_name,    file_size: response.message.file_size}`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>