I am trying to get the file extension of the files from the multiple file input array.
I tried to use File::extension() but it expects a string and I have an array.
I don't need this value for the validation so I can't use Laravel validation. Thanks
I am trying to get the file extension of the files from the multiple
file input array.
Then loop over the $_FILES array.
foreach ($_FILES as $key => $file) {
$extensions[$key] = \File::extension($file['name']);
}
The above presumes no fancy/opinionated $_FILES structure mutations ;p
The best way I use,
foreach ( $request->file ( 'attachment' ) as $attachment ) {
$orignalName = $attachment->getClientOriginalName ();
$mimeType = $attachment->getMimeType () ;
}
If you are using Laravel's default file upload.
Related
I'm using akeneo-labs spreadsheet-parser library to extract data from xlsx file.
use Akeneo\Component\SpreadsheetParser\SpreadsheetParser;
$workbook = SpreadsheetParser::open('myfile.xlsx');
$myWorksheetIndex = $workbook->getWorksheetIndex('myworksheet');
foreach ($workbook->createRowIterator($myWorksheetIndex) as $rowIndex => $values) {
var_dump($rowIndex, $values);
}
Actually, you can get value by column index in a loop, is it possible to get value by column name instead?
maybe using another package as suggested fixes your problem.
also you can use array_column https://www.php.net/manual/en/function.array-column.php
Maybe you can do that in a spreadsheet CSV file by using PHP default function fgetcsv(), you can go throw an overview from here: https://www.php.net/manual/en/function.fgetcsv
fgetcsv — Gets line from file pointer and parse for CSV fields
First of all, save as your Xls file in CSV type then you can take your value from that CSV file by column name.
You can try this.
use Akeneo\Component\SpreadsheetParser\SpreadsheetParser;
$workbook = SpreadsheetParser::open('myfile.xlsx');
$myWorksheetIndex = $workbook->getWorksheetIndex('myworksheet');
// all columns
$columns = [];
foreach ($workbook->createRowIterator($myWorksheetIndex) as $rowIndex => $values) {
if ($rowIndex == 1) {
$columns = $values;
} else {
$datum = array_combine($columns, $values);
// get value by column name
var_dump($datum['name']);
}
}
I have two arrays, $files with file names and $imagesFormats with formats of this images. I want to move every file from $files to directory given in $imageFormats. Indexes of files and it's formats are same in both arrays. What's the best way to do this - creating another associative array ($image => $format) or there is another solution?
If the two array have the same key you can do in the same foreach .. eg:
foreach( $files as $key => $value ) {
yourfunction($value); ///
yourfunction($imagesFormats [$key] ) ;
}
I have this structure in a csv file
"http://example.com/example.jpg","example","example2","example3","1234,56"
Now I assign this line to a variable $line and
$fields = explode('","',$line);
It stops at "http". The same with
$fields = csv2array(str_replace(':','\\:',$line),$delimiter=',',$enclosure='"',$escape='\\');
where
function csv2array($input,$delimiter=',',$enclosure='"',$escape='\\'){
$fields=explode($enclosure.$delimiter.$enclosure,substr($input,1,-1));
foreach ($fields as $key=>$value)
$fields[$key]=str_replace($escape.$enclosure,$enclosure,$value);
return($fields);
}
How to read each field into an array?
I would suggest reading the file using fgetcsv() which returns an array of values exactly as you want.
This question already has answers here:
What is the meaning of [] [closed]
(4 answers)
Closed 8 years ago.
I have been following a tutorial on readdir(), is_dir() etc involved in setting up a small image gallery based on folders and files on my FTP. I was wondering what the $directorys[] = $file; part does specifically?
while( $file= readdir( $dir_handle ) )
{
if( is_dir( $file ) ){
$directorys[] = $file;
}else{
$files[] = $file;
}
}
$directory is an array.
The code
$directory[] = $file
adds $file to the end of $directory array. This is the same as
array_push($directory, $file).
More info at array_push at phpdocs
$file will contain the name of the item that was scanned. In this case, the use of is_dir($file) allows you to check that $file in the current directory is a directory or not.
Then, using the standard array append operator [], the $file name or directory name is added to a $files/$directorys array...
It pushes an item to the array, instead of array_push, it would only push one item to the array.
Using array_push and $array[] = $item works the same, but it's not ideal to use array_push as it's suitable for pushing multiple items in the array.
Example:
Array (
)
After doing this $array[] = 'This works!'; or array_push($array, 'This works!') it will appear as this:
Array (
[0] => This works!
)
Also you can push arrays into an array, like so:
$array[] = array('item', 'item2');
Array (
[0] => Array (
[0] => item
[1] => item2
)
)
It adds the directory to the directory array :)
if( is_dir( $file ) ){
$directorys[] = $file; // current item is a directory so add it to the list of directories
}else{
$files[] = $file; // current item is a file so add it to the list of files
}
However if you use PHP 5 I really suggest using a DirectoryIterator.
BTW naming that $file is really bad, since it isn't always a file.
It creates a new array element at the end of the array and assigns a value to it.
I'm trying to define both ['name'] and ['tmp_name'], but i'm having no idea how to do so?
Here is my code:
foreach ($_FILES['uploads']['name'] as $filename) {
// Do stuff
}
But inorder to complete and move the file, I need the $_FILES['uploads']['tmp_name'] aswell. How do I define both? I tried looking it up, but found nothing.
Get the index from the foreach and access the associated entries:
foreach ($_FILES['uploads']['name'] as $i => $filename) {
$tmp_name = $_FILES["uploads"]["tmp_name"][$i];
}
Note: Do not use the unfiltered name for the target filename. That's a client-supplied value and can contain anything.