Wednesday, May 5, 2010

File Uploading

// Where the file is going to be placed
$target_path = "/tmp/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['itemUpload']['name']);

if(move_uploaded_file($_FILES['itemUpload']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['itemUpload']['name']).
    " has been uploaded
\n";
} else{
    echo "There was an error uploading the file, please try again!";
}

$localFile = "/tmp/".basename( $_FILES['itemUpload']['name']);

$remoteFile = "/var/www/push/".basename( $_FILES['itemUpload']['name']);

$scp_connection = ssh2_connect('remotehost',22);
ssh2_auth_password($scp_connection, 'username', 'password');

ssh2_scp_send($scp_connection, $localFile, $remoteFile , 0664);

Upload a file via HTTP, then SCP it to another server.  Been using the hell out of this so I can "sanitize" what files get uploaded to the production server (and force them to push it to Dev first).

No comments:

Post a Comment