How do I select the local image of the computer and upload it to my server which should return the URL of the image in TinyMCE and thus generate the <img>
tag with the src
containing the URL of the image on the server?
I was reading the File & Image Upload And I did not quite understand. I tried to implement. I saw that there should be a script to handle the upload and it should return the JSON with the image address, but I did not understand how it will work on the client side, better how to apply it in TimyMCE.
I already have the basic back-end script ready to receive the images, but I can not get TMCE to send them. I'm almost giving up and creating a separate gallery of images for this.
<?php
class Upload {
private $file;
private $nameInput;
private $mimeImg;
private $dir;
private $dirUpload;
private $newName;
function __construct($file, $name) {
$this->file = $file;
$this->nameInput = $name;
$this->dir = $_SERVER['DOCUMENT_ROOT'] . '/mysite/img/postagens/';
$this->dirUpload = $this->dir . date('YmdHisS-') . basename($this->file[$name]['name']);
$this->newName = '/mysite/img/postagens/' . date('YmdHisS-') . basename($this->file[$name]['name']);
$this->mimeImg = ['image/png', 'image/jpeg', 'image/gif', 'image/ico', 'image/vnd.microsoft.icon'];
}
public function getName() {
return $this->file[$this->nameInput]['name'];
}
public function getNewName() {
return $this->newName;
}
public function getDirUpload() {
return $this->dirUpload;
}
public function getType() {
return $this->file[$this->nameInput]['type'];
}
public function getSize() {
return $this->file[$this->nameInput]['size'];
}
public function getError() {
return $this->file[$this->nameInput]['error'];
}
public function setName($name) {
$this->dirUpload = $this->dir . '/' . $name;
}
public function setTmpName() {
$this->file[$this->nameInput]['tmp_name'];
}
public function setSalve() {
return move_uploaded_file($this->file[$this->nameInput]['tmp_name'], $this->dirUpload);
}
function getFile() {
return $this->file[$this->nameInput]['tmp_name'];
}
}
$upload = new Upload($_FILES, 'img');
$upload->setSalve();
header('Content-Type: application/json');
json_encode(['location',$upload->getNewName()]);