Help on server app - Retrieve and convert file

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
LVL 6
xSinbadAsked:
Who is Participating?
 
GitchoConnect With a Mentor Commented:
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
 
GitchoCommented:
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
 
GitchoCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
xSinbadAuthor Commented:
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
 
GitchoCommented:
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
 
xSinbadAuthor Commented:
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
 
xSinbadAuthor Commented:
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
 
xSinbadAuthor Commented:
This creates the logo.gif file but it is  too small and doesnt open.
0
 
GitchoCommented:
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
 
xSinbadAuthor Commented:
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
 
GitchoCommented:
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
 
xSinbadAuthor Commented:
Same problem the logo.gif is even smaller in size now.
0
 
GitchoCommented:
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
 
xSinbadAuthor Commented:
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
 
GitchoCommented:
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
 
xSinbadAuthor Commented:
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
 
xSinbadAuthor Commented:
I tried it on another server http://www.xu8.net/index.php  check out the results this time it is different again.
0
 
GitchoCommented:
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
 
xSinbadAuthor Commented:
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
 
GitchoCommented:
Well, if you got it working on your PC, that's a start .... sounds like a permissions issue.

let me know
0
 
xSinbadAuthor Commented:
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
 
xSinbadAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.