2

I'm trying to debug this issue by posting raw PNG image data to the server with the help of Postman. Here's a screenshot, which might help to understand the issue:

enter image description here

On the server I'm receiving the file as follows:

$png = $GLOBALS["HTTP_RAW_POST_DATA"];

Then I write the data to a new file:

$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $png);
fclose($fh);

The file gets saved correctly, but it now has a different file size, 417KB instead of 279KB which is the size of the original file.

Now of course, I can't do any image operation as none of the functions (such as getimagesize which returns bool(false)) recognizes the file as a valid image.

I have debugged this process to a point where the issue must be somewhere in the file operations, but I don't understand why the file just doesn't result in the very same file type and size as the original, when the only thing I am doing is using the same raw data.

UPDATE:

I've now compared the encodings of the original file with the uploaded one, and the former is in ISO-8859-1 and it displays correctly, the latter is in UTF-8 and has about 138kB more in file size.

Now I've achieved to convert the file on the server to ISO-8859-1.

fwrite($fh, iconv("UTF-8", "ISO-8859-1", $png));

The resulting file does now have the same output file size (279kB), but it is still not recognized as a PNG image, some information seems to still get lost.

UPDATE (1):

I've been able to examine the issue further and found out, that the original file is exactly 4 bytes bigger than the generated file, thus the resulting PNG seems to be corrupted.

Screenshot of the terminal showing the two different file sizes

UPDATE (2):

I'm now able to save the file and open it as a valid PNG. The following code seems to be saving the image correctly:

$input          = fopen("php://input","r+");
$destination    = fopen($myFile, 'w+');

stream_copy_to_stream($input, $destination);

fclose($input);
fclose($destination);   

However when trying to open the file with the imagecreatefrompng function I get a 500 error. I'm now trying to figure out if it's a memory issue in PHP.

4

3 に答える 3

1

問題は、「バイナリ」データをテキスト フィールドにコピーして POST をテストする方法にある可能性があります。

同じデータをテキスト エディターに貼り付けると、これを png 拡張子で保存するときに有効な画像ファイルが得られません。

アップロードをテストするために、ファイル フィールドを含む単純なフォームを作成してみてください

于 2012-10-25T12:23:49.697 に答える
0

アップロードにnginxを使用していて問題はありませんが、http://www.php.net/manual/en/features.file-upload.post-methodに従ってファイルをアップロードする標準のPHPの方法を使用しています。 php

それを試してみることをお勧めします。

于 2012-10-25T12:10:49.270 に答える
0

使用してみてください: < ?php $postdata = file_get_contents("php://input"); ?> 生データを取得するには。ケーキの ajax 投稿から送信されたデータを取得するために時々使用します。

于 2012-10-25T15:26:45.717 に答える