nic上传说“无效的上传ID”,无法使其正常工作

时间:2012-07-26 20:00:47

标签: upload wysiwyg nicedit

我尝试使用nicupload插件实现nicEdit,但是当我选择要上传的文件时,“无法上传图片”,服务器响应显示“无效的上传ID”。

这是调用脚本并初始化的代码:

<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">//<![CDATA[
bkLib.onDomLoaded(function() {
new nicEditor({uploadURI : '../../nicedit/nicUpload.php'}).panelInstance('area1');
});
//]]>
</script>

nicUpload.php的路径是正确的,代码是可以在文档中找到的代码:http://nicedit.com/src/nicUpload/nicUpload.js

我更改了上传文件夹,并设置了写权限。根据文档(http://wiki.nicedit.com/w/page/515/Configuration%20Options),这就是全部,但我不断收到错误。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

在寻找解决方案很长一段时间后(很多帖子没有真正的解决方案),我现在自己修复了代码。我现在能够将图像上传到我自己的服务器。瘟疫和日食; - )

主要问题是nicUpload.php已经过时了,无法使用当前的nicEdit-Upload功能。

缺少是错误处理,随意添加此...

nicEditor添加到您的php文件并配置以使用nicEdit.php:

new nicEditor({iconsPath : 'pics/nicEditorIcons.gif', uploadURI : 'script/nicUpload.php'}

下载 nicEdit.js未压缩并更改nicEdit.js中的以下行:

uploadFile : function() {
var file = this.fileInput.files[0];
if (!file || !file.type.match(/image.*/)) {
  this.onError("Only image files can be uploaded");
  return;
}
this.fileInput.setStyle({ display: 'none' });
this.setProgress(0);

var fd = new FormData(); 
fd.append("image", file);
fd.append("key", "b7ea18a4ecbda8e92203fa4968d10660");
var xhr = new XMLHttpRequest();
xhr.open("POST", this.ne.options.uploadURI || this.nicURI);

xhr.onload = function() {
  try {
    var res = JSON.parse(xhr.responseText);
  } catch(e) {
    return this.onError();
  }
  //this.onUploaded(res.upload); // CHANGE HERE
  this.onUploaded(res);
}.closure(this);
xhr.onerror = this.onError.closure(this);
xhr.upload.onprogress = function(e) {
  this.setProgress(e.loaded / e.total);
}.closure(this);
xhr.send(fd);

},

onUploaded : function(options) {
this.removePane();
//var src = options.links.original; // CHANGE HERE
var src = options['url'];
if(!this.im) {
  this.ne.selectedInstance.restoreRng();
  //var tmp = 'javascript:nicImTemp();';
  this.ne.nicCommand("insertImage", src);
  this.im = this.findElm('IMG','src', src);
}
var w = parseInt(this.ne.selectedInstance.elm.getStyle('width'));
if(this.im) {
  this.im.setAttributes({
    src : src,
    width : (w && options.image.width) ? Math.min(w, options.image.width) : ''
  });
}

}

像这样更改nicUpload.php

<?php
/* NicEdit - Micro Inline WYSIWYG
 * Copyright 2007-2009 Brian Kirchoff
 *
 * NicEdit is distributed under the terms of the MIT license
 * For more information visit http://nicedit.com/
 * Do not remove this copyright message
 *
 * nicUpload Reciever Script PHP Edition
 * @description: Save images uploaded for a users computer to a directory, and
 * return the URL of the image to the client for use in nicEdit
 * @author: Brian Kirchoff <briankircho@gmail.com>
 * @sponsored by: DotConcepts (http://www.dotconcepts.net)
 * @version: 0.9.0
 */

/* 
* @author: Christoph Pahre
* @version: 0.1
* @description: different modification, so that this php file is working with the newest nicEdit.js (needs also modification - @see) 
* @see http://stackoverflow.com/questions/11677128/nicupload-says-invalid-upload-id-cant-make-it-works
*/

define('NICUPLOAD_PATH', '../images/uploadedImages'); // Set the path (relative or absolute) to
                                      // the directory to save image files

define('NICUPLOAD_URI', '../images/uploadedImages');   // Set the URL (relative or absolute) to
                                      // the directory defined above

$nicupload_allowed_extensions = array('jpg','jpeg','png','gif','bmp');

if(!function_exists('json_encode')) {
    die('{"error" : "Image upload host does not have the required dependicies (json_encode/decode)"}');
}

if($_SERVER['REQUEST_METHOD']=='POST') { // Upload is complete

    $file = $_FILES['image'];
    $image = $file['tmp_name'];
    $id = $file['name'];

    $max_upload_size = ini_max_upload_size();
    if(!$file) {
        nicupload_error('Must be less than '.bytes_to_readable($max_upload_size));
    }

    $ext = strtolower(substr(strrchr($file['name'], '.'), 1));
    @$size = getimagesize($image);
    if(!$size || !in_array($ext, $nicupload_allowed_extensions)) {
        nicupload_error('Invalid image file, must be a valid image less than '.bytes_to_readable($max_upload_size));
    }

    $filename = $id;
    $path = NICUPLOAD_PATH.'/'.$filename;

    if(!move_uploaded_file($image, $path)) {
        nicupload_error('Server error, failed to move file');
    }

    $status = array();
    $status['done'] = 1;
    $status['width'] = $size[0];
    $rp = realpath($path);
    $status['url'] =  NICUPLOAD_URI ."/".$id;


    nicupload_output($status, false);
    exit;
} 

// UTILITY FUNCTIONS

function nicupload_error($msg) {
    echo nicupload_output(array('error' => $msg)); 
}

function nicupload_output($status, $showLoadingMsg = false) {
    $script = json_encode($status);
    $script = str_replace("\\/", '/', $script);
    echo $script;

    exit;
}

function ini_max_upload_size() {
    $post_size = ini_get('post_max_size');
    $upload_size = ini_get('upload_max_filesize');
    if(!$post_size) $post_size = '8M';
    if(!$upload_size) $upload_size = '2M';

    return min( ini_bytes_from_string($post_size), ini_bytes_from_string($upload_size) );
}

function ini_bytes_from_string($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }
    return $val;
}

function bytes_to_readable( $bytes ) {
    if ($bytes<=0)
        return '0 Byte';

    $convention=1000; //[1000->10^x|1024->2^x]
    $s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
    $e=floor(log($bytes,$convention));
    return round($bytes/pow($convention,$e),2).' '.$s[$e];
}

?>

答案 1 :(得分:1)

您可以手动将ID传递给您的脚本:例如nicUpload.php?id = introPicHeader,它将成为您定义的images文件夹中的introPicHeader.jpg(或相应的扩展名)。

但是,我注意到这个脚本坏了,如果在nicEditorAdvancedButton.extend期间直接在nicEdit.js中指定,则无法访问配置选项uploadURI({。这会导致访问相对路径的“未知”资源,导致错误。

文档另有说明,而且这里为imgur.com指定了nicURI(可能是默认的)这一事实让我觉得我还可以在一个地方而不是在一个地方添加对nicUpload.php脚本的uploadURI引用每个编辑器实例化。

<强>更新

如果你在实例化期间传递它,这是有效的,我想这确实允许简单的动态id群。

不幸的是,nicUpload.php充满了错误,它的输出不是JSON。编辑器期望解析JSON并找到脚本标记和带有意外标记“&lt;”的错误。

我将尝试识别大量其他错误:

在nicEdit.js

  1. A.append(“image”)应该是实际的A.append(“nicImage”)
  2. this.onUploaded(D.upload)应该成为this.onUploaded(D)
  3. this.onUploaded(D)应该在var D = JSON.parse(C.responseText)之后移动到try块内以修复变量范围问题
  4. B.image.width需要成为B.width
  5. 在nicUpload.php中

    1. JSON输出格式不正确,注释掉html输出并输出json_encode($ status)。
    2. JSON输出需要返回一个名为links而不是url的键/值对,尽管在varEdit.js中将var D = B.links重命名为var D = B.url也可以作为修复。
    3. php和javascript代码都有很多不足之处,我经常会遇到很多错误并且一直在修复它们。