PHP Yii framework error message Undefined index - php

I'm trying to give allowed extension so I did try this code but I get error say
Undefined index: files[]
$ly_formname = 'files[]';
$file_type = $_FILES[$ly_formname]['type']; //returns the mimetype
$allowed = array("image/jpeg", "image/gif", "application/pdf");
if(!in_array($file_type, $allowed)) {
$error_message = 'Only jpg, gif, and pdf files are allowed.';
$error = 'yes';
}
I changed my variable name and still get the message error
the form name is file[] in yii framework
public function run()
{
return Html::input('file', 'files[]', null, $this->getOptions());
}
the input code I have is :
<input type="file" id="contentFormFilesGallery" name="files[]"
multiple="multiple" title="Upload file" accept="image/*" data-upload-url=""
data-upload-drop-zone="#contentFormBody" data-upload-
progress="#contentFormFiles_progress" data-upload-
preview="#contentFormFiles_preview" data-upload-form="" data-upload-single=""
data-upload-submit-name="fileList[]" data-upload-hide-in-stream="" data-php-max-
file-uploads="20" data-php-max-file-uploads-message="Sorry, you can only upload
up to 20 files at once." data-max-number-of-files="50" data-max-number-of-files-
message="This upload field only allows a maximum of 50 files." data-ui-
widget="file.Upload" data-ui-init="1" style="display:none">

Change files[] to files.
By having multiple form elements with the same name, PHP will convert those to an array. You don't have to (and can't) treat them as an array in HTML.
So, if you have two input with the name files, you can access it as $_POST['files'] or $_GET['files'].

Related

How to upload multiple files from single form input in Laravel?

I'm trying to upload multiple pdf files using Laravel. I've made the form using simple HTML and enabled the multiple attribute in the input field. I've tried many other methods mentioned in the stack community but they didn't work for me.
In the controller, I've the following to check whether the files are being uploaded or not. The foreach loop will return the path of the files uploaded.
Controller Code
if ($request->hasFile('corpfile')) {
$file = $request->file('corpfile');
// dd($file);
foreach ($file as $attachment) {
$path = $attachment->store('corporate');
dd($path);
}
HTML Form Code
<input type="file" name='corpfile[]' accept=".xls, .xlsx, .xlsm, .pdf" multiple>
The dd($file) is returning an array with all the files uploaded but the dd($path) is returning only one file's path.
I don't know what the issue is. Any help is appreciated.
you're diying the loop with dd
if you want to store the path in array , you should do that
$path = [];
if ($request->hasFile('corpfile')) {
$file = $request->file('corpfile');
// dd($file);
foreach ($file as $attachment) {
$path[] = $attachment->store('corporate');
}
}

How to convert the PHP Photo Upload /images for PHP video upload /videos?

My Php class for photos is:
class Photo extends Db_object{
protected static $db_table = "photos";
protected static $db_table_fields = array('photo_id', 'title', 'description', 'filename', 'type', 'size');
public $photo_id;
public $title;
public $description;
public $filename;
public $type;
public $size;
public $tmp_path;
public $upload_directory = "images";
public $custom_errors = array();
public $upload_errors_array = array(
UPLOAD_ERR_OK => "There is no error",
UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize",
UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive",
UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded.",
UPLOAD_ERR_NO_FILE => "No file was uploaded.",
UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder.",
UPLOAD_ERR_NO_FILE => "Failed to write file to disk.",
UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload."
);
And the upload form is
<form action="upload.php" enctype="multipart/form-data" method="post">
<?php
//check if there was an error and print out the error
if(!empty($upload_errors)){
echo $the_message;
}
?>
<input type="file" name="file_upload"><br>
<input type="submit" name="submit">
</form>
So i've never uploaded a video before and used a or whatever tag that is. Can anyone point to a tutorial that is not javasript and backend drag and drop video files? Specifically uploaded the file with .avi, mp4 and so on? I realize that photo uploads allows gifs so that could work but I am looking for a mvc hard coded approach.
I do not quite understand your logic but if you want to upload a video and save the information you want in your database you can use some library in javascript (Jquery) like : dropzone for upload (front-end side) and use the global variable $ _FILES (look this) to extract the information you need!

Laravel 5 file and image upload

View:
<input type="file" name="photo">
<button class="btn">Send</button>
<input type="submit" value="send"> // I tested by input and button but it also does not work
Controller:
$f = $request->file("photo");
if($f->isValid()){
echo "upload"
$f->move("/public/upload/file.png");
}else{
echo 'not upload';
}
Error:
Fatal error: Call to a member function isValid() on null
I do not know why but I can not do upload images to the site. I've tried many ways but nothing works. Send files by request and get an error, I tried to use a Request::file('photo') or Input::file('photo')->isValid() but it not work too.
var_dump($request->file('prod_photo'));
If you will use such code in response gets NULL
EDIT:
$f = $request->file('photo');
if ($f->isValid()) {
$ext = $f->getClientOriginalExtension();
$org_name = $f->getClientOriginalName();
$r = rand(11111,999999);
$f->move("public/uploads", $r.$org_name);
//
}
How can I change the image size and check if the file is certainly the picture?
Add enctype="multipart/form-data" to your form
EDIT:
You can easily check it with validator, like so:
$this->validate($request, [
'photo' => 'required|image' //works for jpeg, png, bmp, gif, or svg
]);
Unfortunely u cant change image size with native php (as i know), you need to use either ImageMagick, GD or Glide (which needs previous one)

custom validation rule for joomla file upload

I am trying to validate a file upload as below using a custom validation rule but its not working.
class JFormRuleResume extends JFormRule
{
public function test(&$element, $value, $group = null, &$input = null, &$form = null)
{
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
$jinput = JFactory::getApplication()->input;
$fileInput = new JInput($_FILES);
$file = $fileInput->get('jform', null, 'files', 'array');
//$files = $jinput->files->get('jform');
//$file = $files['resume'];
$filename = JFile::makeSafe($file['name']['resume']);
$filesize = $file['size'];
if (strtolower(JFile::getExt($filename))!='pdf') {
$element->addAttribute('message', strtolower(JFile::getExt($filename)));
return false;
}
if($filesize<2000000){
$element->addAttribute('message', "File size bigger than 2MB");
return false;
}
//var_dump($files);
return true;
}
}
Whether I upload a pdf file or other files with different extension, the error "Invalid file type" is returned.
Please enlighten me what the problem is?
My field looks like this:
<field
name="resume"
type="file"
label="Resume"
description=""
size="40"
accept="application/pdf"
validate="resume"
required="true"
/>
UPDATE
This is an update to what i've discovered so far. The post data
$requestData = JRequest::getVar('jform', null, 'post', 'array');
do not fetch the file input value. It is therefore, i've to add the following code in the controller action before i validate the form $data = $model->validate($form, $requestData);
$requestData = JRequest::getVar('jform', null, 'post', 'array');
// Get the file data array from the request.
$jinput = JFactory::getApplication()->input;
$fileInput = new JInput($_FILES);
$file = $fileInput->get('jform', null, 'files', 'array');
// Make the file name safe.
$filename = JFile::makeSafe($file['name']['resume']);
$requestData['resume'] = strtolower($filename);
JRequest::setVar('jform', $requestData );
$form = $model->getForm();
if (!$form) {
JError::raiseError(500, $model->getError());
return false;
}
$data = $model->validate($form, $requestData);
This way i am able to inject the file input value to the post array. But the problem still lies somewhere because the above validation works only partially. The validation works correctly only for files like docx, png, htm, php, txt .... and if i submit doc, pdf (larger than 2MB), zip files the validation do not work at all, instead i am displayed back the form without data and with the validation warning that all the fields are missing.
This is very strange, i desperately need some help.
Finally made through with the problem of form being redisplayed when file of bigger size is uploaded. It was a server problem, had to change the following in php.ini of my wamp server
upload_max_filesize = 100M
post_max_size = 100M
And regarding the second issue for file upload value not being in post was a new thing to me which i didn't know. Had to upload the file seperately first, then get the name by which the file was saved and then use that name to be inserted in the database.

How to get uploaded file name and size before validating that file using zend addvalidator

when using zend add validators for validating a file size and extension.Is it possible to track the file name and size of the file uploaded.Can we get the size and name of the file even when validation fails.I cant track the file name when validation fails as the file not coming to temp directory when validation fails.Is the file validated before storing in temp directory
here is my code snippet:
$document_path_field = $this->CreateElement('file','document_path');
$document_path_field->setLabel('Document');
$document_path_field->setAttrib('class','button');
//$document_path_field->setDestination(SUPPORTING_DOCUMENT_DIRECTORY);
$document_path_field->addValidator('Count', false, 1);
$document_path_field->addPrefixPath('Course_Validate_File', 'Course/validate/File', 'validate');
$document_path_field->addValidator('Size', false, 1000000);
$document_path_field->addPrefixPath('Course_Validate_File', 'Course/validate/File', 'validate');
$document_path_field->addValidator('CheckExtension',false,'docx,doc,jpg,png,gif,pdf');
$document_path_field->clearDecorators();
if(isset($field_required_array['document_path']) && $field_required_array['document_path'] == "Yes")
{
$document_path_field->setRequired(true);
}
else
{
$document_path_field->setRequired(false);
}
$document_path_field->setDecorators($this->setFieldElementDecorators());
if(in_array('document_path',$field_names_array))
{
array_push($form_elements,$document_path_field);
}
$current_document_path = $this->CreateElement('hidden','current_document_path');
$current_document_path->setLabel('Current Document')
->clearDecorators()
->addDecorator($imageviewScript)
->setValue($this->_document_path);
array_push($form_elements,$current_document_path);
The code you have posted looks like it's for a form, not the controller so it doesn't really help tbh.
Under example #2 here: http://framework.zend.com/manual/en/zend.file.transfer.introduction.html#zend.file.transfer.introduction.checking
In the second if statement, you can simply get the file name:
if (!$upload->isValid($file)) {
print "Sorry but $file is not what we wanted";
continue;
} else echo 'File '.$info['name'].' is not allowed';
More info: File Upload using zend framework 1.7.4

Categories