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.

Backing Up or Mirroring a File<br />

461<br />

Listing 20.4<br />

Continued<br />

else<br />

$localtime=0;<br />

$remotetime = ftp_mdtm($conn, $remotefile);<br />

if (!($remotetime >= 0))<br />

{<br />

// This doesn't mean the file's not there, server may not support mod<br />

time<br />

echo 'Can\'t access remote file time.';<br />

$remotetime=$localtime+1; // make sure of an update<br />

}<br />

else<br />

{<br />

echo 'Remote file last updated ';<br />

echo date('G:i j-M-Y', $remotetime);<br />

echo '';<br />

}<br />

if (!($remotetime > $localtime))<br />

{<br />

echo 'Local copy is up to date.';<br />

exit;<br />

}<br />

// download file<br />

echo 'Getting file from server...';<br />

$fp = fopen ($localfile, 'w');<br />

if (!$success = ftp_fget($conn, $fp, $remotefile, FTP_BINARY))<br />

{<br />

echo 'Error: Could not download file';<br />

ftp_quit($conn);<br />

exit;<br />

}<br />

fclose($fp);<br />

echo 'File downloaded successfully';<br />

// close connection to host<br />

ftp_quit($conn);<br />

?><br />

<br />

<br />

The output from running this script on one occasion is shown in Figure 20.4.

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

Saved successfully!

Ooh no, something went wrong!