PHP Processing File Uploads

Save image in database table. Use REPLACE to kick out any old image with same name. my dbh = Cookbook::connect ; dbh-do REPLACE INTO image name,type,data VALUES?,?,?, undef, image_name, mime_type, data; dbh-disconnect ; } } print end_html ; exit 0;

18.9.5 PHP

To writ e an upload form in PHP, include a file field. I f you wish, you m ay also include a hidden field preceding t he file field t hat has a nam e of MAX_FILE_SIZE and a value of t he largest file size youre willing t o accept : form method=POST enctype=multipartform-data action=?php print get_self_path ; ? input type=hidden name=MAX_FILE_SIZE value=4000000 Image name:br input type=text name=image_name size=60 br Image file:br input type=file name=upload_file size=60 br br input type=submit name=choice value=Submit form Be aware t hat MAX_FILE_SIZE is advisory only, because it can be subvert ed easily. To specify a value t hat cannot be exceeded, use t he upload_max_filesize configurat ion set t ing in t he PHP init ializat ion file. There is also a file_uploads set t ing t hat cont rols whet her or not file uploads are allowed at all. When t he user subm it s t he form , file upload inform at ion m ay be obt ained as follows: • As of PHP 4.1, file upload inform at ion from POST request s is placed in a separat e array, _FILES , which has one ent ry for each uploaded file. Each ent ry is it self an array wit h four elem ent s. For exam ple, if a form has a file field nam ed upload_file and t he user subm it s a file, inform at ion about it is available in t he following variables: • _FILES[upload_file][name] original filename on client host • _FILES[upload_file][tmp_name] temporary filename on server host • _FILES[upload_file][size] file size, in bytes _FILES[upload_file][type] file MIME type Be careful here, because t here m ay be an ent ry for an upload field even if t he user subm it t ed no file. I n t his case, t he tmp_name value will be t he em pt y st ring or t he st r ing none . • Earlier PHP 4 releases have file upload inform at ion in a separat e array, HTTP_POST_FILES , which has ent ries t hat are st ruct ured like t hose in _FILES . For a file field nam ed upload_file , inform at ion about it is available in t he following variables: • HTTP_POST_FILES[upload_file][name] original filename on client host • HTTP_POST_FILES[upload_file][tmp_name] temporary filename on server host • HTTP_POST_FILES[upload_file][size] file size, in bytes HTTP_POST_FILES[upload_file][type] file MIME type • Prior t o PHP 4, file upload inform at ion for a field nam ed upload_file is available in a set of four HTTP_POST_VARS variables: • HTTP_POST_VARS[upload_file_name] original filename on client host • HTTP_POST_VARS[upload_file] temporary filename on server host • HTTP_POST_VARS[upload_file_size] file size, in bytes HTTP_POST_VARS[upload_file_type] file MIME type _FILES is a superglobal array global in any scope . HTTP_POST_FILES and HTTP_POST_VARS m ust be declared wit h t he global keyw ord if used in a non- global scope, such as wit hin a funct ion. To avoid having t o fool around figuring out which array cont ains file upload inform at ion, it m akes sense t o writ e a ut ilit y rout ine t hat does all t he work. The following funct ion, get_upload_info , t akes an argum ent corresponding t o t he nam e of a file upload field. Then it exam ines t he _FILES , HTTP_POST_FILES , and HTTP_POST_VARS arrays as necessary and ret urns an associat ive array of inform at ion about t he file, or an unset value if t he inform at ion is not available. For a successful call, t he array elem ent keys are tmp_name , name , size , and type t hat is, t he keys are t he sam e as t hose in t he ent ries wit hin t he _FILES or HTTP_POST_FILES arrays. function get_upload_info name { global HTTP_POST_FILES, HTTP_POST_VARS; unset unset; Look for information in PHP 4.1 _FILES array first. Check the tmp_name member to make sure there is a file. The entry in _FILES might be present even if no file was uploaded. if isset _FILES { if isset _FILES[name] _FILES[name][tmp_name] = _FILES[name][tmp_name] = none return _FILES[name]; return unset; } Look for information in PHP 4 HTTP_POST_FILES array next. if isset HTTP_POST_FILES { if isset HTTP_POST_FILES[name] HTTP_POST_FILES[name][tmp_name] = HTTP_POST_FILES[name][tmp_name] = none return HTTP_POST_FILES[name]; return unset; } Look for PHP 3 style upload variables. Check the _name member, because HTTP_POST_VARS[name] might not actually be a file field. if isset HTTP_POST_VARS[name] isset HTTP_POST_VARS[name . _name] { Map PHP 3 elements to PHP 4-style element names info = array ; info[name] = HTTP_POST_VARS[name . _name]; info[tmp_name] = HTTP_POST_VARS[name]; info[size] = HTTP_POST_VARS[name . _size]; info[type] = HTTP_POST_VARS[name . _type]; return info; } return unset; } See t he post _im age.php script for det ails about how t o use t his funct ion t o get im age inform at ion and st ore it in MySQL. The upload_tmp_dir PHP configurat ion set t ing cont rols where uploaded files are saved. This is t m p by default on m any syst em s, but you m ay want t o override it t o reconfigure PHP t o use a different direct ory t hat s owned by t he web server user I D and t hus m ore privat e.

18.9.6 Python