PHP scripts to upload files to an FTP

Permalink
Hi everyone,

Some help would be much appreciated... We are trying to build the function into our website for users to upload files which get put into our own ftp server. I have this working on our standard on pemise apache web sever, but would like the functionality built into our website for a specific project...

I have tried using a single page form calling a controller which runs the php upload script but they won't work!.. The php forgets the file have been selected so returns a "file cannot be empty" error... perhaps because the file is never gettign pout into the temp folder or something?...

below is by scripts....
FORM:

<form method="post"
action="<?php echo $this->action('search_user')?>">
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

PHP:


<?php
class ServicesMyProfileController extends Controller {
public function search_user() {
print 'I am running!';
}
}

$ftp_server = "***.***.***.**";
$ftp_user_name = "*****";
$ftp_user_pass = "*****";
$remote_dir = "/upload";


// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

//default values
$file_url = "";

if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);

//check if directory exists and if not then create it
if(!@ftp_chdir($conn_id, $remote_dir)) {
//create diectory
ftp_mkdir($conn_id, $remote_dir);
//change directory
ftp_chdir($conn_id, $remote_dir);
}

$file = $_FILES["file"]["tmp_name"];
$remote_file = $_FILES["file"]["name"];

$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}

if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
?>

 
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
wchestnutt replied on at Permalink Reply
Thanks for the reply... Will this upload to my own specified FTP server? It doesn't have many details and is quite expensive.

I am very surprised and disappointed in Concrete there isn't a way to simply code it! If this was a standard website I would be able to do it in a couple of minutes!

Best regards,
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
What do you mean by "Will this upload to my own specified FTP server?". If you want to upload the file to your own mentioned path, then its not for you. It will upload the file as normal way into the "files" directory.
Regarding the disappointment:
Yes you are right, if you use standard php code to do this, its a matter of few minutes

But I can say one thing that in any MVC based framework it will take a decent amount of time to accomplish this.

Anyway you can use the C5 default form block to allow user to upload files. Also you can define a sets in file manager in which the files will be added.

Rony
wchestnutt replied on at Permalink Reply
Ah ok, so by my own specified FTP I mean that I have an FTP server here in the office (not the one used for C5) and I wan't the ability for people browsing my web page to upload a file and for it to go directly onto my office FTP server, as the uploaded files become part of an automated document management system and distributed out to mobile devices.

Kind regards, William.
JohntheFish replied on at Permalink Reply
JohntheFish
Front End File Uploader provides a popup to upload files to a fileset or fID, either by file field or by drag/drop.

If you want a straight forward form, you can use a file field in a form block.

Or you can simply assign users permissions to use the c5 File manager dialog.

All of the above upload files into the File manager. You then pull them into pages as images or otherwise embedded, or use a file download block to allow downloads of those files.

All the above is within concrete5 and involves no coding.

Perhaps you could clarify why you need to upload a file to a web server only to FTP it somewhere else. If you want to use it from the web server, you dont need the ftp. If you want to use it from the ftp server, you could ftp it straight there in the first place and skip moving the file via the web server as an intermediary.

Looking at your php code, for it to stand any chance of working the ftp code would need to be inside the controller action. At the moment you have a controller class containing the search_user() action that says 'I am running' and ends, then end the method and class and follow it with a bunch of orphaned code that gets run immediately after the class is declared, not when the action within the class is called by the form submission.