I'm trying to write a small script in PHP that will upload a file automatically via FTP.
I'm having a bit of trouble getting the script to work properly.. i've cut it's size down quite a bit to show here and it's only the essentials i'm going to show ya.
I keep getting an error saying that $upload != true and i'm unsure why. Also, when I try to change directories it's not working as expected. Again, i'm unsure why.
If anyone could help me out w/this I would appreciate it:
<?php
/*
* Defining Server address
* and, if applicable, port
* (21 by default), username,
* password, and screenshot
* to be uploaded and where
* it should be uploaded to
* along with what we should
* name that file.
*
*/
$serv = "ftp.site.com";
$user = "user";
$pass = "pass";
$screenshotLocation = "C:\\0CurrentDesktop001.png";
$whereTo = "/public_html/";
$filename = "currentDesktop.png";
/*
* Checking for screen shot
* to be uploaded to remote
* host.
* If it exists, we'll upload
* it. If not, The script
* will not finish executing.
*
*/
if(file_exists($screenshotLocation)) {
// connect to FTP server
$conn = ftp_connect($serv);
echo "<span style=color:blue><b>$screenshotLocation Exists. Connecting to $serv...</b></span><br />";
}
else { die("There is no file to upload!"); }
if($conn) {
echo "Successfully connected to <b>$serv</b><br />";
}
/*
* If all is well, the
* screenshot exists,
* and we're now connected
* to the remote host.
* Lets log in!
*
*/
$login = ftp_login($conn, $user, $pass);
if($login) {
echo "Successfully Logged in as <b>$user</b><br />";
}
/*
* Ok, we're now logged in if
* you supplied the right credentials
* that is! Lets check where we are
* on the remote host.
*
*/
$here = ftp_pwd($conn);
echo "Current Working Directory: <b>$here</b><br />";
echo "Changing Directory to: <b>$whereTo</b><br />";
/*
* changing directory to
* $whereTo
*
*/
$dir_change = ftp_chdir($conn, $whereTo);
if($dir_change) {
echo "Directory Changed.<br />Current Working Directory: <b>$here</b>";
}
/*
* Seems to me we should now be in
* the public_html folder on the
* remote host. So, w/o further ado,
* let us upload our screenshot!
*
*/
$upload = ftp_put($conn, "$screenshotLocation", "$filename", FTP_BINARY);
/*
* Ok, our screenshot should now be available
* on the remote host via the [url]WWW.[/url] Lets test
* to see whether or not our file was actually
* uploaded or not.
*
*/
if($upload) {
echo "<br />Upload: <span style=color:blue><b>Successful!</b></span>";
}
else {
ftp_quit($conn);
die("<br />Upload: <span style=color:red><b>Unsuccessful!</b></span>... Quitting.<br />LOGGED OUT");
}
/*
* If everything went smoothly, the screenshot
* should have been uploaded and you'll see a
* success message. Else, we quit the ftp session
* and you'll see an error message. Either way,
* we must quit our ftp session, and if execution
* made it this far, everything {should} be good
* to go. So lets quit this ftp session.
*
*/
ftp_quit($conn);
/*
*
* This concludes my attempt at an automated
* upload script via FTP. Execution time
* may be slow and what not, so the script
* might die due to execution time {fatal error}.
* I'm still working on this, and it's no where
* near done. ( Began: 9.27.03 )
*
* STATUS: Unfunctional. Connects to Server,
* However, does not upload file. Prints
* "/" as Current Working Directory.
* dir_change doesn't go to public_html.
*
*/
?>
Anybody familiar w/the ftp functions in php? I've never really played w/them until just the other day. Seems to me this script should work, but hey.. what do i know 😛