How to echo raw post data in PHP?
My goal is to simply echo $_POST['imageVar']; back with content headers as a quick and dirty means to "export" an image from a flash application. I saw an example of this as follows, but it does not work with my php version/config:
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $jpg;
}
So as a work around I created a script that saves the image to the server, reads the image and echos it back, then deletes the image. I don't like this approach as I need to have a directory that is writable by the apache user (im on a shared server). Is there a way to accomplish what I am doing here without hte need to use the temp file?
<?
$fileName = basename( $_FILES['uploadedfile']['name']);
$tempFile = "uploads/" . $fileName;
$fileSize = $HTTP_POST_FILES['uploadedfile']['size'];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $tempFile)) {
$fh = fopen($tempFile, 'r');
$fileCon开发者_开发技巧tents = fread($fh, $fileSize);
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=$fileName");
echo $fileContents;
fclose($fh);
unlink($tempFile);
} else {
echo "upload fail";
}
?>
Any input or ideas greatly appreciated! Thanks for looking
You can just use:
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=$fileName")
readfile($_FILES['uploadedfile']['tmp_name']);
No need to move it if you're not going to keep it.
On a side note, regarding the first solution you were looking at, does an echo file_get_contents('php://input');
not work?
$s = file_get_contents('php://input');
// add headers for download dialog-box
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $s;
What about that?
精彩评论