尝试使用php从Flash保存图像时出错#2044

时间:2011-07-26 13:16:33

标签: php actionscript-3

我在Flash Player的调试版中遇到此错误 错误#2044:未处理的ioError: text =错误#2032:流错误

我在网上搜索没有成功的解释

下面的代码在WampServer http://localhost/中有效,但在我将其在服务器上联机时则不行:

as3:


import com.adobe.images.JPGEncoder;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequest;

var jpgSource:BitmapData = new BitmapData(img_mc.width,img_mc.height); jpgSource.draw(img_mc); var jpgEncoder:JPGEncoder = new JPGEncoder(85); var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

//set the request's header,method and data var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream"); var loader:URLLoader = new URLLoader(); //sends jpg bytes to saveJPG.php script var myRequest:URLRequest = new URLRequest("saveJPG.php"); myRequest.requestHeaders.push(header); myRequest.method = URLRequestMethod.POST; myRequest.data = jpgStream; loader.load(myRequest); //fire complete event; loader.addEventListener(Event.COMPLETE,saved); function saved(e:Event) { //trace the image file name trace(loader.data); }

saveJPG.php:


if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

//the image file name   
$fileName = 'img.jpg';

// get the binary stream
$im = $GLOBALS["HTTP_RAW_POST_DATA"];

//write it
$fp = fopen($fileName, 'wb');
fwrite($fp, $im);
fclose($fp);

//echo the fileName;
echo $fileName;

} else echo 'result=An error occured.';

更新

我试过//the image file name $fileName = 'img.jpg'; // get the binary stream $im = $GLOBALS["HTTP_RAW_POST_DATA"]; //write it $fp = fopen($fileName, 'wb'); fwrite($fp, $im); fclose($fp); //echo the fileName; echo $fileName;

而不是$im = file_get_contents("php://input");

我得到了同样的错误。

3 个答案:

答案 0 :(得分:1)

var myRequest:URLRequest = new URLRequest("saveJPG.php");

尝试为请求设置完整的网址。

答案 1 :(得分:1)

Error #2044: Unhandled ioError:. text=Error #2032: Stream Error

做它想要的东西添加未处理的ioError eventlistener

loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler );
function ioErrorHandler( e:Event ):void{
  trace('ioErrorHandler ' + e.toString() )
}

你也应该有这个

loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
function securityErrorHandler ( e:Event ):void{
  trace('securityErrorHandler ' + e.toString() )
}

答案 2 :(得分:0)

似乎服务器阻止我交换字节或使用$GLOBALS["HTTP_RAW_POST_DATA"];或类似的东西。

所以我想出了不同的方法,而不是将图像作为字节发送到php文件 我将它发送为十六进制字符串:


import com.adobe.images.JPGEncoder;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequest;

var jpgSource:BitmapData = new BitmapData(img_mc.width,img_mc.height); jpgSource.draw(img_mc); var jpgEncoder:JPGEncoder = new JPGEncoder(80); var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

//get the whole jpgStream as a string of hex var hexStream:String=""; for (var i:int=0;i < jpgStream.length ; i++) { hexStream += decToHex(jpgStream[i]); }

function decToHex(dec:int):String { var hex:String = dec.toString(16).toUpperCase(); if (dec<16) { hex = "0"+hex; } return hex; }

//transfer hexStream to the server

var variables:URLVariables = new URLVariables(); variables.hexStream = hexStream;

var myRequest:URLRequest = new URLRequest("saveimg.php"); myRequest.method = URLRequestMethod.POST; myRequest.data = variables;

var loader:URLLoader = new URLLoader(myRequest); loader.dataFormat = URLLoaderDataFormat.BINARY loader.load(myRequest);

在服务器端只有一行PHP代码

file_put_contents('img.jpg', pack('H*', $_POST['hexStream']));

显然这比使用HTTP_RAW_POST_DATA慢 但是嘿