120 lines
13 KiB
HTML
120 lines
13 KiB
HTML
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Step 3: Uploading the image file to a server</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch34.html" title="Chapter 34. Showing SPAM statistics"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Step 3: Uploading the image file to a server</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 34. Showing SPAM statistics</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Step 3: Uploading the image file to a server"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2620804"></a>Step 3: Uploading the image file to a server</h2></div></div></div>
|
|||
|
|
|||
|
<p>This class assumes that PHP has been built with support for FTP. Since the script
|
|||
|
is straightforward we do not discuss it any more in details other than noting that
|
|||
|
we use the default system logger to store information that we have uploaded the file
|
|||
|
successfully. This is done via the PHP method <span class="command"><strong>syslog()</strong></span>.</p>
|
|||
|
<p>
|
|||
|
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
|||
|
2
|
|||
|
3
|
|||
|
4
|
|||
|
5
|
|||
|
6
|
|||
|
7
|
|||
|
8
|
|||
|
9
|
|||
|
10
|
|||
|
11
|
|||
|
12
|
|||
|
13
|
|||
|
14
|
|||
|
15
|
|||
|
16
|
|||
|
17
|
|||
|
18
|
|||
|
19
|
|||
|
20
|
|||
|
21
|
|||
|
22
|
|||
|
23
|
|||
|
24
|
|||
|
25
|
|||
|
26
|
|||
|
27
|
|||
|
28
|
|||
|
29
|
|||
|
30
|
|||
|
31
|
|||
|
32
|
|||
|
33
|
|||
|
34
|
|||
|
35
|
|||
|
36
|
|||
|
37
|
|||
|
38
|
|||
|
39
|
|||
|
40
|
|||
|
41
|
|||
|
42
|
|||
|
43
|
|||
|
44
|
|||
|
45
|
|||
|
46
|
|||
|
47
|
|||
|
48
|
|||
|
49
|
|||
|
50
|
|||
|
51
|
|||
|
52
|
|||
|
53
|
|||
|
54
|
|||
|
55
|
|||
|
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
|||
|
</span><span class="hl-comment">/*</span><span class="hl-comment">*
|
|||
|
* Class FTPUploader
|
|||
|
</span><span class="hl-comment">*/</span><span class="hl-code">
|
|||
|
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">FTPUploader</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
|||
|
</span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-var">$iserver</span><span class="hl-code">=</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-var">$iuid</span><span class="hl-code">=</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$ipwd</span><span class="hl-code">=</span><span class="hl-quotes">'</span><span class="hl-quotes">'</span><span class="hl-code">;
|
|||
|
</span><span class="hl-comment">/*</span><span class="hl-comment">*
|
|||
|
* Creat new instance of the FTP uploader class
|
|||
|
*
|
|||
|
*</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">$aServer The URI for the server
|
|||
|
*</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">$aUID The ftp user id
|
|||
|
*</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">$aPWD The ftp user password
|
|||
|
*</span><span class="hl-inlinedoc"> @return </span><span class="hl-comment">FTPUploader
|
|||
|
</span><span class="hl-comment">*/</span><span class="hl-code">
|
|||
|
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">__construct</span><span class="hl-brackets">(</span><span class="hl-var">$aServer</span><span class="hl-code">,</span><span class="hl-var">$aUID</span><span class="hl-code">,</span><span class="hl-var">$aPWD</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$this</span><span class="hl-code">-></span><span class="hl-identifier">iserver</span><span class="hl-code"> = </span><span class="hl-var">$aServer</span><span class="hl-code">;
|
|||
|
</span><span class="hl-var">$this</span><span class="hl-code">-></span><span class="hl-identifier">iuid</span><span class="hl-code"> = </span><span class="hl-var">$aUID</span><span class="hl-code">;
|
|||
|
</span><span class="hl-var">$this</span><span class="hl-code">-></span><span class="hl-identifier">ipwd</span><span class="hl-code"> = </span><span class="hl-var">$aPWD</span><span class="hl-code">;
|
|||
|
</span><span class="hl-brackets">}</span><span class="hl-code">
|
|||
|
</span><span class="hl-comment">/*</span><span class="hl-comment">*
|
|||
|
* Upload the specified file to the given directory on the server
|
|||
|
*
|
|||
|
*</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">$aFile Name and path of file to uploads
|
|||
|
*</span><span class="hl-inlinedoc"> @param </span><span class="hl-comment">$aUploadDir The directory on the server where the file should be stored
|
|||
|
</span><span class="hl-comment">*/</span><span class="hl-code">
|
|||
|
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">Upload</span><span class="hl-brackets">(</span><span class="hl-var">$aFile</span><span class="hl-code">,</span><span class="hl-var">$aUploadDir</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$conn_id</span><span class="hl-code"> = @</span><span class="hl-identifier">ftp_connect</span><span class="hl-brackets">(</span><span class="hl-var">$this</span><span class="hl-code">-></span><span class="hl-identifier">iserver</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> !</span><span class="hl-var">$conn_id</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
|||
|
</span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">Raise</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">FTP connection failed.</span><span class="hl-special">\n</span><span class="hl-string">Attempted to connect to </span><span class="hl-var">{$this->iserver} for user {$this->iuid}</span><span class="hl-string">.</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-brackets">}</span><span class="hl-code">
|
|||
|
|
|||
|
</span><span class="hl-var">$login_result</span><span class="hl-code"> = </span><span class="hl-identifier">ftp_login</span><span class="hl-brackets">(</span><span class="hl-var">$conn_id</span><span class="hl-code">, </span><span class="hl-var">$this</span><span class="hl-code">-></span><span class="hl-identifier">iuid</span><span class="hl-code">, </span><span class="hl-var">$this</span><span class="hl-code">-></span><span class="hl-identifier">ipwd</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-var">$conn_id</span><span class="hl-brackets">)</span><span class="hl-code"> || </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-var">$login_result</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
|||
|
</span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">Raise</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">FTP login has failed.</span><span class="hl-special">\n</span><span class="hl-string">Attempted to connect to </span><span class="hl-var">{$this->iserver} for user {$this->iuid}</span><span class="hl-string">.</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-brackets">}</span><span class="hl-code">
|
|||
|
|
|||
|
</span><span class="hl-identifier">syslog</span><span class="hl-brackets">(</span><span class="hl-identifier">LOG_INFO</span><span class="hl-code">,</span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">GetTitle</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">"</span><span class="hl-string">Connected to </span><span class="hl-var">{$this->iserver}</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-comment">//</span><span class="hl-comment"> Delete potential old file</span><span class="hl-comment"></span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$ftp_file</span><span class="hl-code"> = </span><span class="hl-var">$aUploadDir</span><span class="hl-code">.</span><span class="hl-identifier">basename</span><span class="hl-brackets">(</span><span class="hl-var">$aFile</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-var">$res</span><span class="hl-code"> = @</span><span class="hl-identifier">ftp_delete</span><span class="hl-brackets">(</span><span class="hl-var">$conn_id</span><span class="hl-code">,</span><span class="hl-var">$ftp_file</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
</span><span class="hl-comment">//</span><span class="hl-comment"> Upload new image</span><span class="hl-comment"></span><span class="hl-code">
|
|||
|
</span><span class="hl-var">$upload</span><span class="hl-code"> = </span><span class="hl-identifier">ftp_put</span><span class="hl-brackets">(</span><span class="hl-var">$conn_id</span><span class="hl-code">, </span><span class="hl-var">$ftp_file</span><span class="hl-code">, </span><span class="hl-var">$aFile</span><span class="hl-code">, </span><span class="hl-identifier">FTP_BINARY</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-var">$upload</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
|||
|
</span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">Raise</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">FTP upload of image failed.</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-brackets">}</span><span class="hl-code">
|
|||
|
|
|||
|
</span><span class="hl-identifier">syslog</span><span class="hl-brackets">(</span><span class="hl-identifier">LOG_INFO</span><span class="hl-code">,</span><span class="hl-identifier">JpGraphError</span><span class="hl-code">::</span><span class="hl-identifier">GetTitle</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">"</span><span class="hl-string">Succesfully uploaded </span><span class="hl-var">$aFile</span><span class="hl-string"> to </span><span class="hl-var">{$this->iserver}</span><span class="hl-string">.</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
|
|||
|
@</span><span class="hl-identifier">ftp_close</span><span class="hl-brackets">(</span><span class="hl-var">$conn_id</span><span class="hl-brackets">)</span><span class="hl-code">;
|
|||
|
</span><span class="hl-brackets">}</span><span class="hl-code">
|
|||
|
</span><span class="hl-brackets">}</span><span class="hl-code">
|
|||
|
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div><p>
|
|||
|
</p>
|
|||
|
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch34.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>
|