内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>php - Uploading image (as data:imagepng:base64) to webserver - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Uploading image (as data:imagepng:base64) to webserver - Stack Overflow

programmeradmin0浏览0评论

I have an image on a web page constructed like so:

<img src="data:image/png;base64;...." />

The contents of the image e from the user pasting into the browser. My question is how do I then upload the image to the webserver (PHP if that matters).

I have an image on a web page constructed like so:

<img src="data:image/png;base64;...." />

The contents of the image e from the user pasting into the browser. My question is how do I then upload the image to the webserver (PHP if that matters).

Share Improve this question asked Feb 7, 2012 at 23:41 ChrisChris 27.4k49 gold badges206 silver badges357 bronze badges 4
  • do you mean pasting in a textarea for example? this sounds quite odd... could you explain the situation a little bit more? – giorgio Commented Feb 7, 2012 at 23:44
  • I have an input that, when a user pastes image data into it (i.e. from clipboard - in Chrome only) itll assign the data to an image as a preview before it is uploaded – Chris Commented Feb 7, 2012 at 23:59
  • @Chris If you want to see an actual js code show what you already have, not just a small unrelated piece of it. – Cheery Commented Feb 8, 2012 at 0:01
  • i totally understand now :) @Cheery has given you a pretty good answer! – giorgio Commented Feb 8, 2012 at 0:01
Add a ment  | 

2 Answers 2

Reset to default 7

1) Take the src attribute with javascript (or the data submitted by user)

2) Submit it to the server 'as is' or cut and submit everything after base64; (AJAX or POST, method GET is probably not very suitable here for large images)

3) Decode base64 on server side (everything after base64; if not cutted), save the result as binary - it is an image.

That's it.

ps: just a reminder - by careful with possible code injection. Check the submitted data or somebody will upload encoded php script. Disable php engine in the folder with uploads and verify that the final result is an actual image (with the help of GD library, for example). Even if the script can not run on your server it could be used for malicious requests to other servers with php scripts.

Just post the base 64 encoded text to your server.

You could save it as...

file_put_contents($image, base64_decode($str));
发布评论

评论列表(0)

  1. 暂无评论