This topic is locked

Copyinf Files from Remote IP

3/18/2024 4:50:56 PM
PHPRunner General questions
J
JoeB7774 author

Hello. I am trying to copy a files from a remote host to my localhost using a button. I have the Server Code as follows but it always seems to fail (I have changed the IP to all zeros). The remote IP does not need authentication. I have allso changed the source directory to "http://00.00.00.00/Lists/MATERIAL.txt" and that also fails. I was just wondering if anoyone can see what I might be doing wrong? I have also tried fopen, file_get_contents but neither seems to work (doesn't produce an error). I have verified that php.ini on localhost has allow_url_fopen is On.

Thanks in advance. Joe.

$source = '\00.00.00.00\Lists\MATERIAL.txt';
$destination = $_SERVER['DOCUMENT_ROOT'] . '/files/testfile.txt';

if(!copy($source, $destination)){
echo "Copy Failed";
}
else{
echo "Copy Worked";
}

C
cristi 3/19/2024

The copy method doesn't always works: behind a firewall, large files corruption, etc
That's why it is better to use ftp_put or ftp_get when you work with remote files even if it seems more complicated - you will get the habit of using the "right way".
Also make sure that you use ftp_pasv() if you are behind a firewall.
The directory where you download the files needs to have write permisions.

Better yet use a free library from the web - leave the hassle of writing code for creating ftp connections and downloads to others who were dedicated on this and just concentrate on your application.
An example is here: https://www.phpclasses.org/package/3174-PHP-Client-to-access-FTP-servers-in-pure-PHP-code.html#information

or this one:

https://github.com/Nicolab/php-ftp-client

admin 3/19/2024

You cannot "copy" files from an IP address. You can download files from a remote computer/server via HTTP or FTP and then save them somethere on a local drive. So, instead of trying copy() function, download and them move them to the required folder.