问题描述
TinyMCE版本:4.6.5
我的表单和tinyMCE编辑器正在'public_html / adm'方向工作,我正在尝试将图像上传到'public_html / images / upload'。
tinyMCE初始化
tinymce.init({
selector: '.mytextarea',plugins: 'advlist autolink link lists charmap print preview media textcolor hr table image code powerpaste',toolbar: 'undo redo | insert | styleselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent hr | link image table code ',powerpaste_word_import: "prompt",menubar: false,min_height: 300,entity_encoding : "raw",// enable title field in the Image dialog
image_title: true,// enable automatic uploads of images represented by blob or data URIs
automatic_uploads: true,// URL of our upload handler (for more details check: https://www.tinymce.com/docs/configure/file-image-upload/#images_upload_url)
images_upload_url: 'postAcceptor.PHP',// here we add custom filepicker only to Image dialog
file_picker_types: 'image',.
.
.
postAcceptor.PHP
<?PHP
$imageFolder = "../images/upload/";
reset ($_FILES);
$temp = current($_FILES);
if (is_uploaded_file($temp['tmp_name'])){
if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/",$temp['name'])) {
header("HTTP/1.0 500 Invalid file name.");
return;
}
if (!in_array(strtolower(pathinfo($temp['name'],PATHINFO_EXTENSION)),array("gif","jpg","png"))) {
header("HTTP/1.0 500 Invalid extension.");
return;
}
$filename = md5(date('YmdHis')).'.jpg';
$file = $imageFolder.$filename;
move_uploaded_file($temp['tmp_name'],$file);
echo json_encode(array('location' => $file));
} else {
header("HTTP/1.0 500 Server Error");
}
?>
<p><img title="logo.png" src="../images/upload/0a13617f5d569df6c616578df855c92a.jpg" alt="" width="335" height="292" /></p>
问题是我的图像无法正确渲染,除非我这样更改图像路径。
<img title="logo.png" src="../../images/upload/0a13617f5d569df6c616578df855c92a.jpg" alt="" width="335" height="292" />
解决方法
您可以使用images_upload_base_path
配置选项来添加从已配置的images_upload_url
返回的URL:
https://www.tiny.cloud/docs-4x/configure/file-image-upload/#images_upload_base_path