POST method uploads errors

$_FILES[“file”][“error”]是错误代码,0表示没有错误,下面几种对应不同的错误
1 : 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值.
2 : 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
3 : 文件只有部分被上传
4 : 没有文件被上传

Error Messages Explained

UPLOAD_ERR_OK
Value: 0; There is no error, the file uploaded with success.

UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

UPLOAD_ERR_PARTIAL
Value: 3; The uploaded file was only partially uploaded.

UPLOAD_ERR_NO_FILE
Value: 4; No file was uploaded.

UPLOAD_ERR_NO_TMP_DIR
Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE
Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

UPLOAD_ERR_EXTENSION
Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.

Tags: ,

Use Wireshark to capture loopback traffic without a loopback adapter

If you’ve ever used Wireshark for debugging applications you may have noticed that it only seems to pick up traffic that is actually transmitted over the wire and ignores all traffic sent to your local ip address or localhost. If you want to watch this traffic without having to install a special loopback adapter you can use the following trick.

How to force local traffic to your default gateway
1) Open a command prompt (Run as Administrator for Vista/7)
2) Type ipconfig/all (note your local ip address(es) and default gateway)
3) Type “route add <your ip address> mask 255.255.255.255 <default gateway IP address> metric 1″

This instructs windows to send any requests for your local ip address to your default gateway, which will in turn forward the request back to your machine. Be aware that this route will disappear once you restart your machine unless you include the -p switch after the route command. You may also notice an echo effect if you’re using Wireshark because you see each request and response twice. You can remove this problem by applying the following filter at the top.

ip.src==<default gateway> or ip.dst==<default gateway>

Consider the default gateway as a client trying to reach your machine and all traffic sent to the default gateway as your machine’s response.

To remove the route, type “route delete <the ip address you entered>”.

If you have an application running locally that uses localhost, you can map localhost to the IP address you added a route for. Just don’t forget you mapped localhost to a different IP than 127.0.0.1!

How to map localhost
1) Open notepad (Run as Administrator in Vista/7)
2) Navigate to C:\Windows\System32\drivers\etc\ and open the hosts file (there’s no extension).
3) Add this entry “<the IP address you added a route for> localhost”. Note that the space between the ip address and localhost is a tab.

Now, when your machine tries to send something to localhost, it will resolve to the IP address you added a route for and send its traffic to your default gateway.

(Important!) Remember to unblock the port used for incoming traffic on your machine. Also, if you find that an application you’re using doesn’t seem to send out traffic the way you expect, try flushing the dns cache with ipconfig/flushdns.

Tags: , , ,

PHP 未知类型变量 empty isset 陷阱

用 empty 检查位置类型的变量时,容易掉进难以察觉的陷阱,请看下面简单的代码,预测一下 3 个 var_dump 的输出:

$mixed_1    = array();
$mixed_2    = 'Nihao';

$bool_1        = empty($mixed_1['1_type']);
$bool_2        = empty($mixed_2['2_type']);

var_dump($bool_1);
var_dump($bool_2);

var_dump($mixed_2['2_type']);

为了不影响你的判断,我多空几行,然后对比结果,是否跟你判断的一致。

公布结果:

bool(true)
bool(false)
string(1) "h"

后面 2 个结果,是不是有点意外?

导致问题的原因,就是 PHP 会自动把字符串按照数组来处理,并且,当 $mixed 不是严格意义的数组时,对元素的引用 key 会把 key 转换为数字,2_type 转换为数字之后,就是数字 “2”,对应的元素值就是 “h”。

对应的,isset 也会认为 $mixed_2[‘2_type’] 是存在的。

结论:

在对未知类型的变量做 isset 或 empty 查询时,应同时附加 is_array 等类型检查,才会更安全。

Tags: , ,

Firefox 网页 光标 闪烁

最近 Firefox 出现怪异情况:鼠标点击网页,在点击的位置显示光标,并一直闪烁,导致 Home End 等按键都无效。

原来这是 Firefox 的 “特色功能”:Caret Browsing,激活这个功能以后,我们可以使用键盘来选择页面内容。

这个功能的开关是F7,打开Firefox,按一下F7,就会跳出一个对话框:

“Pressing F7 turns Caret Browsing on or off. This feature places a moveable cursor in web pages, allowing you to select text with the keyboard. Do you want to turn Caret Browsing on?”

选择”Yes”开;选择”No”关。

Tags:

黑色礼物

滴滴、滴滴。。。无数的报警短信,让手机一直在不停地响着,每响一次,都让我心惊胆战。

我盼望这种煎熬赶紧过去,但这无数的报警短信,排成了长长的队列,一个一个从手机里蹦出来,变成尖刀,插进我的心脏。

页面白了,白了三分钟。

我的世界黑了,不知道黑暗什么时候可以过去。

我坐在座位上,眼睛紧紧地盯着屏幕。

惊魂未定。脑子里一片空白。

我不敢离开座位,我不敢出现在同事面前,我不敢看同事的眼睛。

我不敢离开公司,我不敢出现在路人的视线里,我就像个罪人,似乎全世界的人都变成了受害者,而我是唯一的罪魁祸首。

这一切偏偏发生在 10 月 26 日,我的命运就像一场闹剧。

Yoyo 说:要勇敢面对问题。

恩,我会的。

Iframe Tips ABC

通常我们用 js 脚本创建 iframe 时,会这样写:

var iframe = document.createElement('iframe');

之后我们可能会定义 id、name、border 等属性,这些看似简单,其实 IE 与非 IE 浏览器之间、IE 和 IE 高版本之间的迥异,使得 iframe 的相关兼容性操作非常地有文章。

牛A:frameborder

现象:使用 (iframe.frameBorder = 数值) 或(iframe.setAttribute(‘frameborder’,数值)) 在 IE 浏览器下无效
原因:IE 浏览器区分属性名称大小写
解决方法:iframe.setAttribute(‘frameBorder’,’0′) Or iframe.setAttribute(‘frameborder’,’0′,0),兼容所有浏览器。

注:经测试,IE8已经修复此问题

牛B:动态将Form target到iframe

背景:假设现在我们要让一个 Form 表单结果提交到一个 HTML 结构中已存在的 iframe,会这样做:

&lt;form id="form" name="form" target="相应iframe的name:iframeNB" method="post" &gt;<br style="margin: 0px; padding: 0px;" />&lt;/form&gt;<br style="margin: 0px; padding: 0px;" />&lt;iframe name="iframeNB" &gt;&lt;/iframe&gt;

OK,什么问题也没有,再假设我们需要提交到脚本动态生成的 iframe 中,会这样做:

&lt;form id="form" name="form" target="iframeNB" method="post" &gt;<br style="margin: 0px; padding: 0px;" />&lt;/form&gt;<br style="margin: 0px; padding: 0px;" />&lt;script&gt;<br style="margin: 0px; padding: 0px;" />var iframe = document.createElement('iframe');<br style="margin: 0px; padding: 0px;" />iframe.name = 'iframeNB';<br style="margin: 0px; padding: 0px;" />...<br style="margin: 0px; padding: 0px;" />someParent.appendChild(iframe);<br style="margin: 0px; padding: 0px;" />&lt;/script&gt;

去 IE 浏览器里试试,你会发现 Form 会在新窗口显示提交结果,Why?
原因:
我为此尝试了很久,结果是IE此前版本不能通过(iframe.name=)这种方式给 iframe 设置 name 值,也就是说生成的 iframe 是没有 name 值的,但却可以 alert 出来,这很诡异;当然,这也并不是没有解决办法。
解决方法,为此我们得为 IE 单独写一行代码:


  <span style="margin: 0px; padding: 0px; color: gray;">/*only for ie */</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">var</span> iframe = document.createElement(<span style="margin: 0px; padding: 0px; color: teal;">'&lt;iframe name="iframeNB"&gt;'</span>); <br style="margin: 0px; padding: 0px;" />

看到这行代码,我们笑了,这是天大的杯具(喜剧?)~~不管IE有多么搓的问题,他总会有自己一套解决之……
而且这行代码会在其他非 IE 浏览器抛出异常,所以我们可以利用这点来做最终版:


    <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">var</span> iframe; <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">try</span><span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />        iframe = document.createElement(<span style="margin: 0px; padding: 0px; color: teal;">'&lt;iframe name="iframeNB"&gt;'</span>); <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span><span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">catch</span>(e)<span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />        iframe = document.createElement(<span style="margin: 0px; padding: 0px; color: teal;">'iframe'</span>); <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span> <br style="margin: 0px; padding: 0px;" />    iframe.name = <span style="margin: 0px; padding: 0px; color: teal;">'iframeNB'</span>; <br style="margin: 0px; padding: 0px;" />    ... <br style="margin: 0px; padding: 0px;" />    someParent.appendChild(iframe); <br style="margin: 0px; padding: 0px;" />    ... <br style="margin: 0px; padding: 0px;" />

[2009-12-9]补充:最佳实践 – YUI 是如何 creat iframe 的


    <span style="margin: 0px; padding: 0px; color: gray;">/** <br style="margin: 0px; padding: 0px;" />    * @description Creates an iframe to be used for form file uploads.  It is remove from the <br style="margin: 0px; padding: 0px;" />    * document upon completion of the upload transaction. <br style="margin: 0px; padding: 0px;" />    * @method createFrame <br style="margin: 0px; padding: 0px;" />    * @private <br style="margin: 0px; padding: 0px;" />    * @static <br style="margin: 0px; padding: 0px;" />    * @param {string} optional qualified path of iframe resource for SSL in IE. <br style="margin: 0px; padding: 0px;" />    * @return {void} <br style="margin: 0px; padding: 0px;" />    */</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: #4169e1; font-weight: bold;">function</span> _createFrame(secureUri)<span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: green;">// IE does not allow the setting of id and name attributes as object</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: green;">// properties via createElement().  A different iframe creation</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: green;">// pattern is required for IE.</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">var</span> frameId = <span style="margin: 0px; padding: 0px; color: teal;">'yuiIO'</span> + <span style="margin: 0px; padding: 0px; color: #4169e1; font-weight: bold;">this</span>._transaction_id,io; <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">if</span>(YAHOO.env.ua.ie)<span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />        io = document.createElement(<span style="margin: 0px; padding: 0px; color: teal;">'&lt;iframe id="'</span> + frameId + <span style="margin: 0px; padding: 0px; color: teal;">'"name="'</span> + frameId + <span style="margin: 0px; padding: 0px; color: teal;">'" /&gt;'</span>); <br style="margin: 0px; padding: 0px;" />        <span style="margin: 0px; padding: 0px; color: green;">// IE will throw a security exception in an SSL environment if the</span> <br style="margin: 0px; padding: 0px;" />        <span style="margin: 0px; padding: 0px; color: green;">// iframe source is undefined.</span> <br style="margin: 0px; padding: 0px;" />        <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">if</span>(<span style="margin: 0px; padding: 0px; color: #4169e1; font-weight: bold;">typeof</span> secureUri == <span style="margin: 0px; padding: 0px; color: teal;">'boolean'</span>)<span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />            io.src = <span style="margin: 0px; padding: 0px; color: teal;">'javascript:false'</span>; <br style="margin: 0px; padding: 0px;" />        <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span> <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span><span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">else</span><span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />        io = document.createElement(<span style="margin: 0px; padding: 0px; color: teal;">'iframe'</span>); <br style="margin: 0px; padding: 0px;" />        io.id = frameId; <br style="margin: 0px; padding: 0px;" />        io.name = frameId; <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span> <br style="margin: 0px; padding: 0px;" />    io.style.position = <span style="margin: 0px; padding: 0px; color: teal;">'absolute'</span>; <br style="margin: 0px; padding: 0px;" />    io.style.top = <span style="margin: 0px; padding: 0px; color: teal;">'-1000px'</span>; <br style="margin: 0px; padding: 0px;" />    io.style.left = <span style="margin: 0px; padding: 0px; color: teal;">'-1000px'</span>; <br style="margin: 0px; padding: 0px;" />    document.body.appendChild(io); <br style="margin: 0px; padding: 0px;" />    YAHOO.log(<span style="margin: 0px; padding: 0px; color: teal;">'File upload iframe created. Id is:'</span> + frameId, <span style="margin: 0px; padding: 0px; color: teal;">'info'</span>, <span style="margin: 0px; padding: 0px; color: teal;">'Connection'</span>); <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span> <br style="margin: 0px; padding: 0px;" />

这里需要额外注意到的一点是:

// IE will throw a security exception in an SSL environment if the<br style="margin: 0px; padding: 0px;" />// iframe source is undefined.<br style="margin: 0px; padding: 0px;" />if(typeof secureUri == 'boolean'){<br style="margin: 0px; padding: 0px;" />io.src = 'javascript:false';<br style="margin: 0px; padding: 0px;" />}

姑且算是牛D吧 =.=!

牛C:iframe.onload

关于 onload 这点大家可以参考怿飞师父的文章:判断 iframe 是否加载完成的完美方法,在此纯引用一次代码:


<span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">var</span> iframe = document.createElement(<span style="margin: 0px; padding: 0px; color: teal;">"iframe"</span>); <br style="margin: 0px; padding: 0px;" />iframe.src = <span style="margin: 0px; padding: 0px; color: teal;">"http://www.planabc.net"</span>; <br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">if</span> (iframe.attachEvent) <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />    iframe.attachEvent(<span style="margin: 0px; padding: 0px; color: teal;">"onload"</span>, <span style="margin: 0px; padding: 0px; color: #4169e1; font-weight: bold;">function</span> () <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />        alert(<span style="margin: 0px; padding: 0px; color: teal;">"Local iframe is now loaded."</span>); <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span>); <br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span> <span style="margin: 0px; padding: 0px; color: navy; font-weight: bold;">else</span> <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />    iframe.onload = <span style="margin: 0px; padding: 0px; color: #4169e1; font-weight: bold;">function</span> () <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">{</span> <br style="margin: 0px; padding: 0px;" />        alert(<span style="margin: 0px; padding: 0px; color: teal;">"Local iframe is now loaded."</span>); <br style="margin: 0px; padding: 0px;" />    <span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span>; <br style="margin: 0px; padding: 0px;" /><span style="margin: 0px; padding: 0px; color: red; font-weight: bold;">}</span> <br style="margin: 0px; padding: 0px;" />document.body.appendChild(iframe); <br style="margin: 0px; padding: 0px;" />

需要注意到的是:

  • IE8也不支持iframe.onload
  • Opera两者均可,所以使用此方法会绑定前者
  • 即使我们不预设iframe.src = some urls,也会默认执行一次onload事件,可以通过分析 src 规避。

附测试文件:
1.iframe_ie_ugly.html
2.iframe_fixed.html
以上,我认为 ABC 中最牛的就是 B 了,这也是我标题的亮点=.=! 斯密达们认为呢?

上述文字转自 http://blog.silentash.com/2009/12/iframe-tips-abc/,感谢原作者给我提供的巨大帮助。

转载在这里与更多人分享。

Tags:

IE Bug Javascript

问题描述:
当 A 标签的文本是 http 开头时,设置 href 属性,会同时以相同的值更新链接文本,反之亦然。

解决方法:
在设置 href 时,在开头添加一个空格 ‘ ‘,即可解决此 bug。

测试 Demo:

http://code.leakon.com/javascript/set_href/set_href.html

注意,只在 IE 下会出现错误。

预期的功能是修改链接的 href 属性。

使用默认的修改方法有 bug,会把链接文本改掉!

Tags: ,

0x80040111 NS_ERROR_NOT_AVAILABLE

Firefox 在处理 Ajax 遇到错误时会报 0x80040111 异常。

在网上查了下,有的说法是,在很短的时间间隔发送了 2 次 Ajax 请求,XMLHttpRequest 的值还没有返回回来就又被请求了一次。

还有一个情况,是我遇到的,在请求成功返回后执行回调函数时,引用了空对象的属性,debug 报错:”null has no properties“。

目前只遇到这 2 种情况,记录备忘。

Tags: