本文整理汇总了PHP中file_validate_extensions函数的典型用法代码示例。如果您正苦于以下问题:PHP file_validate_extensions函数的具体用法?PHP file_validate_extensions怎么用?PHP file_validate_extensions使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了file_validate_extensions函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFileValidateExtensions
/**
* Test the file_validate_extensions() function.
*/
function testFileValidateExtensions()
{
$file = entity_create('file', array('filename' => 'asdf.txt'));
$errors = file_validate_extensions($file, 'asdf txt pork');
$this->assertEqual(count($errors), 0, 'Valid extension accepted.', 'File');
$file->setFilename('asdf.txt');
$errors = file_validate_extensions($file, 'exe png');
$this->assertEqual(count($errors), 1, 'Invalid extension blocked.', 'File');
}
示例2: validatePdfUpload
protected function validatePdfUpload(array &$form, FormStateInterface &$form_state, UploadedFile $file_upload, $file_field_name) {
/**
* @var $file_upload \Symfony\Component\HttpFoundation\File\UploadedFile
*/
if ($file_upload && $file_upload->isValid()) {
// Move it to somewhere we know.
$uploaded_filename = $file_upload->getClientOriginalName();
// Ensure the destination is unique; we deliberately use managed files,
// but they are keyed on file URI, so we can't save the same one twice.
$scheme = $this->config('fillpdf.settings')->get('scheme');
$destination = file_destination(FillPdf::buildFileUri($scheme, 'fillpdf/' . $uploaded_filename), FILE_EXISTS_RENAME);
// Ensure our directory exists.
$fillpdf_directory = FillPdf::buildFileUri($scheme, 'fillpdf');
$directory_exists = file_prepare_directory($fillpdf_directory, FILE_CREATE_DIRECTORY + FILE_MODIFY_PERMISSIONS);
if ($directory_exists) {
$file_moved = $this->fileSystem->moveUploadedFile($file_upload->getRealPath(), $destination);
if ($file_moved) {
// Create a File object from the uploaded file.
$new_file = File::create([
'uri' => $destination,
'uid' => $this->currentUser()->id(),
]);
$errors = file_validate_extensions($new_file, 'pdf');
if (count($errors)) {
$form_state->setErrorByName('upload_pdf', $this->t('Only PDF files are supported, and they must end in .pdf.'));
}
else {
$form_state->setValue('upload_pdf', $new_file);
}
}
else {
$form_state->setErrorByName('upload_pdf', $this->t("Could not move your uploaded file from PHP's temporary location to Drupal file storage."));
}
}
else {
$form_state->setErrorByName('upload_pdf', $this->t('Could not automatically create the <em>fillpdf</em> subdirectory. Please create this manually before uploading your PDF form.'));
}
}
else {
$form_state->setErrorByName('upload_pdf', $this->t('Your PDF could not be uploaded. Did you select one?'));
}
}
示例3: hook_file_insert
/**
* Respond to a file being added.
*
* This hook is called after a file has been added to the database. The hook
* doesn't distinguish between files created as a result of a copy or those
* created by an upload.
*
* @param $file
* The file that has been added.
*
* @see file_save()
*/
function hook_file_insert($file)
{
// Add a message to the log, if the file is a jpg
$validate = file_validate_extensions($file, 'jpg');
if (empty($validate)) {
watchdog('file', 'A jpg has been added.');
}
}