Solved

Help on server app - Retrieve and convert file

Posted on 2004-08-20
22
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Curl Problem 10 66
Make an array show the subkey and put it in a query 2 50
SSL unsecure page mystery 17 38
Load string Array from file 23 36
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

740 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