/[corp_html]/back/phormation/file_upload.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /back/phormation/file_upload.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Fri Jan 26 17:53:58 2001 UTC (23 years, 4 months ago) by dpavlin
Branch point for: MAIN, dbp
Initial revision

1 <?
2 /*
3 * Phormation
4 * - A library of PHP code to make development of database-driven
5 * html forms easy and quick
6 *
7 * Copyright (C) 2000 Jason D. Hildebrand
8 * PeaceWorks Computer Consulting
9 *
10 * jason@peaceworks.ca
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
25 */
26
27
28
29 // if "pg_process_hook" is specified as the process_hook parameter for a
30 // file upload widget, this function will be called when a new file is
31 // uploaded
32
33 // processparm [in] - is a user-defined parameter which can be specified in the type array
34 // tmpfilename [in] - the temporary filepath/name of the uploaded file (stored on the server)
35 // filename [in] - the original name of the uploaded file
36 // targetdir [in] - the target directory
37
38 // the function must return a unique filename (without path). Phormation will
39 // prepend the target directory to this filename and then move the uploaded
40 // file to this destination
41
42 function default_image_process_hook( $processparm, $tmpfilename, $filename, $targetdir )
43 {
44 // first generate the unique filename
45 $filename = default_process_hook( $processparm, $tmpfilename, $filename, $targetdir );
46
47 // now try to figure out what kind of image was uploaded, so that we can
48 // add an extension to the filename
49 $ext = determine_image_type( $tmpfilename );
50 $filename .= "." . $ext;
51
52 return( $filename );
53 }
54
55 function default_process_hook( $processparm, $tmpfilename, $filename, $targetdir )
56 {
57 global $DB;
58 if( $DB == "mysql" ) {
59 $filename = mysql_process_hook( $processparm, $tmpfilename, $filename, $targetdir );
60 } else if( $DB == "pgsql" ) {
61 $filename = pgsql_process_hook( $processparm, $tmpfilename, $filename, $targetdir );
62 } else {
63 make_error( "DB type is not valid." );
64 }
65 return( $filename );
66 }
67
68 function pgsql_process_hook( $processparm, $tmpfilename, $filename, $targetdir )
69 {
70 global $conn; // connection to a database
71
72 // this hook expects the name of a database 'sequence' to be given in the
73 // $processparm parameter
74
75 $query = "select nextval('$processparm')";
76 $result = false;
77 for( $i = 0; !$result && $i < 5; $i++ ) {
78 $result = dbi_exec( $conn, $query );
79 }
80 $array = dbi_fetch_array( $result, 0 );
81 $num = $array[0];
82 $filename = 'file' . $num;
83 return( $filename );
84 }
85
86 function mysql_process_hook( $processparm, $tmpfilename, $filename, $targetdir )
87 {
88 global $conn; // connection to a database
89
90 // this hook expects the name of a database table to be given in the
91 // $processparm parameter, which will be used as a counter to generate
92 // unique filenames
93
94 // lock the counter table
95 dbi_exec( $conn, "lock tables $processparm write" );
96
97 // get the value of the counter
98 $result = dbi_exec( $conn, "select num from $processparm where id='cntr'" );
99
100 if( dbi_numrows( $result ) == 0 ) {
101 // if the counter doesn't yet exist in the table, use the value 0 for
102 // this call, and insert 1 into the table (for the next time)
103 $num = 0;
104 $filename = 'file' . $num;
105 dbi_exec( $conn, "insert into $processparm (id,num) values ('cntr', 1 )" );
106 } else {
107 // otherwise use the current value, and increment the value
108 $array = dbi_fetch_array( $result, 0 );
109 $num = $array[0];
110 $filename = 'file' . $num;
111 dbi_exec( $conn, "update $processparm set num = num + 1 where id='cntr'" );
112 }
113
114 // release all table locks
115 dbi_exec( $conn, "unlock tables" );
116 return( $filename );
117 }
118
119 function determine_image_type( $filename )
120 {
121 // thanks to H. Peter Anvin <hpa@zytor.com> (author of magicfilter) for the magic!
122 $fd = fopen( $filename, "r" );
123 if( $fd ) {
124 $string = fread( $fd, 20 );
125 fclose( $fd );
126
127 $str2 = substr( $string, 6 );
128
129 if( strncmp( $string, "GIF89a", 6 ) == 0 || strncmp( $string, "GIF87a", 6 ) == 0 ) {
130 $ext = "gif";
131 } else if(
132 ( strncmp( $string, "\377\330\377\340", 4 ) == 0 && strncmp( $str2, "JFIF\0", 5 ) == 0 )
133 || ( strncmp( $string, "\377\330\377\341", 4 ) == 0 && strncmp( $str2, "Exif\0", 5 ) == 0 ) ) {
134 $ext = "jpg";
135 } else if( strncmp( $string, "\x89PNG", 4 ) == 0 ) {
136 $ext = "png";
137 } else if( strncmp( $string, "MM\0\x2a", 4 ) == 0 || strncmp( $string, "II\x2a\0", 4 ) == 0 ) {
138 $ext = "tiff";
139 }
140 }
141 return( $ext );
142 }
143
144
145 ?>

  ViewVC Help
Powered by ViewVC 1.1.26