NicEdit:图片上传到自己的服务器不起作用 - >配置错了?

时间:2012-10-02 20:20:24

标签: php image upload nicedit

我对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 ......

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

nicEdit开发人员做了一些糟糕

他们更新了nicUpload.js的{​​{1}}版本,该版本与imgur.com不兼容。当您从他们的页面下载lib时,服务器只需将nicUpload.php版本包含在imgur中。

我弄清楚如何使这项工作。

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

nicEdit.js

之间修改 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'}
    }

从您从svn获得的nicEditors.registerPlugin(nicPlugin,nicUploadOptions); 粘贴nicUpload.js

并删除此行:

nicUpload/

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

...在var nicUploadButton中,以避免提示弹出,而不是记录您的登录值。似乎现在正在工作。

答案 4 :(得分:0)

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

这是文件:

uploader.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检查图像属性:

utils.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
}

警告: 我敢肯定,如果您将其用作开放式上传器,这样做的安全性还不够,但是我必须在受限制的页面上执行此操作,因此它可以完成工作。

这与从http://nicedit.com/download.php下载的0.9 r25版本一起使用

答案 5 :(得分:0)

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

解决方案:将 nicURI:“ http://example.com/upload-editor-images更改为 nicURI:“上传编辑器图像”