public function testValidateMimeTypeJpgMatch() { $_FILES['field']['name'] = 'john.jpg'; $_FILES['field']['size'] = 31066; $_FILES['field']['tmp_name'] = './resources/images/john.jpg'; $uploader = new fUpload(); $uploader->setMIMETypes(array('image/jpeg'), 'Please upload a JPG image'); $this->assertEquals(NULL, $uploader->validate('field', TRUE)); }
/** * Validates uploaded files to ensure they match all of the criteria defined * * @internal * * @param fActiveRecord $object The fActiveRecord instance * @param array &$values The current values * @param array &$old_values The old values * @param array &$related_records Any records related to this record * @param array &$cache The cache array for the record * @param array &$validation_messages The existing validation messages * @return void */ public static function validate($object, &$values, &$old_values, &$related_records, &$cache, &$validation_messages) { $class = get_class($object); foreach (self::$file_upload_columns[$class] as $column => $directory) { $column_name = fORM::getColumnName($class, $column); if (isset($validation_messages[$column])) { $search_message = self::compose('%sPlease enter a value', fValidationException::formatField($column_name)); $replace_message = self::compose('%sPlease upload a file', fValidationException::formatField($column_name)); $validation_messages[$column] = str_replace($search_message, $replace_message, $validation_messages[$column]); } // Grab the error that occured try { if (fUpload::check($column)) { $uploader = self::setUpFUpload($class, $column); $uploader->validate($column); } } catch (fValidationException $e) { if ($e->getMessage() != self::compose('Please upload a file')) { $validation_messages[$column] = fValidationException::formatField($column_name) . $e->getMessage(); } } } }
public function testFileUploadField2() { $this->setExpectedException('fValidationException'); $_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['CONTENT_TYPE'] = 'multipart/form-data'; $_FILES = array(); $_FILES['file'] = array('name' => 'test.txt', 'type' => '', 'tmp_name' => './resources/text/example', 'error' => '', 'size' => 17); $uploader = new fUpload(); $uploader->setMIMETypes(array('text/csv'), 'Please upload a CSV file'); try { $v = new fValidation(); $v->addFileUploadRule('file', $uploader); $v->validate(); } catch (fValidationException $e) { $this->assertContains('File: Please upload a CSV file', $e->getMessage()); throw $e; } }
$banner->setStatus(fRequest::get('id_state', 'integer')); try { $banner->store(); } catch (Exception $e) { exit("Ha ocurrido un error."); } $lastId = $banner->prepareIdBanner(); /* * Add Region * Limited By User Permissions */ /* * Add Files to Server */ if (!empty($_FILES)) { $uploader = new fUpload(); $uploader->setOptional(); $uploader->setMIMETypes($acceptedFiles, 'El tipo de archivo es incorrecto'); $dir = 'uploads/banner/'; $dir2 = 'uploads/banner/thumbs/'; $imageDescrip = fRequest::encode('imageDescrip'); $uploaded = fUpload::count('files'); for ($i = 0; $i < $uploaded; $i++) { $ext = strtolower(pathinfo($_FILES['files']['name'][$i], PATHINFO_EXTENSION)); $_FILES['files']['name'][$i] = fURL::makeFriendly(str_replace(' ', '-', $_FILES['files']['name'][$i])) . ".{$ext}"; $uploader->move($dir, 'files', $i); $fileName[] = $_FILES['files']['name'][$i]; $fileType[] = $_FILES['files']['type'][$i]; copy($dir . $fileName[$i], $dir2 . $fileName[$i]); $image3 = new fImage($dir2 . $fileName[$i]); $image3->cropToRatio(1, 1, 'left', 'bottom');
$data->query("INSERT INTO ".$_POST["table"]."(coduser, filename) VALUES(".$_SESSION["coduser"] .",'". $Files[$i] ."')"); } }catch (fValidationException $e) { $error = $e->getMessage(); } break; case "video": //die("Pase por aca Video"); try { $error = 0; // Create an fUpload object to handle the file upload $uploader = new fUpload(); $uploader->setMaxFileSize(UPLOAD_MOVIE_MAXSIZE, "error2"); // Require the user upload an image (with MIME type checking server-side) /*$uploader->setMIMETypes( array('video/x-flv','video/mp4','video/x-ms-wmv'), 'error1' );*/ $Files = array(); $uploaded = fUpload::count('file_input_name'); $Files = $_FILES['file_input_name']['name']; for ($i=0; $i < $uploaded; $i++) { $ext = strtolower(substr($Files[$i], strrpos($Files[$i],".")+1));
//Ako je validacija prosla uspijesno if ($v->passes()) { //Nova instanca fUpload klase $uploader = $app->fupload; //Setiranje dozvoljenih ekstenzija $uploader->setMIMETypes(array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'), 'The file uploaded in not an allowed image type.'); //Ogranicavanje velicine fajla $uploader->setMaxSize('5MB'); //Validacija ucitanog fajla $error = $uploader->validate('photos', true); //Folder za smijestanje slika $dir = new fDirectory($uploadDir); //Var. u kojoj cuvamo ucitane slike u obliku niza $files = array(); //Prebrojavanje ucitanih slika $uploaded = fUpload::count('photos'); //Pokrecmo for petlju da izlista i ucita sve slike jednu po jednu for ($i = 0; $i < $uploaded; $i++) { //Prolaz kroz sve ucitane slike i ucitavanje jedne po jedne u odredjeni folder $files[] = $uploader->move($dir, 'photos', $i); //Promjena apsolutne putanje u http:// putanju slike $path = str_replace(dirname(dirname(INC_ROOT)), $app->config->get('app.url'), $uploadDir . strtolower($photos[$i])); //Upis svih slika u bazu podataka $photo->create(['user_id' => $app->auth->id, 'album_id' => $albumId, 'path' => $path, 'size' => $size[$i], 'type' => $type[$i]]); } //Odgovor server o uspijesnom ili ne uspijesnom uploadu fajlova i foldera if ($uploaded) { echo json_encode(array("status" => true, "message" => "Photos are successfully uploaded.")); } else { echo json_encode(array("status" => false, "message" => $v->errors())); }
function build_json_response() { if (!isset($_POST['json'])) { return array('error' => array('message' => "No JSON found")); } $data = json_decode($_POST['json'], true); if (!$data) { return array('error' => array('message' => "JSON could not be decoded")); } $_POST = $data; // fValidation inspects $_POST for field data $validator = new fValidation(); $validator->addRequiredFields('title', 'details', 'venue', 'address', 'organizer', 'email', 'read_comic'); $validator->addEmailFields('email'); $validator->addRegexReplacement('#^(.*?): (.*)$#', '\\2 for <span class="field-name">\\1</span>'); // If id is specified require secret $validator->addConditionalRule(array('id'), NULL, array('secret')); $messages = $validator->validate(TRUE, TRUE); if (!$data['read_comic']) { $messages['read_comic'] = 'You must have read the Ride Leading Comic'; } if ($messages) { return array('error' => array('message' => 'There were errors in your fields', 'fields' => $messages)); } $inputDateStrings = get($data['dates'], array()); $validDates = array(); $invalidDates = array(); foreach ($inputDateStrings as $dateString) { $date = DateTime::createFromFormat('Y-m-d', $dateString); if ($date) { $validDates[] = $date; } else { $invalidDates[] = $dateString; } } if ($invalidDates) { $messages['dates'] = "Invalid dates: " . implode(', ', $invalidDates); } if (count($validDates) === 1) { $data['datestype'] = 'O'; $data['datestring'] = date_format($validDates[0], 'l, F j'); } else { // not dealing with 'consecutive' $data['datestype'] = 'S'; $data['datestring'] = 'Scattered days'; } // Converts data to an event, loading the existing one if id is included in data $event = Event::fromArray($data); // Else if ($event->exists() && !$event->secretValid($data['secret'])) { return array('error' => array('message' => 'Invalid secret, use link from email')); } $messages = $event->validate($return_messages = TRUE, $remove_column_names = TRUE); if (isset($_FILES['file'])) { $uploader = new fUpload(); $uploader->setMIMETypes(array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'), 'The file uploaded is not an image'); $uploader->setMaxSize('2MB'); $uploader->setOptional(); $file_message = $uploader->validate('file', TRUE); if ($file_message != null) { $messages['file'] = $file_message; } global $IMAGEDIR; $file = $uploader->move($IMAGEDIR, 'file'); $event->setImage($file->getName()); } if ($messages) { return array('error' => array('message' => 'There were errors in your fields', 'fields' => $messages)); } // if needs secret generate and email if (!$event->exists()) { $includeSecret = true; } else { $includeSecret = false; } // If there are validation errors this starts spewing html, so we validate before $event->store(); // Create/delete EventTimes to match the list of dates included EventTime::matchEventTimesToDates($event, $validDates); // Returns the created object $details = $event->toDetailArray(true); if ($includeSecret) { $details['secret'] = $event->getPassword(); // Wait until after it is stored to ensure it has an id $event->emailSecret(); } return $details; }
echo "<select>"; Status::printOption($selectedOption); echo "</select>"; } else { if ($_POST['type'] == "lastCode") { $counter = Inv_item::findByClassificationCode($_POST['classific']); echo sprintf("%03d", $counter->count() + 1); } else { if ($_POST['type'] == "upload") { try { $uploadDirectory = new fDirectory('../storage/image/' . $_POST['hiddenId']); } catch (fExpectedException $e) { $uploadDirectory = fDirectory::create('../storage/image/' . $_POST['hiddenId']); } try { $uploader = new fUpload(); $uploader->setMIMETypes(array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'), 'The file uploaded is not an image'); $uploader->enableOverwrite(); $file = $uploader->move($uploadDirectory, 'file'); $inv_item = new Inv_item($_POST['hiddenId']); $inv_item->setImageUrl('storage/image/' . $_POST['hiddenId'] . '/' . $file->getFilename()); $inv_item->store(); echo "Image uploaded"; } catch (fExpectedException $e) { echo $e->printMessage(); } } } } } }