TinyMCE 在图片上传时创建文件夹
TinyMCE create folders on images upload
我正在开发一个有多个用户的博客系统。我将 tinymce 实现为创建内容的默认编辑器。一切正常,但我需要修改tinymce的图片上传,我需要每个用户在保存创建的内容时,如果选择将图片添加到内容中,将它们上传到专用文件夹,如果不存在则创建,在里面php 上传脚本中配置的默认上传目录。我有以下代码,但它没有按预期工作,当我 post 内容时,它不会为图像创建新目录,但只会将内容保存到数据库中。
TinyMCE 配置:
<script>
$(document).ready(function(){
tinymce.init({
selector: '#post-content',
height: 380,
plugins: 'save print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern help',
toolbar: 'save | formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat | image',
images_upload_url: 'ImageHandler.php',
automatic_uploads: false,
save_onsavecallback: function(){
var action = 'savePost';
var dataTitle = $('#clientName').val();
var data = tinymce.get('post-content').getContent();
$.ajax({
type: 'POST',
url: 'PostHandler.php',
data: {action: action, client_name: dataTitle, post_content: data},
cache: false,
success: function(response){
alert(response);
}
});
}
});
});
</script>
ImageHandler.php(该脚本是在 tinymce 文档中 posted 的,我只评论了 CORS 部分,因为它会给我带来一些问题。)
<?php
$accepted_origins = array("http://localhost", "http://192.168.1.1", "http://example.com");
$imageFolder = "../../img/portfolio/";
reset ($_FILES);
$temp = current($_FILES);
if (is_uploaded_file($temp['tmp_name'])){
// if (isset($_SERVER['HTTP_ORIGIN'])) {
// // same-origin requests won't set an origin. If the origin is set, it must be valid.
// if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
// header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
// } else {
// header("HTTP/1.1 403 Origin Denied");
// return;
// }
// }
if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
header("HTTP/1.1 400 Invalid file name.");
return;
}
// Verify extension
if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
header("HTTP/1.1 400 Invalid extension.");
return;
}
if(!file_exists($_POST['client_name'])){
$clientFolder = mkdir($imageFolder.$_POST['client_name'], 0777);
}
// Accept upload if there was no origin, or if it is an accepted origin
//$filetowrite = $imageFolder . $temp['name'];
$filetowrite = $clientFolder . $temp['name'];
move_uploaded_file($temp['tmp_name'], $filetowrite);
// Respond to the successful upload with JSON.
// Use a location key to specify the path to the saved image resource.
// { location : '/your/uploaded/image/file'}
echo json_encode(array('location' => $filetowrite));
} else {
// Notify editor that the upload failed
header("HTTP/1.1 500 Server Error");
}
?>
PostHandler.php(该文件将管理用户内容的保存)
<?php
require_once dirname(__DIR__, 1).'/Config.php';
if(isset($_POST['action']) && $_POST['action'] === 'savePost'){
$clientName = $_POST['client_name'];
$postContent = $_POST['post_content'];
$stmt = $db->prepare("INSERT INTO portfolio (post_content, client_name) VALUES (?, ?)");
if($stmt->execute(array($postContent, $clientName))){
echo 'ok';
} else {
echo $db->errorCode();
}
}
?>
file_exists($_POST['client_name'])
你应该提供完整的路径。
我正在开发一个有多个用户的博客系统。我将 tinymce 实现为创建内容的默认编辑器。一切正常,但我需要修改tinymce的图片上传,我需要每个用户在保存创建的内容时,如果选择将图片添加到内容中,将它们上传到专用文件夹,如果不存在则创建,在里面php 上传脚本中配置的默认上传目录。我有以下代码,但它没有按预期工作,当我 post 内容时,它不会为图像创建新目录,但只会将内容保存到数据库中。 TinyMCE 配置:
<script>
$(document).ready(function(){
tinymce.init({
selector: '#post-content',
height: 380,
plugins: 'save print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern help',
toolbar: 'save | formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat | image',
images_upload_url: 'ImageHandler.php',
automatic_uploads: false,
save_onsavecallback: function(){
var action = 'savePost';
var dataTitle = $('#clientName').val();
var data = tinymce.get('post-content').getContent();
$.ajax({
type: 'POST',
url: 'PostHandler.php',
data: {action: action, client_name: dataTitle, post_content: data},
cache: false,
success: function(response){
alert(response);
}
});
}
});
});
</script>
ImageHandler.php(该脚本是在 tinymce 文档中 posted 的,我只评论了 CORS 部分,因为它会给我带来一些问题。)
<?php
$accepted_origins = array("http://localhost", "http://192.168.1.1", "http://example.com");
$imageFolder = "../../img/portfolio/";
reset ($_FILES);
$temp = current($_FILES);
if (is_uploaded_file($temp['tmp_name'])){
// if (isset($_SERVER['HTTP_ORIGIN'])) {
// // same-origin requests won't set an origin. If the origin is set, it must be valid.
// if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
// header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
// } else {
// header("HTTP/1.1 403 Origin Denied");
// return;
// }
// }
if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
header("HTTP/1.1 400 Invalid file name.");
return;
}
// Verify extension
if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
header("HTTP/1.1 400 Invalid extension.");
return;
}
if(!file_exists($_POST['client_name'])){
$clientFolder = mkdir($imageFolder.$_POST['client_name'], 0777);
}
// Accept upload if there was no origin, or if it is an accepted origin
//$filetowrite = $imageFolder . $temp['name'];
$filetowrite = $clientFolder . $temp['name'];
move_uploaded_file($temp['tmp_name'], $filetowrite);
// Respond to the successful upload with JSON.
// Use a location key to specify the path to the saved image resource.
// { location : '/your/uploaded/image/file'}
echo json_encode(array('location' => $filetowrite));
} else {
// Notify editor that the upload failed
header("HTTP/1.1 500 Server Error");
}
?>
PostHandler.php(该文件将管理用户内容的保存)
<?php
require_once dirname(__DIR__, 1).'/Config.php';
if(isset($_POST['action']) && $_POST['action'] === 'savePost'){
$clientName = $_POST['client_name'];
$postContent = $_POST['post_content'];
$stmt = $db->prepare("INSERT INTO portfolio (post_content, client_name) VALUES (?, ?)");
if($stmt->execute(array($postContent, $clientName))){
echo 'ok';
} else {
echo $db->errorCode();
}
}
?>
file_exists($_POST['client_name'])
你应该提供完整的路径。