4

更准确地说,我对 textareas 的 nicEditor 有疑问;图片上传按钮。

我的文件 index.php 包含调用 nicEditor 的位置。在同一个文件夹中还有另外两个文件夹:“images”,我想在其中存储文件和“includes”,其中 nicEdit.js 和 nicUpload.php(其中包含从官方网站提供的上传代码)。

我的问题是:当我想通过nicEdit上传图片时,错误信息“上传图片失败”。出现,虽然我已经设置了以下参数:

  • 在 nicEdit.js 中,nicURI 设置为“includes/nicUpload.php”
  • 在 nicUpload.php 中,NICUPLOAD_PATH 被定义为“./images”,而 NICUPLOAD_URI 被定义为“images”(我在这里尝试了其他几种组合,但似乎都不起作用)
  • 文件夹“images”的权限为 777

我只是在尝试和错误上浪费了几个小时,但这样做却没有得到任何积极的结果......

[编辑]:

当我上传更大的文件时,我可以看到上传栏正在处理,但是一旦完成,就会出现“上传图片失败”

nicEdit.js 中的代码包括:

var nicUploadButton=nicEditorAdvancedButton.extend({nicURI:'includes/nicUpload.php',errorText:"Failed to upload image",addPane:function ......
4

6 回答 6

6

解决方案简单!!!

1 - 创建文件 image.php 并粘贴代码:

<?php
//Check if we are getting the image
if(isset($_FILES['image'])){
        //Get the image array of details
        $img = $_FILES['image'];       
        //The new path of the uploaded image, rand is just used for the sake of it
        $path = "upload/" . rand().$img["name"];
        //Move the file to our new path
        move_uploaded_file($img['tmp_name'],$path);
        //Get image info, reuiqred to biuld the JSON object
        $data = getimagesize($path);
        //The direct link to the uploaded image, this might varyu depending on your script location    
        $link = "http://$_SERVER[HTTP_HOST]"."/nicedit/".$path;
        //Here we are constructing the JSON Object
        $res = array("upload" => array(
                                "links" => array("original" => $link),
                                "image" => array("width" => $data[0],
                                                 "height" => $data[1]
                                                )                              
                    ));
        //echo out the response :)
        echo json_encode($res);
}
?>

2 - 打开并编辑 nickedit.js:

Find the line starting like nicURI:"http://api.imgur.com/2/upload.json" 
Replace it with

nicURI:"image.php"


DONE ! Now try uploading something and it would go directly to your server :)

字体: http: //manzzup.blogspot.com.br/2014/03/customize-nicedit-image-upload-to.html

于 2014-04-02T12:16:30.407 回答
3

nicEdit 开发人员做了一些坏事

他们更新了nicUpload.jsimgur.com不兼容的版本nicUpload.php。当您从他们的页面下载 lib 时,服务器只需将imgur版本包含到nicEdit.js.

我想出了如何使这项工作。

在版本 23 上签出他们的 svn 存储库

  svn checkout http://svn.nicedit.com//trunk/nicUpload/
  svn update -r 23

编辑未压缩版本nicEdit.js删除之间的代码

/* START CONFIG */
var nicUploadOptions = {
    buttons : {
        'upload' : {name : 'Upload Image', type : 'nicUploadButton'}
    }

nicEditors.registerPlugin(nicPlugin,nicUploadOptions);

粘贴你从 svn 获得的nicUpload.jsnicUpload/

并删除这一行:

/* NICEDIT_REMOVE_START */,iconFiles : {'upload' : 'src/nicUpload/icons/upload.gif'}/* NICEDIT_REMOVE_END */
于 2012-11-15T10:38:29.003 回答
0

如果您从 htaccess 保护区上传,则必须删除

C.setRequestHeader("Authorization","Client-ID c37fc05199a05b7");

...在 varnicUploadButton中以避免弹出提示,而不是获取您的登录值。似乎现在可以工作了。

于 2016-07-03T19:52:29.637 回答
0

没有一个答案对我有用,所以我制作了一个 PHP 上传器文件,该文件检索几乎与 imgur 相同的对象。

这是文件:

上传者.php

<?php
require_once 'controllers/utils.php';

$target_image_dir = "views/images/inner_img/";
$target_file = ""; 

$response =  array('data' => [], 'success' => false, "status" =>499 );
$fotoLink = json_encode($_FILES);

if(!empty($_FILES["image"]['tmp_name'])){
        $imagen = $_FILES["image"];                     
        list($width, $height) = getimagesize($imagen['tmp_name']);
        $imageFileType = pathinfo($imagen["name"],PATHINFO_EXTENSION);
        $fileNameCoded = uniqid('', false).'.'. strtolower($imageFileType);
        $target_file = $target_image_dir.$fileNameCoded;
        $fotoLink = $target_file;        


       if (move_uploaded_file($imagen["tmp_name"], $target_file)){
            strtolower($imageFileType));    
            $littleImage = '/views/images/inner_img/'.$fileNameCoded;
            $fotoLink = SITE_URL.$littleImage;

            $data = array( "type"=> "image/".strtolower($imageFileType),                                    
                            "width"=> $width ,
                            "height"=> $height,                            
                            "name"=> "",                      
                            "link"=> $fotoLink);
            $response =  array('data' => $data, 'success' => true, "status" => 200 );
        }
}

$response =  array('data' => $data, 'success' => true, "status" => 200);

echo json_encode($response);

utils.php 检查图像属性:

实用程序.php

define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);
define('semana',  array('','Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado','Domingo'));

function checkImageAttrs($image, $targeFile)
{
    if(getimagesize($image["tmp_name"]) === false)
    {      
        return "El archivo de el archivo no es una imagen.";
    }

    if ($image["size"] > 10*MB) 
    {
       return "Ups, el archivo es muy grande.";        
    }

    $imageFileType = pathinfo($image["name"],PATHINFO_EXTENSION);

    if($imageFileType!="jpg" && $imageFileType!="png" && $imageFileType!="jpeg"  && $imageFileType!="gif" )
    {
        return "Ups, sólo están permitidos los archivos de tipo JPG, JPEG, PNG y GIF.";
    }

   return "OK";
}

function randomPassword($characters_count) {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < $characters_count; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

警告: 如果您将其用作开放式上传器,我确信这不够安全,但我必须为受限页面执行此操作,所以它可以完成工作。

这适用于从以下网址下载的 0.9 r25 版:http: //nicedit.com/download.php

于 2018-11-06T22:13:14.627 回答
0

简单的解决方案仍然适用于修订版 25(压缩)。res 数组是要替换的,如上所述。确保语法正确。
文件大小也有一些限制 - 实际上是 2M。尝试在代码或文件中更改它。并且不要忘记先创建上传文件夹并添加正确的链接。
希望这可以帮助某人。保持冷静。

于 2016-07-02T15:22:46.513 回答
0

上传到同一服务器上的图像时,我也收到此错误[object ProgressEvent],因为 从源“ http://example.com ”访问“ http://example.com/upload-editor-images ”的 XMLHttpRequest已被阻止通过 CORS 策略:对预检请求的响应未通过访问控制检查:预检请求不允许重定向。

解决方法:将nicURI:" http://example.com/upload-editor-images "改为nicURI:"upload-editor-images"

于 2019-03-08T06:47:31.090 回答