cross-domain-upload.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * PHP cross-domain upload demo for Editor.md
  4. *
  5. * @FileName: upload.php
  6. * @Auther: Pandao
  7. * @E-mail: pandao@vip.qq.com
  8. * @CreateTime: 2015-02-15 19:12:11
  9. * @UpdateTime: 2015-02-15 20:47:52
  10. * Copyright@2015 Editor.md all right reserved.
  11. */
  12. header("Content-Type:text/html; charset=utf-8");
  13. header("Access-Control-Allow-Origin: *");
  14. require("editormd.uploader.class.php");
  15. error_reporting(E_ALL & ~E_NOTICE);
  16. $path = __DIR__ . DIRECTORY_SEPARATOR;
  17. $url = dirname($_SERVER['PHP_SELF']) . '/';
  18. $savePath = realpath($path . '../uploads/') . DIRECTORY_SEPARATOR;
  19. $saveURL = '//'. $_SERVER['SERVER_NAME'] . $url . '../uploads/'; // 本例是演示跨域上传所以加上$_SERVER['SERVER_NAME']
  20. $formats = array(
  21. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp')
  22. );
  23. $name = 'editormd-image-file'; // file input name
  24. $callbackUrl = $_GET['callback'];
  25. if (isset($_FILES[$name]))
  26. {
  27. $imageUploader = new EditorMdUploader($savePath, $saveURL, $formats['image'], false); // Ymdhis表示按日期生成文件名,利用date()函数
  28. $imageUploader->config(array(
  29. 'maxSize' => 1024, // 允许上传的最大文件大小,以KB为单位,默认值为1024
  30. 'cover' => true // 是否覆盖同名文件,默认为true
  31. ));
  32. $imageUploader->redirect = true;
  33. $imageUploader->redirectURL = $callbackUrl . (empty(parse_url($callbackUrl)['query']) ? '?' : '&') . 'dialog_id=' . $_GET['dialog_id'] . '&temp=' . date('ymdhis');
  34. if ($imageUploader->upload($name))
  35. {
  36. $imageUploader->message('上传成功!', 1);
  37. }
  38. else
  39. {
  40. $imageUploader->message('上传失败!', 0);
  41. }
  42. }
  43. ?>