I am using a jQuery form builder to create any kind of form input. The issue I had, in particular, is when I create multiple file inputs, the input name is generated randomly by default, the format is something like "file-0000-0". I would like to get the input name but since it's random, I can only think of one way to fetch the name which is by using the allFiles() method. How do I fetch the name of the input?
Code Example
$fileRequest = $request->allFiles();
return $fileRequest;
It will return something like this:
{file-1649657296668-0: {…}, file-1649657297967-0: {…}}.
Now how do I get both of the file input names above?
Thanks for the help.
Since allFiles() returns associative array you can get keys:
$files = $request->allFiles();
$name_of_files = array_keys($files);
// ["file-1649657296668-0", "file-1649657297967-0"]
Or you can loop through that array and access files as well:
$files = $request->allFiles();
foreach($files as $name_of_file => $file) {
echo $name_of_file; // file-1649657296668-0
// $file is UploadedFile instance
}
Related
I am using laravel 5.4 and I'm trying to replace the imagePath field in my request (renaming the uploaded image).
explanation:
when the form is submitted the request field(request->imagePath) contains the temporary location of the uploaded image, I am moving that tmp image to a dir while changing its name ($name). so now as the request->imagePath still has old tmp image location I want to change request->imagePath value to have the new location and then create the user.
Like so
if($request->hasFile('imagePath'))
{
$file = Input::file('imagePath');
$name = $request->name. '-'.$request->mobile_no.'.'.$file->getClientOriginalExtension();
echo $name."<br>";
//tried this didn't work
//$request->imagePath = $name;
$file->move(public_path().'/images/collectors', $name);
$request->merge(array('imagePath' => $name));
echo $request->imagePath."<br>";
}
But Its not working, Here is the output
mahela-7829899075.jpg
C:\xampp\tmp\php286A.tmp
Please Help
I believe merge() is the correct method, it will merge the provided array with the existing array in the ParameterBag.
However, you're accessing the input variables incorrectly. Try using $request->input('PARAMETER_NAME') instead...
Therefore, your code should look like this:
if ($request->hasFile('imagePath')) {
$file = Input::file('imagePath');
$name = "{$request->input('name')}-{$request->input('mobile_no')}.{$file->getClientOriginalExtension()}";
$file->move(public_path('/images/collectors'), $name);
$request->merge(['imagePath' => $name]);
echo $request->input('imagePath')."<br>";
}
Note: You can also pass your path into public_path() and it will concatenate it for you.
References
Retrieving Input:
https://laravel.com/docs/5.4/requests#retrieving-input
$request->merge():
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Http/Request.php#L269
public_path: https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php#L635
Just wondering why I can't get value of array name from input type file.
HTML:
<input type="file" name="collateral_photo[]" id="collateral_photo" class="default"/>
The name of this input file is an array.
PHP Laravel
return Request::file('collateral_photo');
The return result is [{}]
What I want right now just how to get value from this input file with array name.
Please, please help me out.
Thank you very much for any help.
use the below code, since the name attribute is an array
$files = Request::file('collateral_photo');
return $files[0];
or if you want second file
return $files[1];
//if you want to access second file and so on
you need to access the file with array index specified.
If you want to return the whole files array itself then use
return $files
And going one step further we can work with multiple-level-arrays like so:
<input name="channel[1][file]" type="file">
<input name="channel[2][file]" type="file">
in laravel you would need a structure
$files = $request->file('channel');
$file1 = $files[1]['file'];
$file2 = $files[2]['file'];
If it is an array of files do:
$request()->file($fieldName)[$key]->getClientOriginalName();
As the title said i need a way to set the variable name depending of what the name of the picture is (i got over 100 different pictures)
Since i got custom classes in another php file for each picture (like tags) like for example:
$picture1 = "hillside sunset";
$picture2 = "beach cyprus";
and so on, so i need to fetch each variable for each picture
Heres the current loop where the div class is going to be each pictures name ($PICTURENAME is just to define where this code goes and is irelevant codewise):
<?php
foreach (glob("img/*.jpg") as $filename)
{
$path = $filename;
$file = basename($path);
$file = basename($path, ".jpg");
echo '<div class="'.$PICTURENAME.'" id="'.$file.'"><img src="'.$filename.'"> '.$file.' </div>';
}
?>
Don't use 100+ variables. Using a database would make far more sense, but if you don't want to get into learning that (you should, though), using a data structure would still make far more sense.
You could create one array (and use it as a map), and have the filename as the key, and the value would be the tags.
In PHP, you can address a variable using another variable:
$name = "foo";
${$name} = "bar";
echo $foo; // prints "bar"
echo ${$name}; // the same as above
However, as Kitsune already recommended, you are better off using something else, e.g., an array.
I have an multiple input sending files and I need guard this images with another name inside my folder called 'home';
So the pictures filing with the name home1.jpg, home2.jpg, etc
So, here is my code:
$file = $_FILES['Filedata'];
$filename_home = "";
$img_array = array($filename);
foreach($img_array as $key=>$value){
$filename_home.="home".$key.".jpg";
}
But this doesn't producing the result.
Any help, will be appreciate
Where does $filename come from? It looks like you want to use $file instead.
Is it possible to write at a particular location in a CSV file using PHP?
I don't want to append data at the end of the CSV file. But I want to add data at the end of a row already having values in the CSV.
thanks in advance
No, it s not possible to insert new data in the middle of a file, due to filesystem nature.
Only append at the end is possible.
So, the only solution is to make another file, write a beginning part of source, append a new value, and then append the rest of the source file. And finally rename a resulting file to original name.
There you go. Complete working code:
<?php
//A helping function to insert data at any position in array.
function array_insert($array, $pos, $val)
{
$array2 = array_splice($array, $pos);
$array[] = $val;
$array = array_merge($array, $array2);
return $array;
}
//What and where you want to insert
$DataToInsert = '11,Shamit,Male';
$PositionToInsert = 3;
//Full path & Name of the CSV File
$FileName = 'data.csv';
//Read the file and get is as a array of lines.
$arrLines = file($FileName);
//Insert data into this array.
$Result = array_insert($arrLines, $PositionToInsert, $DataToInsert);
//Convert result array to string.
$ResultStr = implode("\n", $Result);
//Write to the file.
file_put_contents($FileName, $ResultStr);
?>
Technically Col. Shrapnel's answer is absolutely right.
Your problem is that you don't want to deal with all these file operations just to change some data. I agree with you. But you're looking for the solution in a wrong level. Put this problem in a higher level. Create a model that represents an entity in your CSV database. Modify the model's state and call its save() method. The method should be responsible to write your model's state in CSV format.
Still, you can use a CSV library that abstracts low level operations for you. For instance, parsecsv-for-php allows you to target a specific cell:
$csv = new parseCSV();
$csv->sort_by = 'id';
$csv->parse('data.csv');
# "4" is the value of the "id" column of the CSV row
$csv->data[4]['firstname'] = 'John';
$csv->save();