-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.php
131 lines (93 loc) · 3.69 KB
/
form.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
/**
* @file form.php
*/
// Load Zend Framework
set_include_path(
implode(PATH_SEPARATOR, array(
get_include_path(),
__DIR__ .'/'
))
);
// Create the auto loader so zend can load the rest automatically
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$form = new Zend_Form;
// Set form name, id, method and action
$form->setName( 'fileUpload' )
->setAttrib( 'id', 'fileUpload' )
->setMethod( 'post' )
->setAttrib('enctype', 'multipart/form-data')
->setAction( 'http://'.
$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
);
$file = new Zend_Form_Element_File('file');
$file->setLabel('File')
->setRequired(true)
//do not save file when getValues() is called
->setValueDisabled(False)
->addValidator('Extension', FALSE, 'jpg,jpeg')
->addValidator(
'Size',
false,
array( 'min' => 20, 'max' => 20000000 ) // 2MB
);
$file->getValidator('Extension')->setMessage( 'JPEGs only' );
// Create text area for message
$message = new Zend_Form_Element_Textarea( 'message' );
$message->setRequired( TRUE )
->setAttrib( 'id', 'message' )
->addErrorMessage( 'Please specify a message' );
// Create a submit button
$submit = new Zend_Form_Element_Submit( 'submit' );
$submit->setLabel( 'Submit' )
->setAttrib( 'id', 'submit' );
// Set the ViewScript decorator for the form and tell it which
// template file to use
$form->setDecorators( array(
array( 'ViewScript', array( 'viewScript' => 'form-contact.tpl.php' ) ) ) );
// Add the form elements AFTER the viewscript decorator has been set
$form->addElements( array( $file, $message, $submit ) );
// Get rid of all element decorators except for ViewHelper to render
// the individual elements and Errors decorator to render the errors.
$form->setElementDecorators( array( 'ViewHelper', 'Errors' ) );
// The file element requires the File decorator instead of the
// ViewHelper decorator
$file->setDecorators( array( 'File', 'Errors' ) );
// Create an instance of Zend_View and set the directory
// for the template files
$view = new Zend_View();
$view->setScriptPath( __DIR__ );
// Tell all the elements in the form which view to use when rendering
foreach ($form as $item){
$item->setView($view);
}
// process or display the form
if( isset( $_POST['submit'] ) && ( $form->isValid( $_POST ) ) ) {
$uploadHandler = new Zend_File_Transfer_Adapter_Http();
$uploadHandler->setDestination( __DIR__ .'/uploads/' );
try {
$uploadHandler->receive();
$data = $form->getValues();
Zend_Debug::dump($data, 'Form Data:');
$fullPath = $uploadHandler->getFileName( 'file' );
$size = $uploadHandler->getFileSize( 'file' );
$mimeType = $uploadHandler->getMimeType( 'file' );
$fileInfo = pathinfo( $fullPath );
$name = $fileInfo['basename'] . '<br>';
// rename the file for security purpose
$newName = 'RM_' . time() . '_' . $fileInfo['basename'] ;
$fullFilePath = __DIR__ .'/uploads/'.$newName;
$filterFileRename = new Zend_Filter_File_Rename(
array('target' => $fullFilePath, 'overwrite' => true)
);
$filterFileRename -> filter($fullPath);
echo 'thanks <br />';
} catch ( Zend_File_Transfer_Exception $e ) {
echo $e->getMessage();
}
} else {
echo $form->render( $view );
}