Example:
Note: Remember to set destination and filter to adaptor before running isValid or Receive,
Because setting filter or destination after running isvalid will cause the isvalid to add 2 times check on the file, one as tmp_name and another as destination name..
which will cause Count validator to fail!
$sDestination .= getcwd() . "/public/assets";
if (!is_dir($sDestination)) {
if (!@mkdir($sDestination, 0777, true)) {
throw new \Exception("Unable to create destination: " . $sDestination);
}
}
$size = new Size(array('min' => 10, "max" => 10000000)); //minimum bytes filesize
$count = new Count(array("min" => 0, "max" => 1));
$extension = new Extension(array("extension" => array("jpg", "png", "swf")));
$oFile = $this->params()->fromFiles('txtFile1');
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setValidators(array($size, $count, $extension), $oFile['name']);
$adapter->setDestination($sDestination);
if (! $adapter->isValid()) {
$dataError = $adapter->getMessages();
$error = array();
foreach ($dataError as $key => $row) {
$error[] = $row;
}
$form->setMessages(array('txtFile1' => $error));
} else {
if ($adapter->receive($oFile['name'])) {
$form->get("txtFile1")->setValue($adapter->getFileName());
} else {
$form->setMessages(array('txtFile1' => "Receive failed"));
}
}
No comments:
Post a Comment