- Timestamp:
- 02/26/06 20:11:16 (3 years ago)
- Files:
-
- tags/RELEASE_0_9_0B2/examples/example_element_file.php (modified) (4 diffs)
- tags/RELEASE_0_9_0B2/patForms/Element/File.php (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
tags/RELEASE_0_9_0B2/examples/example_element_file.php
r332 r354 49 49 'uploaddir' => './fileupload', 50 50 'overwrite' => 'no', 51 'usesession' => ' no',52 //'maxsize' => 20000,51 'usesession' => 'yes', 52 'maxsize' => 900000, 53 53 //'tempdir' => '/tmp_does:not:exist' 54 54 //'permissions' => 0666, 55 55 //'replacename' => 'i/[^a-z0-9]\./_', 56 //'mimetype' => array( 'image/*', 'image/png' ),56 'mimetype' => array( 'image/*', 'image/png' ), 57 57 ), 58 58 ), … … 68 68 'default' => 'x', 69 69 'maxlength' => '15', 70 'minlength' => ' 4',70 'minlength' => '2', 71 71 ), 72 72 ), … … 87 87 // serialize the elements 88 88 $elements = $form->renderForm(); 89 90 89 91 90 // ERROR DISPLAY ------------------------------------------------------ 92 91 // ask the form if it has been submitted and display errors. For … … 102 101 displayForm( $form, $elements ); // see patExampleGen/customFunctions.php 103 102 104 105 103 106 104 tags/RELEASE_0_9_0B2/patForms/Element/File.php
r332 r354 13 13 * Though the user may not upload a file twice of validation of other form elements faile 14 14 * - tempdir: The place to store uploaded file until finalization 15 * - maxsize: maximum size of upload file in bytes (consider your web server's and PHP settings!) 15 16 * - mimetype: Defines as many allowed filetype as you want. Supports asterisk for groups like: 'image/*' 16 17 * - replacechars: use Perl-Regular expression for filename-translation … … 205 206 "outputFormats" => array(), 206 207 ), 208 "maxsize" => array( "required" => false, 209 "format" => "int", 210 "outputFormats" => array(), 211 ), 207 212 "mimetype" => array( "required" => false, 208 213 "format" => "array", … … 324 329 } 325 330 326 if( !is_dir( $tempdir ) && !is_writeable( $tempdir ) ) 327 { 331 if( !is_dir( $tempdir ) && !is_writeable( $tempdir ) ) { 328 332 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_TEMPDIR_NOT_VALID, 329 333 'Session support is on but cannot work', … … 362 366 363 367 // editable or not? 364 if( isset( $this->attributes['edit'] ) && $this->attributes['edit'] == 'no' ) 365 { 368 if( isset( $this->attributes['edit'] ) && $this->attributes['edit'] == 'no' ) { 366 369 return $this->serializeHtmlReadonly( $value ); 367 370 } … … 369 372 // create element 370 373 $this->attributes['name'] = $nameUpload; 374 $this->attributes['id'] = $nameUpload; 371 375 $element = $this->toHtml(); 372 if( patErrorManager::isError( $element ) ) 373 { 376 if( patErrorManager::isError( $element ) ) { 374 377 return $element; 375 378 } 376 379 377 $this->attributes["name"] = $name; 378 $hidden = $this->createHiddenTag( $this->attributes["value"] ); 380 $this->attributes["name"] = $name; 381 $valueElement = $this->createHiddenTag( $this->attributes["value"] ); 382 /* 383 if( $this->useSession() && $this->getSessionValue( 'state' ) == 'valid' ) { 384 $atts = $this->attributes; 385 $atts = array( 386 'value' => $this->attributes["value"], 387 'type' => 'checkbox', 388 'title' => 'keep uploaded file: "'. $this->attributes["value"] .'"', 389 'checked' => 'checked' 390 ); 391 $valueElement = $this->createTag( 'input', 'full', $atts ); 392 } 393 */ 379 394 380 395 // and return to sender... 381 return $ hidden. "\n" . $element;396 return $valueElement . "\n" . $element; 382 397 } 383 398 … … 413 428 $empty = false; 414 429 $mime = false; 430 $uploade = false; 415 431 416 432 $error = UPLOAD_ERR_NO_FILE; 417 433 $restored = false; 418 434 419 $files = $_FILES; 420 if (($namespace = $this->getNamespace()) && isset($files[$namespace])) { 421 $files = $files[$namespace]; 422 } 423 435 $files = false; 436 if( isset( $_FILES ) ) { 437 $files = $_FILES; 438 439 if( ( $namespace = $this->getNamespace() ) && isset( $files[$namespace] ) ) { 440 $files = $files[$namespace]; 441 } 442 } 443 424 444 // file was just uploaded 425 if( isset( $files ) && (isset($files[$nameUpload]) || isset($files['name'][$nameUpload]))) 426 { 427 if (!$namespace) { 445 if( !empty( $files ) && ( isset($files[$nameUpload] ) || isset( $files['name'][$nameUpload] ) ) ) { 446 if( !$namespace ) { 428 447 $uploadFile = $files[$nameUpload]['tmp_name']; 429 448 $error = $files[$nameUpload]['error']; … … 440 459 441 460 // see if the value comes from "setValue()" instead 442 if( empty( $value ) && !empty( $this->value ) ) 443 { 444 if( $this->useSession() ) 445 { 461 if( empty( $value ) && !empty( $this->value ) ) { 462 if( $this->useSession() ) { 446 463 // already stored in session 447 464 $state = $this->getSessionValue( 'state' ); 448 if( $state == 'valid' ) 449 { 465 if( $state == 'valid' ) { 450 466 return true; 451 467 } 468 $this->setSessionValue( 'state', 'finalized' ); 469 return true; 452 470 } 453 454 /*455 $dir = $this->attributes['uploaddir'];456 $mime = mime_content_type( $dir . '/' . $this->value );457 if( !$this->_checkMimeType( $mime ) )458 {459 return false;460 }461 */462 463 if( $this->useSession() )464 {465 $this->setSessionValue( 'state', 'finalized' );466 }467 return true;468 471 } 469 472 470 473 // don't allow empty files 471 if( !$error && !$size ) 472 { 474 if( !$error && !$size ) { 473 475 $this->addValidationError( 6 ); 474 476 return false; … … 476 478 477 479 // new file -> must be valid 478 if( $this->useSession() ) 479 { 480 if( $this->useSession() ) { 480 481 $this->setSessionValue( 'state', 'invalid' ); 481 } 482 483 // remove previous uploaded file 484 if( $oldfile = $this->getSessionValue( 'uploadfile' ) ) 485 { 486 unlink( $oldfile ); 487 } 488 489 if( !$this->_checkErrorCode( $error ) ) 490 { 482 483 // remove previous uploaded file 484 if( $oldfile = $this->getSessionValue( 'uploadfile' ) ) { 485 unlink( $oldfile ); 486 } 487 } 488 489 if( !$this->_checkErrorCode( $error ) ) { 491 490 return false; 492 491 } 493 492 494 493 // validate file-type 495 if( !$this->_checkMimeType( $mime ) ) 496 { 494 if( !$this->_checkMimeType( $mime ) ) { 497 495 return false; 498 496 } 497 498 if( $size > $this->attributes['maxsize'] ) { 499 $this->addValidationError( 3, array( 'maxsize' => $this->attributes['maxsize'] ) ); 500 return false; 501 } 499 502 } 500 503 // see if file was uploaded during this session … … 505 508 $mime = $this->getSessionValue( 'mime' ); 506 509 507 if( $sessValue = $this->getSessionValue( 'name' ) ) 508 { 510 if( $sessValue = $this->getSessionValue( 'name' ) ) { 509 511 $value = $sessValue; 510 512 } 511 513 512 514 // all done, skip the rest 513 if( $state == 'finalized' ) 514 { 515 if( $state == 'finalized' ) { 515 516 $this->value = $value; 516 517 return true; … … 518 519 519 520 $exists = $this->_fileExists( $value ); 520 if( patErrorManager::isError( $exists ) ) 521 { 521 if( patErrorManager::isError( $exists ) ) { 522 522 return $exists; 523 523 } 524 524 525 if( $exists ) 526 { 525 if( $exists ) { 527 526 return true; 528 527 } 529 528 530 if( $state == 'valid' || file_exists( $uploadFile ) ) 531 { 529 if( $state == 'valid' || file_exists( $uploadFile ) ) { 532 530 $restored = true; 533 531 $error = UPLOAD_ERR_OK; 534 532 } 535 533 536 if( !$this->_checkErrorCode( $error ) ) 537 { 534 if( !$this->_checkErrorCode( $error ) ) { 538 535 return false; 539 536 } … … 543 540 { 544 541 $exists = $this->_fileExists( $value ); 545 if( patErrorManager::isError( $exists ) ) 546 { 542 if( patErrorManager::isError( $exists ) ) { 547 543 return $exists; 548 544 } 549 else if( $exists ) 550 { 545 else if( $exists ) { 551 546 return true; 552 547 } … … 554 549 555 550 // not required 556 if( empty( $value ) && $this->attributes['required'] != 'yes' ) 557 { 551 if( empty( $value ) && $this->attributes['required'] != 'yes' ) { 558 552 return true; 559 553 } 560 554 561 555 $checkDir = $this->_checkDestination(); 562 if( patErrorManager::isError( $checkDir ) ) 563 { 556 if( patErrorManager::isError( $checkDir ) ) { 564 557 return $checkDir; 565 558 } … … 567 560 // check if file exists 568 561 $dir = $this->attributes['uploaddir']; 569 if( $this->attributes['overwrite'] != 'yes' ) 570 { 571 if( file_exists( $dir . '/' . $value ) ) 572 { 562 if( $this->attributes['overwrite'] != 'yes' ) { 563 if( file_exists( $dir . '/' . $value ) ) { 573 564 $this->addValidationError( 2 ); 574 565 return false; … … 577 568 578 569 // keep uploaded file 579 if( $this->useSession() && !$restored ) 580 { 570 if( $this->useSession() && !$restored ) { 581 571 $tempfile = tempnam( $this->tempdir, $name . '_' ); 582 if( !move_uploaded_file( $uploadFile, $tempfile ) ) 583 {572 573 if( !move_uploaded_file( $uploadFile, $tempfile ) ) { 584 574 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_CANNOT_MOVE_UPLOAD_FILE, 585 575 "Cannot upload file", … … 613 603 $dir = $this->attributes["uploaddir"]; 614 604 615 $files = $_FILES;616 if (($namespace = $this->getNamespace()) && isset($files[$namespace])) {617 $files = $files[$namespace];618 }619 620 605 $uploadFile = false; 621 if( $this->useSession() ) 622 { 606 if( $this->useSession() ) { 623 607 // check if this upload was finalized 624 608 $state = $this->getSessionValue( "state" ); 625 if( $state == "finalized" ) 626 { 609 if( $state == "finalized" ) { 627 610 return true; 628 611 } … … 633 616 $this->setSessionValue( "state", "finalized" ); 634 617 } 635 else if( isset( $files[$nameUpload]["tmp_name"] ) ) 636 { 637 $uploadFile = $files[$nameUpload]["tmp_name"]; 638 } 639 else if( isset( $files["tmp_name"][$nameUpload] ) ) 640 { 641 $uploadFile = $files["tmp_name"][$nameUpload]; 618 else { 619 $files = $_FILES; 620 if (($namespace = $this->getNamespace()) && isset($files[$namespace])) { 621 $files = $files[$namespace]; 622 } 623 624 if( isset( $files[$nameUpload]["tmp_name"] ) ) { 625 $uploadFile = $files[$nameUpload]["tmp_name"]; 626 } 627 else if( isset( $files["tmp_name"][$nameUpload] ) ) { 628 $uploadFile = $files["tmp_name"][$nameUpload]; 629 } 642 630 } 643 631 644 632 // if file does not exist and is not required, function will just return 645 if( $this->attributes["required"] != "yes" && !$uploadFile ) 646 { 633 if( $this->attributes["required"] != "yes" && !$uploadFile ) { 647 634 return true; 648 635 } 649 636 650 637 // cannot use 'move_upload_file()' in case of usage of sessions 651 if( !rename( $uploadFile, "$dir/$value" ) ) 652 { 638 if( !rename( $uploadFile, "$dir/$value" ) ) { 653 639 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_CANNOT_MOVE_UPLOAD_FILE, 654 640 "Cannot upload file", … … 688 674 // no file send 689 675 case UPLOAD_ERR_NO_FILE: 690 if( $this->attributes['required'] == 'yes' ) 691 { 676 if( $this->attributes['required'] == 'yes' ) { 692 677 $this->addValidationError( 1 ); 693 678 return false; … … 714 699 function _checkMimeType( $type ) 715 700 { 716 if( empty( $this->attributes["mimetype"] ) ) 717 { 701 // an empty type means there was no file uploaded - don't bother about that 702 if( empty( $type ) ) { 703 return true; 704 } 705 706 if( empty( $this->attributes["mimetype"] ) ) { 718 707 return true; 719 708 } 720 709 721 710 // check for exact match 722 if( in_array( $type, $this->attributes["mimetype"] ) ) 723 { 711 if( in_array( $type, $this->attributes["mimetype"] ) ) { 724 712 return true; 725 713 } … … 727 715 // check for *-pattern like image/* 728 716 list( $major ) = explode( "/", $type ); 729 foreach( $this->attributes["mimetype"] as $m ) 730 { 717 foreach( $this->attributes["mimetype"] as $m ) { 731 718 $m = explode( "/", $m ); 732 if( $m[1] == "*" && $m[0] == $major ) 733 { 719 if( $m[1] == "*" && $m[0] == $major ) { 734 720 return true; 735 721 } … … 751 737 $preg = explode( "/", $this->attributes["replacename"] ); 752 738 753 if( count( $preg ) != 3 ) 754 { 739 if( count( $preg ) != 3 ) { 755 740 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_REPLACE_PREG_INVALID, 756 741 "Attribute 'replace' is not a valid PREG", … … 770 755 { 771 756 // check destination 772 if( !strlen( $this->attributes["uploaddir"] ) ) 773 { 757 if( !strlen( $this->attributes["uploaddir"] ) ) { 774 758 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_NO_FILE_UPLOAD_DIR, 775 759 "Cannot upload file", … … 778 762 779 763 $dir = $this->attributes["uploaddir"]; 780 if( !is_dir( $dir ) ) 781 { 764 if( !is_dir( $dir ) ) { 782 765 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_NO_FILE_UPLOAD_DIR, 783 766 "Cannot upload file", … … 785 768 } 786 769 787 if( !is_writeable( $dir ) ) 788 { 770 if( !is_writeable( $dir ) ) { 789 771 return patErrorManager::raiseError( PATFORMS_FILE_ERROR_UPLOAD_DIR_NOT_WRITEABLE, 790 772 "Cannot upload file", … … 804 786 function _fileExists( $file ) 805 787 { 806 if( !$file && !strlen( $file ) ) 807 { 788 if( !$file && !strlen( $file ) ) { 808 789 return false; 809 790 } 810 791 811 792 $result = $this->_checkDestination(); 812 if( patErrorManager::isError( $result ) ) 813 { 793 if( patErrorManager::isError( $result ) ) { 814 794 return $result; 815 795 } 816 796 817 797 $dir = $this->attributes["uploaddir"]; 818 if( file_exists( $dir . "/" . $file ) ) 819 { 798 if( file_exists( $dir . "/" . $file ) ) { 820 799 return true; 821 800 } … … 831 810 function _getPermission() 832 811 { 833 if( $this->permission != null ) 834 { 812 if( $this->permission != null ) { 835 813 return $this->permission; 836 814 } … … 839 817 840 818 // permission string must be contain four letters, e.g. 0644 841 if( strlen( $perm ) != 4 ) 842 { 819 if( strlen( $perm ) != 4 ) { 843 820 patErrorManager::raiseError( PATFORMS_FILE_ERROR_PERMISSION_ATTRIBUTE_NOT_VALID, 844 821 "Wrong format of attribute 'permissions'", … … 847 824 } 848 825 849 if( !is_numeric( $perm ) ) 850 { 826 if( !is_numeric( $perm ) ) { 851 827 patErrorManager::raiseError( PATFORMS_FILE_ERROR_PERMISSION_ATTRIBUTE_NOT_VALID, 852 828 "Wrong format of attribute 'permissions'", … … 855 831 } 856 832 857 if( $perm[0] != '0' ) 858 { 833 if( $perm[0] != '0' ) { 859 834 patErrorManager::raiseError( PATFORMS_FILE_ERROR_PERMISSION_ATTRIBUTE_NOT_VALID, 860 835 "Wrong format of attribute 'permissions'",
