close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Automatically upload a file via FTP (for use with a scheduled CRON job)

Thread began 7/10/2017 8:32 am by Jared Lui | Last modified 7/14/2017 12:54 pm by Ray Borduin | 340 views | 12 replies |

Jared Lui

Automatically upload a file via FTP (for use with a scheduled CRON job)

GOAL: Using CRON, run a script at scheduled times. The script uploads the contents of a directory on the servers computer via FTP to another directory on another computer.

I have never used CRON before but setting up jobs in cPanel does not look that difficult. I have read dozens on posts (all 5-10 years old) on how to connect via FTP and put files. But none describe how to do this via cron.

From what I can gather, it would be simpler to create a script that does the connection/upload and just use CRON to run the file.

Here's what I have been able to write so far but am unsure how to turn this into a usable script file to run in CRON. I'm thinking a Php file?

Any help is appreciated.
---------------------------------------

#!/bin/bash
HOST=ftp.pentagon.base.com
USER=pentagon
PASSWORD=`echo cGFzc3dvcmQ= | base64 --decode`

ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd /public_html/uploads/base
mput *.xlsx
bye
EOF

Sign in to reply to this post

Ray BorduinWebAssist

I'd get it working on a .php page and you can debug and get it working when you access the page directly. Once you have that working just google how to set up a CRON job to run a PHP page and it should be a simple one liner. Some servers have slightly different syntax, but your hosting support can always help if push comes to shove. It is a common enough task that you should be able to find the answer online.

Sign in to reply to this post

Jared Lui

Thanks Ray,

After failing for 2 days with writing a .sh script I wen back to Php as was my intention (was talked into a "better" method by the other servers IT people who were no help at all).

Below is what I have so far and despite my comments I am confusing myself with the name/paths aparantly. Here's my latest error:

Warning: ftp_put(/uploads/partsbase/test.txt): failed to open stream: No such file or directory in /home/baileyhyd/public_html/partsbase.php on line 25
FTP upload of /uploads/partsbase/test.txt has failed!

Here's the link if you want to see it: http://www.baileyhyd.com/partsbase.php

---------------------------------------------------

<!--?php ini_set('display_errors',0); ?-->
<?php
$name = "test.txt";
$filename = "/uploads/partsbase/test.txt";

//-- Connection Settings
$ftp_server = "pentagonposts.partsbase.com"; // Address of FTP server.
$ftp_user_name = "pentagonposts"; // Username
$ftp_user_pass = 'password'; // Password
$destination_file = "test.txt"; //where you want to throw the file on the webserver (relative to your login dir)

$conn_id = ftp_connect($ftp_server) or die("<span style='color:#FF0000'><h2>Couldn't connect to $ftp_server</h2></span>"); // set up basic connection

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("<span style='color:#FF0000'><h2>You do not have access to this ftp server! /h2></span>"); // login with username and password, or give invalid user message

if ((!$conn_id) || (!$login_result)) { // check connection
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}

$upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY); // upload the file
if (!$upload) { // check upload status
echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
} else {
echo "<span style='color:#339900'><h2>Uploading $name Completed Successfully!</h2></span><br /><br />";
}
ftp_close($conn_id); // close the FTP stream
?>

Sign in to reply to this post

Ray BorduinWebAssist

Since you are working with local files you can't use an absolute path like"$filename = "/uploads/partsbase/test.txt"

You probably need a relative path like: $filename = "../../uploads/partsbase/test.txt"

Sign in to reply to this post

Jared Lui

Actually removing the leading / worked. Was able to send test.txt.

Now, what I need to do is make it do this *.xlsx becuase the name will change daily. It is named for each date automatically and saved to that folder.

I though that "mput" uploaded multiple files in the directory and that *.xlsx would work. I guess I was wrong because I get:
Fatal error: Call to undefined function ftp_mput() in /home/baileyhyd/public_html/partsbase.php on line 25

I need to upload all files in the uploads/partsbase directory (even though there will only ever be one) but because the name will change, I need to upload the contents of the dir rather than a specific file. Make sense?

Sign in to reply to this post

Ray BorduinWebAssist

You can use scandir() to return all of the files in that folder into an array and then loop through that array to put all of the files.

Sign in to reply to this post

Jared Lui

See private message

Sign in to reply to this post

Ray BorduinWebAssist

I think this will work:

php:
<?php

$foldername 
"uploads/partsbase/";

//-- Connection Settings
$ftp_server "pentagonposts.partsbase.com"// Address of FTP server.
$ftp_user_name "pentagonposts"// Username
$ftp_user_pass 'password'// Password
$destination_folder ""//where you want to throw the file on the webserver (relative to your login dir)

$conn_id ftp_connect($ftp_server) or die("<span style='color:#FF0000'><h2>Couldn't connect to $ftp_server</h2></span>"); // set up basic connection

$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass) or die("<span style='color:#FF0000'><h2>You do not have access to this ftp server! /h2></span>"); // login with username and password, or give invalid user message

if ((!$conn_id) || (!$login_result)) { // check connection
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo 
"Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}


$files array_slice(scandir($foldername), 2);
for (
$x=0$x<sizeof($files); $x++) {

  
$upload ftp_put($conn_id$destination_folder $files[$x], $foldername $files[$x], FTP_BINARY); // upload the files
  
if (!$upload) { // check upload status
    
echo "<span style='color:#FF0000'><h2>FTP upload of " $files[$x] . " has failed!</h2></span> <br />";
  } else {
    echo 
"<span style='color:#339900'><h2>Uploading " .$files[$x] . " Completed Successfully!</h2></span><br /><br />";
  }
    
}
ftp_close($conn_id); // close the FTP stream
?>
Sign in to reply to this post

Jared Lui

Wife said it worked!

Sign in to reply to this post

Jared Lui

The cron job is not sending errors anymore so am unable to track when the script is running.

So need to add a function to send an email with a date/time stamp of when it's run.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...