Hi all,

I have to find out to write a function that can serve PDF documents (invoices) in the browser window.

My challenge is that the PDF fiels is on another server, which I do have access to.

Can I somehow just create a link on the site, and then get the pdf from the different server to appear, by using fgets?

I know fgets can be used for cvs data - but I need to show the entire document/transfer it from one server to another.

Any ideas is appreciated, as I need to create this function asap :-)

Best regards, Klemme

Can I somehow just create a link on the site, and then get the pdf from the different server to appear, by using fgets?

Basically yes, assuming fgets is allowed to open it. You can always try other methods, like cUrl. Be sure to serve the correct header for the file.

So, now its changed so that I should get the files via FTP.

I cant seem to connect, does this look correct to you?

$conn = ftp_connect("ftp://xx.xxx.xxx.xxx/MyFTPFolder") or die("Could not connect");
ftp_login($conn,"XXX\\Username","Password");

ftp_fget($conn,$target,$source,FTP_ASCII);

ftp_close($conn);

Which doesnt connect - Should be correct credentials. Am I doing something wrong here?

/Klemme

Connect to the server, then chdir to the correct folder.

$ftp_server = "ftp://11.111.111.111";
$ftp_dir    = "/UpLoad";
$ftp_user   = "QQQ\namr";
$ftp_pass   = "ACACACACACAC";

// Opret forbindelse til FTP serveren
$conn_id = ftp_connect($ftp_server) or die("Kunne ikke forbinde til $ftp_server\n"); 

// Log ind på serveren
if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Forbundet som: $ftp_user $ftp_server\n";
} else {
    echo "Kunne ikke forbinde som: $ftp_user\n";
}

// Skift til relevant folder
if (ftp_chdir($conn_id, $ftp_dir)) {
echo "Der er skiftet til folder: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Der kunne ikke skiftes til folder\n";
}


// close the connection
ftp_close($conn_id);

I get the error echoet out from the ftp_connect function.

Any errors you guys can see here?

/Klemme

Can you connect manually?

I cant - Guess I need to be granted access..

So, does that mean my IP should be added?

Maybe. Contact the administrator of that server.

Done, It is allready added - Any idea on how to debug and figure out what is done incorrect?

I'd try with a regular ftp client first, they usually have a lot of logging in the process. Did you remove the ftp:// from the hostname?

Nope.

When ftp:// is there I get a php warning and the die message.

Without - No warning, just the die message from the connection error.

Does that point in any direction?

Can you connect with an ftp client now?

Nope, I tried connecting via FileZilla, but I get connection lost by timeout..

A bit lost here im afraid!

Firewall, router?

Back again,

I try to connect using this script:

$host = gethostbyaddr("66.666.666.666");            
        //$server   = 'ftp://'.$host;
        $server     = $host;                                
        $user       = "ABC\Admin";                  
        $pass       = "xxx";                        
        $file       = "Filename.pdf";                        
        $local_file = 'downloads/test.pdf'; 
        $ftp_dir    = "/ftp_files";

        $sessid     = ftp_connect($server);                 
        $login      = ftp_login($sessid, $user, $pass); 

        if ( (!$sessid) || (!$login) ) 
        {
            echo "Der blev ikke oprettet forbindelse: Tjek ftp host, username og password";
        }

        if ( ftp_chdir($sessid, $ftp_dir) ) 
        {
            echo "Der er skiftet til folder: " . ftp_pwd($sessid) . "\n";
        } 
        else 
        { 
            echo "Der kunne ikke skiftes til folder\n";
        }   


        if ( ftp_get($sessid, $local_file, $file, FTP_BINARY) )
        {
            echo "Overførslen er udført til: $local_file\n";
        } 
        else 
        {
            echo "Der var et problem ved ftp_get funktionen\n";
        }

        // Luk FTP forbindelse
        ftp_close($sessid);

The above gives me theese warnings:

   Warning: gethostbyaddr() [function.gethostbyaddr]: Address is not a valid IPv4 or IPv6 address in /ddd/ddd/ddd.com/ddd/ddd/index.php on line 108

Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known in /ddd/ddd/ddd.com/ddd/ddd/index.php on line 119

Warning: ftp_login() expects parameter 1 to be resource, boolean given in /ddd/ddd/ddd.com/ddd/ddd/index.php on line 120
Der blev ikke oprettet forbindelse: Tjek ftp host, username og password
Warning: ftp_chdir() expects parameter 1 to be resource, boolean given in /ddd/ddd/ddd.com/ddd/ddd/index.php on line 133
Der kunne ikke skiftes til folder 
Warning: ftp_get() expects parameter 1 to be resource, boolean given in /ddd/ddd/ddd.com/ddd/ddd/index.php on line 146
Der var et problem ved ftp_get funktionen 
Warning: ftp_close() expects parameter 1 to be resource, boolean given in /ddd/ddd/ddd.com/ddd/ddd/index.php on line 157

The remote server I am trying to connect to, is set up as an FTP server. I have read on php.net, that I shouldnt put anything else in the ftp_connect function, except the ip or the hostname.

Any idea how I can get better error information outputted on my screen?

AND I CAN ACCESS IT FROM THE BROWSER JUST FINE.....

It is possible that gethostbyaddr returns more than one result. See this comment.

When I echo it out, I get just the hostname, so it looks fine.

Maybe the ftp_connect, only accepts hostnames, starting with ftp.hostname.com rather than just the IP address. Any idea if that is the case?

Im starting to wonder if the IIS server is set up incorrect. But if that is the case, then I shouldnt be able to go to the wanted url via the browser, or should I?

/Klemme

Was wondering if there is a PHP function, which returns the ftp.name of a host, by feeding the IP adress to it?

This returns the name(s) associated with the IP address. Note that there can be more hosts returned, as an IP address can be shared by multiple domains.

http://php.net/gethostbyaddr

Could it be an option to use Curl for this perhaps?

havent much experience in this, so please bear with me here:

$url    = 'http://www.domain.dk/Microsoft_sporgeskema.pdf';
        $path   = 'http://dev.sitename.blah.dk/test_site/downloads/test.pdf';

        $fp     = fopen($path, 'w');

        $ch     = curl_init($url);
        curl_setopt($ch, CURLOPT_FILE, $fp);

        $data   = curl_exec($ch);

        curl_close($ch);
        fclose($fp);

This works directly from my localhost - Putting it live, meaning on test server - I get this:

Warning: fopen(http://dev.sitename.blah.dk/test_site/downloads/test.pdf) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in http://blah.index.php on line 152

/Klemme

That's because you use an URL, use a relative/absolute path for fopen instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.