代码之家  ›  专栏  ›  技术社区  ›  Menazanie

JSON中位置0 tinymce处的意外标记

  •  0
  • Menazanie  · 技术社区  · 7 年前

    这个问题被问了很多次,所有的问题都是以不同的方式提出的,这就是为什么我怀疑我的问题在这些问题中得到了解决。

    我使用 使用文件上传器。如果我使用URL,它可以工作,因为它可以从互联网上取回它。当我想使用文件夹中的图像时,它会将其图像上传到TinyMCE中,盒子顶部写着 . 但这条消息一直在上面,当我发布内容时,除了图像以外的所有内容都会被发送。回想起来,我发现文件目录中没有任何内容,我想把所有上传的图像放在那里。此外,它还返回了一个错误。

    意外标记(<在位置0的JSON中: 抬头看了看错误,发现了一些有趣的事情。它显示了内容的路径。

    move_uploaded_file(this_is_the_path/this_is_the_image.jpg)
    

    在这个例子中看起来不错,但在开发工具中,路径显示为橙色,这是一个字符串,但文件是黑色的。问题出在这里吗? 我把它包在一根绳子里,明显的结果是:

    move_uploaded_file(this_is_the_path/'this_is_the_image.jpg')
    

    这显然是错误的。

    2个相关但目前不重要的次要问题。

    以数字开头的文件根本不会被上传,并将作为错误消息发送:500。

    我的PHP代码用于处理路径。

    <?php
        $accepted_origins = array("http://localhost:8080");
        $imageFolder = "uploaded_content_frontpage/";
        reset ($_FILES);
        $temp = current($_FILES);
        if (is_uploaded_file($temp['tmp_name'])){
            if (isset($_SERVER['HTTP_ORIGIN'])) {
                if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
                    header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
                } else {
                    header("HTTP/1.0 403 Origin Denied");
                    return;
                }
            }
    
    // Sanitize input
            if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
                header("HTTP/1.0 500 Invalid file name.");
                return;
            }
    
    // Verify extension
            if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
                header("HTTP/1.0 500 Invalid extension.");
                return;
            }
    
          // Accept upload if there was no origin, or if it is an accepted origin
            $filetowrite = 'uploaded_content_frontpage/'.$temp['name'];
            move_uploaded_file($temp['tmp_name'], $filetowrite);
    
            echo json_encode(array('location' => $filetowrite));
       } else {
            // Notify editor that the upload failed
            header("HTTP/1.0 500 Server Error");
       }
    ?>
    

    路径是错误的还是拼写错误还是其他原因?

    $(document).ready(function() {
    tinymce.init ({
            theme: 'modern',
            selector: '.add_body_structure',
            height: 1000,
            menubar: true,
            branding: false,
            toolbar: 'undo redo | styleselect bold italic forecolor backcolor fontselect fontsizeselect | link paste | alignleft aligncenter alignright alignjustify | outdent indent bullist numlist | removeformat | insert',
            plugins: ' contextmenu print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media mediaembed template table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern help autoresize noneditable', 
            contextmenu: 'paste link image inserttable cell row column deletetable',
            advlist_bullet_styles: 'square',
            advlist_number_styles: 'lower-alpha,lower-roman,upper-alpha,upper-roman',
            statusbar: false,
            browser_spellcheck: true,
            image_title: true,      
            automatic_uploads: true,
            media_live_embeds: true,
            contextmenu: true,
            relative_urls: false,
            remove_script_host: false,
            paste_data_images: true,
            encoding: 'xml',
            invalid_elements: 'script, input, textarea, textfield, col, colgroup, caption, dir, meta, object, select, option, source, title',
            fontsize_formats: '8pt 10pt 12pt 14pt 16pt 18pt 20pt 22pt 24pt 26pt 28pt 30pt 32pt 34pt 36pt 38pt 40pt',
            images_upload_url: '/wysiwyg/tinymce_main/wysiwyg_one_page_version2.1/views/home/code_for_images/image_uploader.php',
    
            file_picker_callback: function(cb, value, meta) {
                var input = document.createElement('input');
                input.setAttribute('type', 'file');
                input.setAttribute('accept', 'image/*, audio/*, video/*');
    
                input.onchange = function() {
                    var file = this.files[0];
    
                    var reader = new FileReader();
                    reader.onload = function () {
    
                        var id = 'blobid' + (new Date()).getTime();
                        var blobCache =  tinymce.activeEditor.editorUpload.blobCache;
                        var base64 = reader.result.split(',')[1];
                        var blobInfo = blobCache.create(id, file, base64);
                        blobCache.add(blobInfo);
                        // call the callback and populate the Title field with the file name
                        cb(blobInfo.blobUri(), { title: file.name });
                    };
                    reader.readAsDataURL(file);
                };
            input.click();
            }
    
    });
    });     
    

    警告: move\u uploaded\u file(uploaded\u content\u frontpage/blobid1510920245387.jpg):无法打开流:C:\wamp\www\wysiwyg\tinymce\u main\wysiwyg\u one\u page\u version2.1\views\home\code\u for\u images\image\u uploader中没有此类文件或目录。php在线 45

    第45行表示移动上传的文件($temp['tmp\u name',$filetowrite);

    正如你们所见,我无法打开流,因为我的目录中没有这样的文件。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Azima    7 年前

    注释掉

    echo $filetowrite;
    

    检查一下,因为文件路径在json之前被回显,这就是导致json解析错误的原因。 还有一个额外的“.”在你的

    $filetowrite = 'uploaded_content_frontpage/'.$temp['name'].(see here);