Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
Shell scripts
* Readme
dict
edit_json (php)
encrypt/decrypt
pdoc
pftp - ftp a file to some place
php fork & IPC
php input cli
phplint
~/bin/inc/lib.php
10
found
auth
Name:
Category: Shell scripts
Body:
// note: this is kept around for comedy value, mostly #!/usr/bin/php <?php /* Copyright 2008 Maarten Spreij This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ if (in_array('--help', $argv)) { echo " This is pftp, a simple php script that FTPs a file to a server. pftp is Copyright 2008 Maarten Spreij, and is licensed under the GNU General Public License. Syntax: pftp [-flags] url file [website] Examples of use: pftp ftp://username:secritpass@ftp.example.com/myfolder/ a_local_file.txt Note that the slash right after 'ftp.example.com' is Not part of the remote path, so to specify the path from the server root, add another slash: pftp ftp://username:secritpass@ftp.example.com//var/www/site1/ foobar.html The file can be specified with any filepath that the shell will understand, ~/Desktop/file.txt, ../../foo.txt, /rootfile etc The -d flag turns on verbose/debugging mode. The -r flag is useful when this script runs as part of some process, and will return the website parameter with the filename tacked on, so you get a full url to the newly uploaded file. Note that the filename or path is not urlencoded. I wrote it because OS X's CLI FTP app seemed to not work very well in non-interactive mode. That's why.\n"; die(); } if (count($argv) < 3) die(" Usage: pftp [-flags] url file [website] url: ftp://user:pass@host/path/ flags: d(ebug), r(eturn) more info: pftp --help\n"); array_shift($argv); # -- Fish out the flaggy bits $flags = ''; while (substr($argv[0], 0, 1) == '-') { $flags .= substr(array_shift($argv), 1); } # -- Do something with flaggy bits (just debug mode for now) define('DEBUG', (bool) (strpos($flags, 'd') !== false)); if (DEBUG) { echo " Debug mode\n"; ini_set('display_errors', '1'); error_reporting(E_ALL); } $return = (bool) (strpos($flags, 'r') !== false); if (count($argv) < 2) die(" Insufficient infos: need url & path.\n"); # -- so we're left with.. $url = array_shift($argv); $file = array_shift($argv); # -- Figure out FTP connection infos -- $pattern = '#ftp://([^:]+):([^@]+)@([^/]+)/(.*)#'; if (preg_match($pattern, $url, $matches)) { list($url, $user, $pass, $host, $path) = $matches; if (DEBUG) echo " User: $user\n Pass: $pass\n Host: $host\n Remote path: $path\n"; }else{ die(" Malformed url, please try again.\n"); } # -- Can haz File plz? -- if (! file_exists($file)) die(" Error: Couldn't find the file you want to upload?\n"); if (DEBUG) echo " Local file: $file\n"; $remote_filename = basename($file); # -- Connect -- if ($conn = ftp_connect($host)) { if (DEBUG) echo " Connected to $host\n"; if ($result = ftp_login($conn, $user, $pass)) { if (DEBUG) echo " Login successful\n"; # Try to hop to the folder $path if ($path) { $hop = ftp_chdir($conn, $path); if (! $hop) { # Failed. maybe the last bit was really the new filename? if (DEBUG) echo " Server: Changing dir to '$path' failed..\n"; if (substr($path, -1) != '/') { # See, let's treat that as a file $remote_filename = basename($path); $old_path = $path; $path = substr($path, 0, strrpos($path, '/')); if ($path) { if (! ftp_chdir($conn, $path)) { ftp_close($conn); die(" Error: couldn't changedir to remote path '$path' (nor '$old_path').\n"); }else{ if (DEBUG) echo " Server: Changed dir to '$path'\n"; } } }else{ ftp_close($conn); die(" Error: couldn't change dir to remote path '$path'.\n"); } }else{ if (DEBUG) echo " Server: Changed dir to '$path'\n"; } } if (ftp_put($conn, $remote_filename, $file, FTP_BINARY)) { if (DEBUG) echo " File upload successful!\n"; if ($return) echo join('', $argv).$remote_filename."\n"; }else{ echo " Error: problem uploading file - something, like, went wrong. Yeah.\n"; } ftp_close($conn); }else{ die(" FTP error: Login failed.\n"); } }else{ die(" FTP error: Connection failed.\n"); } ?>
Footer