Solved

Help on server app - Retrieve and convert file

Posted on 2004-08-20
22
513 Views
Last Modified: 2008-02-01
See Q.  http://www.experts-exchange.com/Web/Web_Languages/Q_21100804.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
0
Comment
Question by:xSinbad
  • 12
  • 10
22 Comments
 
LVL 5

Expert Comment

by:Gitcho
ID: 11857228
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) ?
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11857238
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);
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11857624
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).
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 5

Expert Comment

by:Gitcho
ID: 11857751
You can write out the contents of basically any file grabbed with URLopen like so :

$new_file_path= 'logo.gif';  
$results = URLopen('http://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 ?
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11857865
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('http://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>
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11857882
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;
}
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11857891
This creates the logo.gif file but it is  too small and doesnt open.
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11857932
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. 'http://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 ?
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11858006
It too small, the file created is 3k in size the original (http://www.experts-exchange.com/images/logo.gif) is 19K and the gif file cannot be viewed once downloaded.
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11859422
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;
}
-------------------------------------------------------------------------------------
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11861640
Same problem the logo.gif is even smaller in size now.
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11861759
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);

?>
-------------------------------------------------------------------------------------
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11861765
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;
}


---
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11861777
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 ?
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11861829
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??
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11861881
I tried it on another server http://www.xu8.net/index.php  check out the results this time it is different again.
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11861999
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.

0
 
LVL 6

Author Comment

by:xSinbad
ID: 11862161
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.
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11862569
Well, if you got it working on your PC, that's a start .... sounds like a permissions issue.

let me know
0
 
LVL 6

Author Comment

by:xSinbad
ID: 11862746
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
0
 
LVL 5

Accepted Solution

by:
Gitcho earned 500 total points
ID: 11863017
I fought with this same problem years back, and it ended up being that I didn't have WRITE permissions on the DIRECTORY ...

If you can't change the permissions on the root directory, create a sub-directory (eg. "temp") & chmod 777 it ... then try writing to that directory instead.


$filename = 'temp/newfilename.zip';     // NOTE : Specify the new directory you created
$results = URLopen('somefile.zip');

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


0
 
LVL 6

Author Comment

by:xSinbad
ID: 11866143
Good thinking I will give that a try tonight.

Gitcho thanks for all your help, if you make a post in here http://www.experts-exchange.com/Web/Web_Languages/Q_21100804.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
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question