13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

464 Chapter 20 Using Network <strong>and</strong> Protocol Functions<br />

The function takes three parameters: an FTP connection (obtained from<br />

ftp_connect()), a username, <strong>and</strong> a password. It returns true if the user can be logged in<br />

<strong>and</strong> false if she can’t. Notice that we put an @ symbol at the start of the line to suppress<br />

errors.We did this because, if the user cannot be logged in, a <strong>PHP</strong> warning appears in<br />

the browser window.You can catch the error as we have done here by testing $result<br />

<strong>and</strong> supplying your own, more user-friendly error message.<br />

Notice that if the login attempt fails, you actually close the FTP connection by using<br />

ftp_quit().We discuss this function more later.<br />

Checking File Update Times<br />

Given that you are updating a local copy of a file, checking whether the file needs<br />

updating first is sensible because you don’t want to have to redownload a file, particularly<br />

a large one, if it’s up to date.This way, you can avoid unnecessary network traffic. Let’s<br />

look at the code that checks file update times.<br />

File times are the reason that you use the FTP functions rather than a much simpler<br />

call to a file function.The file functions can easily read <strong>and</strong>, in some cases, write files<br />

over network interfaces, but most of the status functions such as filemtime() do not<br />

work remotely.<br />

To begin deciding whether you need to download a file, you check that you have a<br />

local copy of the file by using the file_exists() function. If you don’t, obviously you<br />

need to download the file. If it does exist, you get the last modified time of the file by<br />

using the filemtime() function <strong>and</strong> store it in the $localtime variable. If it doesn’t<br />

exist, you set the $localtime variable to 0 so that it will be “older” than any possible<br />

remote file modification time:<br />

echo ‘Checking file time...’;<br />

if (file_exists($localfile))<br />

{<br />

$localtime = filemtime($localfile);<br />

echo ‘Local file last updated ‘;<br />

echo date(‘G:i j-M-Y’, $localtime);<br />

echo ‘’;<br />

}<br />

else<br />

$localtime=0;<br />

(You can read more about the file_exists() <strong>and</strong> filemtime() functions in Chapters 2,<br />

“Storing <strong>and</strong> Retrieving Data” <strong>and</strong> 19,“Interacting with the File System <strong>and</strong> the Server,”<br />

respectively.)<br />

After you have sorted out the local time, you need to get the modification time of<br />

the remote file.You can get this time by using the ftp_mdtm() function:<br />

$remotetime = ftp_mdtm($conn, $remotefile);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!