PHP Soap binary image issue - php

I have a web service for iPhone. I receive an image from iPhone in binary form. What I want to ask is can we determine the extension of image being send in binary form.
my code:
$data=base64_decode($data);
$path='event_image/img_out.gif'; /// issue is here
$fp=fopen($path,'w+');
if($fp){ fwrite($fp,$data); fclose($fp); }

Files have signatures http://www.garykessler.net/library/file_sigs.html
You can read from the first line of the file and check.
function output_jpeg($filename)
{
if(($fp = fopen($filename, “rb”)) === false) {
return;
}
$line = fread($fp, 4);
// check the ‘magic number’ of the file
if($line === “\377\330\377\340”) {
fseek($fp, 0);
fpassthru($fp);
}
fclose($fp);
}

Related

how to stream video from a thread in PHP?

hey i am trying to write web page that stream live video, from my locally server. i want to use threads. so i tried to use file_get_content and i got gibberish presenting in my web page . this is the thread, does anybody know different method for presenting video within the thread?
class stream extends Thread {
public function run() {
$file = "/var/www/html/movie.mp4"; // The media file's location
$f = fopen($file, 'rb'); // Open the file in binary mode
$chunkSize = 8192; // The size of each chunk to output
// Start outputting the data
while(true){
fpassthru($f);
echo file_get_contents('/var/www/html/movie.mp4');
//echo fread($f, $chunkSize);
//$data = fread($f, $chunkSize);
// echo $data;
flush();
}
}
}

read a big text file and copy the selected lines to another file

I need to create a database table from here:
So, I copied the same file to a server folder using curl command. I need to retrieve 00-00-00 (hex) XEROX CORPORATION, 00-00-01 (hex) XEROX CORPORATION etc. from the above text file. I need to copy only the hex values and the first line of organisation to another text file.
How can I do it with PHP?
<?php
// open the input and the output
$fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt","r");
$out = fopen("somefile.txt","w");
while($rec = fgets($fp)){
// check to see if the line contains '(hex)'
if (strpos($rec, '(hex)') !== false){
// if so write it out
fputs($out, $rec."\n");
}
}
fclose($fp);
fclose($out);
Try this one
<?php
$i = 1;
$a_line = "";
$first_line = "";
$handle = #fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
if($i == 1)
// here you have first line
$first_line = $buffer;
else {
// check the $buffer contains the substring (hex) and XEROX CORPORATION
// if it contains put it in another variable
$a_line .= $buffer;
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
// finally write the $first_line to a header file
// then write $a_line to another file
?>
<?php
error_reporting(0);
$fs=fopen("1.txt", "r");
$ft=fopen("2.txt", "w");
if ($fs==NULL)
{
echo "Can't Open Source File ...";
exit(0);
}
if ($ft==NULL)
{
echo "Can't Open Destination File ...";
fclose ($fs);
exit(1);
}
else
{
while ($ch=fgets($fs))
fputs($ft, $ch);
fclose ($fs);
fclose ($ft);
}
?>

getimagesize() limiting file size for remote URL

I could use getimagesize() to validate an image, but the problem is what if the mischievous user puts a link to a 10GB random file then it would whack my production server's bandwidth. How do I limit the filesize getimagesize() is getting? (eg. 5MB max image size)
PS: I did research before asking.
You can download the file separately, imposing a maximum size you wish to download:
function mygetimagesize($url, $max_size = -1)
{
// create temporary file to store data from $url
if (false === ($tmpfname = tempnam(sys_get_temp_dir(), uniqid('mgis')))) {
return false;
}
// open input and output
if (false === ($in = fopen($url, 'rb')) || false === ($out = fopen($tmpfname, 'wb'))) {
unlink($tmpfname);
return false;
}
// copy at most $max_size bytes
stream_copy_to_stream($in, $out, $max_size);
// close input and output file
fclose($in); fclose($out);
// retrieve image information
$info = getimagesize($tmpfname);
// get rid of temporary file
unlink($tmpfname);
return $info;
}
You don't want to do something like getimagesize('http://example.com') to begin with, since this will download the image once, check the size, then discard the downloaded image data. That's a real waste of bandwidth.
So, separate the download process from the checking of the image size. For example, use fopen to open the image URL, read little by little and write it to a temporary file, keeping count of how much you have read. Once you cross 5MB and are still not finished reading, you stop and reject the image.
You could try to read the HTTP Content-Size header before starting the actual download to weed out obviously large files, but you cannot rely on it, since it can be spoofed or omitted.
Here is an example, you need to make some change to fit your requirement.
function getimagesize_limit($url, $limit)
{
global $phpbb_root_path;
$tmpfilename = tempnam($phpbb_root_path . 'store/', unique_id() . '-');
$fp = fopen($url, 'r');
if (!$fp) return false;
$tmpfile = fopen($tmpfilename, 'w');
$size = 0;
while (!feof($fp) && $size<$limit)
{
$content = fread($fp, 8192);
$size += 8192; fwrite($tmpfile, $content);
}
fclose($fp);
fclose($tmpfile);
$is = getimagesize($tmpfilename);
unlink($tmpfilename);
return $is;
}

Using WAMP: Read file permissions on Windows

my system : windos xp
I have given all the permission to all user for file.
but i can not read file but I get filesize,
why this thing happen, reason i can not identify.
what should i do to over come this problem.
Code
$fileName = "1.php";
if (floatval(phpversion()) >= 4.3) {
//loading data
$fileData = file_get_contents($fileName);
print(filesize($fileName));
} else {
//if file not exist then return -3
if (!file_exists($fileName)) {
eturn -3;
}
$fp = fopen($fileName, 'r');
// if file is not open in read mode then return -2
if (!$fp) return -2;
$fileData = '';
print(filesize($fileName));
//checking end of file
while(!feof($fp))
$fileData .= fgetc($fileName);
fclose($fp);
}
echo $fileData;
Your problems are:
eturn should say return - this is probably a parse error
The actual problem is that you are calling fgetc($fileName) when it should be fgetc($fp). You are passing the string of the filename to fgetc() instead of the file pointer your created.
Change:
$fileData .= fgetc($fileName);
To
$filedata .= fgetc($fp);

Copying file uploaded vai PHP's HTTP Get in PHP 4

I have been working on adding functionality to a site originally written in PHP 4.4.9. It's not in their budget to port the site to PHP5, so don't even suggest it. (Although it needs it badly). The problem I am facing is how to copy binary data from a GET request to a file location on the server. The code that is currently written to support this method is as follows:
function save($path) {
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
if ($realSize != $this->getSize()){
return false;
}
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
}
The problem that I am having with this is the funciton stream_copy_to_stream is only supported in PHP 5. Here is what I have so far, but it seems to only create files that are 8K in size and I'm not sure why. It should, in theory, allow for up to 20M.
function save($path) {
$input = fopen("php://input", "rb");
$temp = tmpfile();
fwrite($temp, fread($input, 20971520));
fclose($input);
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
#stream_copy_to_stream($temp, $target);
fwrite($target, fread($temp, 20971520));
fclose($target);
echo $path;
return true;
}
I removed the size check because I couldn't figure a way to get the filesize when reading. Any help is greatly appreciated on this. I have been racking my brain for literally hours and I know there is someone out there, most likely on stackoverflow, that can answer my question probably very easily.
Thanks for all the help in advance!
Also, I am submitting data via GET to to allow multiple file uploads with progress bars, etc.
I came across this thread looking for answer for exact same problem.
I know post is old but putting answer here for anyone else looking.
You were close.
fread only takes 8192 byte chunks out of stream at a time. So you will have to loop through until it sees end of file.
function save($path) {
$input = fopen("php://input", "rb");
$temp = tmpfile();
while(!feof($input))
fwrite($temp, fread($input, 8192));
//fwrite($temp, fread($input, 20971520));
fclose($input);
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
#stream_copy_to_stream($temp, $target);
while(!feof($temp))
fwrite($target, fread($temp, 8192));
fclose($target);
echo $path;
return true;
}

Categories