Link to home
Start Free TrialLog in
Avatar of xSinbad
xSinbad

asked on

Help on server app - Retrieve and convert file

See Q.  https://www.experts-exchange.com/questions/21100804/Advice-on-a-server-app.html

What I want to have is a web page where I can input a URL for a file on another web server. My web server will then download this file and then convert it from Binary to ASCII (text) (don't ask) and then make it available for download.

Sounds easy but I dont know PHP because I just started learning it yesterday, but never fear I am a quick learner given a good teacher 8-)


Cheers
Marcus
Avatar of Gitcho
Gitcho

This function will get u part of the way there ....  

-------------------------------------------------------------------------------------
function URLopen($url)
{
      // Fake the browser type
      ini_set('user_agent','MSIE 4\.0b2;');
      $dh = fopen("$url",'rb');
      $f = fopen($url,"rb");
      while ($r = fread($f,8192)) {
            $result .= $r;
      }
      fclose($f);
      return $result;
}

echo URLopen("http://www.google.com");

-------------------------------------------------------------------------------------

At some level, everything is binary ... so I guess the question is how do you want your results to display --- or  what do you want the file to look like (eg. html from google as file contents) ?
these might help ...

-------------------------------------------------------------------------------------
// convert an input string into it's binary equivalent.
function asc2bin($inputString, $byteLength=8)
{
    $binaryOutput = '';
    $strSize = strlen($inputString);

    for($x=0; $x<$strSize; $x++)
    {
        $charBin = decbin(ord($inputString{$x}));
        $charBin = str_pad($charBin, $byteLength, '0', STR_PAD_LEFT);
        $binaryOutput .= $charBin;
    }

    return $binaryOutput;
}

-------------------------------------------------------------------------------------

// convert a binary representation of a string back into it's original form.
function bin2asc($binaryInput, $byteLength=8)
{
    if (strlen($binaryInput) % $byteLength)
    {
        return false;
    }
   
    // why run strlen() so many times in a loop? Use of constants = speed increase.
    $strSize = strlen($binaryInput);
    $origStr = '';

    // jump between bytes.
    for($x=0; $x<$strSize; $x += $byteLength)
    {
        // extract character's binary code
        $charBinary = substr($binaryInput, $x, $byteLength);
        $origStr .= chr(bindec($charBinary)); // conversion to ASCII.
    }
    return $origStr;
}

-------------------------------------------------------------------------------------

$inputString = "An input string, complete with punctuation!";

$binOut = asc2bin($inputString);
printf("Input String: %s\nBinary Version: %s\n",$inputString, $binOut);

$ascOut = bin2asc($binOut);
printf("Input Binary: %s\nOutput ASCII: %s\n",$binOut, $ascOut);
Avatar of xSinbad

ASKER

Thanks Gitcho I will have a look at those today.

>At some level, everything is binary ... so I guess the question is how do you want your results to display --- or  what do you want the file to look like (eg. html from google as file contents) ?

This will be for binary/compiled files for example executable files, they just need to converted to an ASCII file and left on the server somewhere. The bin2asc example above should satisfy the last part of my problem what I need as well is a way to get the file from another server (download it to my server for conversion).
You can write out the contents of basically any file grabbed with URLopen like so :

$new_file_path= 'logo.gif';  
$results = URLopen('https://www.experts-exchange.com/images/logo.gif');

$handle=fopen($new_file_path,"wb") or die("Error opening file : " . $new_file_path);
fwrite($handle, $results);
fclose($handle);

Are we getting closer ?
Avatar of xSinbad

ASKER

This is the sort of stuff I am after!

But be gentle with me I dont really know much about php, when runing the urlopen I get this error;

Fatal error: Call to undefined function URLopen() in D:\Dev\index.php on line 11

Do I need to use includes or lid refs or anything like that for php?

Using this code;

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 
<?php
$new_file_path= 'logo.gif';  
$results = URLopen('https://www.experts-exchange.com/images/logo.gif');

$handle=fopen($new_file_path,"wb") or die("Error opening file : " . $new_file_path);
fwrite($handle, $results);
fclose($handle);
?>
</body>
</html>
Avatar of xSinbad

ASKER

Ok I found this and it seems to help, sort of.


function URLopen($url)
{
       // Fake the browser type
       ini_set('user_agent','MSIE 4\.0b2;');

       $dh = fopen("$url",'r');
       $result = fread($dh,8192);                                                                                                                          
       return $result;
}
Avatar of xSinbad

ASKER

This creates the logo.gif file but it is  too small and doesnt open.
What do you mean "it is too small and doesn't open" ?

The 2 parameters you need to pass are :

1. the full name of the file you want (eg. 'https://www.experts-exchange.com/images/logo.gif'   or  'c:\windows\my_documents\powerpoint.pps'  etc. etc.)
2. the new name of the file you'd like to dump the contents into.  

That make sense ?
Avatar of xSinbad

ASKER

It too small, the file created is 3k in size the original (https://www.experts-exchange.com/images/logo.gif) is 19K and the gif file cannot be viewed once downloaded.
Replace the URLopen function with this :

-------------------------------------------------------------------------------------
function URLopen($url)
{
     // Fake the browser type
     ini_set('user_agent','MSIE 4\.0b2;');
     $dh = fopen("$url",'rb');
     $f = fopen($url,"rb");
     while ($r = fread($f,8192)) {
          $result .= $r;
     }
     fclose($f);
     return $result;
}
-------------------------------------------------------------------------------------
Avatar of xSinbad

ASKER

Same problem the logo.gif is even smaller in size now.
Weird ... This script is working for me ... Is this what you have ?

-------------------------------------------------------------------------------------
<?php

function URLopen($url)
{
     // Fake the browser type
     ini_set('user_agent','MSIE 4\.0b2;');
     $dh = fopen("$url",'rb');
     $f = fopen($url,"rb");
     while ($r = fread($f,8192)) {
          $result .= $r;
     }
     fclose($f);
     return $result;
}

// convert an input string into it's binary equivalent.
function asc2bin($inputString, $byteLength=8)
{
    $binaryOutput = '';
    $strSize = strlen($inputString);

    for($x=0; $x<$strSize; $x++)
    {
        $charBin = decbin(ord($inputString{$x}));
        $charBin = str_pad($charBin, $byteLength, '0', STR_PAD_LEFT);
        $binaryOutput .= $charBin;
    }

    return $binaryOutput;
}

// convert a binary representation of a string back into it's original form.
function bin2asc($binaryInput, $byteLength=8)
{
    if (strlen($binaryInput) % $byteLength)
    {
        return false;
    }
   
    // why run strlen() so many times in a loop? Use of constants = speed increase.
    $strSize = strlen($binaryInput);
    $origStr = '';

    // jump between bytes.
    for($x=0; $x<$strSize; $x += $byteLength)
    {
        // extract character's binary code
        $charBinary = substr($binaryInput, $x, $byteLength);
        $origStr .= chr(bindec($charBinary)); // conversion to ASCII.
    }
    return $origStr;
}

$inputString = "An input string, complete with punctuation!";

$binOut = asc2bin($inputString);
printf("Input String: %s\nBinary Version: %s\n",$inputString, $binOut);

$ascOut = bin2asc($binOut);
printf("Input Binary: %s\nOutput ASCII: %s\n",$binOut, $ascOut);

$filename = 'olympics.jpg';  // d:\web\rmck.com\2004.01.01\test\
$results = URLopen('http://www.athens2004.com/Images/Sport%20Gallery/Table%20Tennis/21%20August%202004/51086745IW011_TABw_sglsm.jpg');

$handle=fopen($filename,"wb") or die("Error opening file");
fwrite($handle, $results);
fclose($handle);

?>
-------------------------------------------------------------------------------------
Avatar of xSinbad

ASKER

I really dont know much about PHP but this doesnt look right to me.

You have used a variable $dh and then done nothing with it or have I cocked up my interpretation of this code?

function URLopen($url)
{
     // Fake the browser type
     ini_set('user_agent','MSIE 4\.0b2;');
     $dh = fopen("$url",'rb');
     $f = fopen($url,"rb");
     while ($r = fread($f,8192)) {
          $result .= $r;
     }
     fclose($f);
     return $result;
}


---
oh yeah ... old code - sorry  ... you can remove that line ...

Apart from that , does the code work ?  I can use any remote file name as an argument and save it as any type of file.

Should word .... what do you get ?
Avatar of xSinbad

ASKER

No doesnt wok for me I still get an incoplete file downloaded, could this be a PHP setup issue?

I have tried it on my PC with the latest PHP installed and it only downloads 1k of the file requested and I have tried this here http://www.fordba.com/cgi-bin/index.php and doent likie that at all you will see if you try it, this server has PHP 4.3.3 does that matter??
Avatar of xSinbad

ASKER

I tried it on another server http://www.xu8.net/index.php  check out the results this time it is different again.
The reason it doesn't like  http://www.fordba.com/cgi-bin/index.php, is because the page doesn't exists.

I would try uploading an image to the same directory as your page (eg. logo.gif), and opening that instead ...

IE.  use $results = URLopen('logo.gif');

See if you get the same error messages.   The reason the file is only 1k, is that the script is writing the contents of $result (which is nothing - URLopen is returning false) to the file.

Avatar of xSinbad

ASKER

Yeh it does exist!

I have fixed it I changed the URLOpen function for this

function OpenURL($url)
{
       ini_set('allow_url_fopen','1');
    $fd = fopen ($url, "r") or die("Could not open file");
    $contents = "";
    while(!feof($fd))
    {
        $contents .= fgets($fd,100);
    }
    fclose($fd);
    return $contents;
}

And it works now on my PC.

All I need to do is get it woking on the web server becuase it keeps returning an error ;

http://www.fordba.com/cgi-bin/index.php

I am taking this up with the server admins at the moment.
Well, if you got it working on your PC, that's a start .... sounds like a permissions issue.

let me know
Avatar of xSinbad

ASKER

Grrrr...

I have given the thing all the bloody permissions under the sun (777) and it still wont work....!!!!

Gitcho thanks for you patience I know how frustrating it cab be trying to answer question when the person asking doesnt have a clue 8-)

Any clue what this means
Warning: fopen(blog.zip): failed to open stream: Permission denied in /home/httpd/vhosts/xu8.net/httpdocs/index.php on line 68
Error opening file
ASKER CERTIFIED SOLUTION
Avatar of Gitcho
Gitcho

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xSinbad

ASKER

Good thinking I will give that a try tonight.

Gitcho thanks for all your help, if you make a post in here https://www.experts-exchange.com/questions/21100804/Advice-on-a-server-app.html
 I would like to share those points with you as well.

Look out for some more post from me in the next couple of days I have lots of points to get rid of LOL.


Cheers
Marcus