i am using dynamic add files and others input fields using jquery, in html using array as name and in route using POST method
when click on add button dynamically add html tags and user will add files and input field and submit
i was upload 5 files and other 5 input fields but when print that files in controller only display 3 files index but display 5 input fields , 2 upload files are missing
View
<form action="{{ route('admin.order.multipleorderstore') }}" method="post" enctype="multipart/form-data" id="ordersubmitbtn">
<div class="form-group col-md-4 col-xs-12 directtocust'+i+'" style="display:none">
<label>Company Name</label>
<input type="text" placeholder="Enter Company Name" class="form-control" id="com_name'+i+'" name="com_name[]"/>
</div>
<div class="form-group col-md-4 col-xs-12">
<label for="invoice">Invoice</label>
<span class="text-danger">*</span>
<input type="file" class="form-control invoicecls" name="invoice[]" required/>
</div>
</form>
Route
Route::post('order/multipleorderstore', 'OrderController#admin_multipleorderstore')->name('admin.order.multipleorderstore');
Controller
public function admin_multipleorderstore(Request $request)
{
//if ($request->method() == 'POST') {
$data = $request->all();
echo '<pre>';
print_r($data);
print_r($request->file('invoice'));
exit;
}
Output
[com_name] => Array
(
[0] => 27
[1] => 27
[2] => 27
[3] => 27
[4] => 31
[5] => 30
)
[invoice] => Array
(
[0] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 55.6 KB .pdf
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/pdf
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[hashName:protected] =>
[pathName:SplFileInfo:private] => /tmp/phpT5cqp5
[fileName:SplFileInfo:private] => phpT5cqp5
)
[1] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 55.6 KB .pdf
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/pdf
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[hashName:protected] =>
[pathName:SplFileInfo:private] => /tmp/phpy9u01S
[fileName:SplFileInfo:private] => phpy9u01S
)
[2] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 55.6 KB .pdf
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/pdf
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[hashName:protected] =>
[pathName:SplFileInfo:private] => /tmp/phpePaNQG
[fileName:SplFileInfo:private] => phpePaNQG
)
)
please help
Related
Here, is my html form :
<form method="post" action="filename.php" enctype="multipart/form-data">
<input id="file_upload" name="image[]" value="Upload" multiple="" type="file">
<button type="submit">Submit</button>
</form>
When I print $_FILES in filename.php file then I am able to get below array in PC :
Array
(
[name] => Array
(
[0] => Sket-printed-shirt.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpSA6YF5
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 280515
)
)
But getting below array in smart phones like : Motorola G4 plus , G5 plus and HTC one mini 2
Array
(
[name] => Array
(
[0] => Sket-printed-shirt.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] =>
)
[error] => Array
(
[0] => 1
)
[size] => Array
(
[0] =>
)
)
The reason you don't have tmp_name there is because the upload failed with error code 1, which indicates the file was too large. See: upload file error codes.
UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the
upload_max_filesize directive in php.ini.
So I a Manifest where you can add multiple customers, produce etc and it saves in the DB as an array
Now I'm making a page where you can view all the manifest info I want it to look something like this
if I try to do something like this {{ $manifest->customer_name }} i get a error htmlspecialchars() expects parameter 1 to be string, array given because it's an array I'm only having trouble with displaying the array data
Controller Code to get the correct manifest
public function view($id) {
$manifests = Manifest::where('id', $id)->where('user_id', Auth::user()->id)->firstOrFail();
return view('users.manifest.view', compact('manifests'));
}
view - usually I would just put {{ $manifets->customer_name }} but i can't because it an array
<table class="table table-striped">
<thead>
<tr>
<th width="15%">Customer</th>
<th width="15%">Produce</th>
<th width="15%">Task</th>
<th width="15%">Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifests->customer_name }}" />
</td>
</tr>
</tbody>
</table>
print_r
App\Manifest Object
(
[table:protected] => manifest
[fillable:protected] => Array
(
[0] => user_id
[1] => date
[2] => driver_name
[3] => truck_number
[4] => run_number
[5] => customer_name
[6] => produce
[7] => task
[8] => units
)
[casts:protected] => Array
(
[customer_name] => array
[produce] => array
[task] => array
[units] => array
)
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[original:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
here is the updated code
#foreach($manifests as $manifest)
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifest['customer_name'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['produce'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['task'] }}" />
</td>
</tr>
#endforeach
The problem is that you are storing array (which is an object in PHP) in database.
This makes a form unrecognisable when you retrieve back the data.
You should serialise the array before storing it to database. It will save the data as a kind of JSON string.
serialize($array_var_name);
Further to get the array back you can use
unserialize($data_var);
Now you can use the array as usual array
You can do something like this:
#foreach($manifets as $manifestos)
#for ($counter = 0; $counter < sizeof($manifestos['customer_name']); $counter++)
<tr>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['customer_name'][$counter] }}" />
</td>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['otherfields'][$counter]}}" />
</td>
//more td here depending on the fields you want to display
</tr>
#endfor
#endforeach
PHP states:
When the session.upload_progress.enabled INI option is enabled, PHP
will be able to track the upload progress of individual files being
uploaded.
I check mine beforehand: echo ini_get("session.upload_progress.enabled");, returns 1 (true)
The upload progress will be available in the $_SESSION superglobal
when an upload is in progress, and when POSTing a variable of the same
name as the session.upload_progress.name INI setting is set to.
When PHP detects such POST requests, it will populate an array in the
$_SESSION, where the index is a concatenated value of the
session.upload_progress.prefix and session.upload_progress.name INI
options.
Code:
<?php
session_start();
?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="submit" />
</form>
<?php
echo '<pre>';
print_r($GLOBALS);
echo '</pre>';
?>
Result:
Array
(
[_GET] => Array
(
)
[_POST] => Array
(
[PHP_SESSION_UPLOAD_PROGRESS] => 123
)
[_COOKIE] => Array
(
[PHPSESSID] => pmbqca8fedqgg3hg6nge41fno2
)
[_FILES] => Array
(
[file1] => Array
(
[name] => profile.jpg
[type] => image/jpeg
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpPLwrND
[error] => 0
[size] => 6946
)
[file2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
[GLOBALS] => Array
*RECURSION*
[_SESSION] => Array
(
)
)
The SESSION variable is completely empty. Why is this?
I want to upload multiple files in php. I've on file input box and beside it i've add more button. when user clicks on button new file upload will come. user should be able to upload only five files if five file uploads control will come using add more then alert will come and user can not add another file upload. Now if i am selecting all five files and printing $_FILES array it is showing file name for first and last array index. Code given below:-
Array
(
[txtProductImage] => Array
(
[name] => Array
(
[0] => 14200127851.jpg
[1] =>
[2] =>
[3] =>
[4] => 14200127864.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] =>
[2] =>
[3] =>
[4] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\xampp\tmp\phpBCCE.tmp
[1] =>
[2] =>
[3] =>
[4] => C:\xampp\tmp\phpBCDF.tmp
)
[error] => Array
(
[0] => 0
[1] => 4
[2] => 4
[3] => 4
[4] => 0
)
[size] => Array
(
[0] => 575185
[1] => 0
[2] => 0
[3] => 0
[4] => 660387
)
)
)
I'd spent lots of time but could not able to get what the issue is. Any help will be appreciated. Thanks in advance
Can you show us more code? How are you getting files and how are you posting them? Did you checked what errors are for elements of array? You should try to print them from array to see what are the errors. This will give you inside view and where you made mistake. Maybe it's too big picture or not correct type.
this works for me.
<?php
echo '<pre>';
ob_start();
var_dump('$_GET',$_GET,'$_POST',$_POST,'$_COOKIE',$_COOKIE,'$_FILES',$_FILES);
echo htmlentities(ob_get_clean()).'</pre>';
?>
<form action="?">
<input type="file" multiple name="files[]" >
<input type="submit" value="Submit">
</form>
Edit: or if you want max 5, this also works for me:
<input type="file" name="files[]" >
<input type="file" name="files[]" >
<input type="file" name="files[]" >
<input type="file" name="files[]" >
<input type="file" name="files[]" >
check http://php.net/manual/en/features.file-upload.multiple.php
Here is my code:
HTML (Multiple Uploads):
<input type="file" name="attached_photo_1" id="attached_photo_1" />
<input type="file" name="attached_photo_2" id="attached_photo_2" />
<input type="file" name="attached_photo_3" id="attached_photo_3" />
<input type="file" name="attached_photo_4" id="attached_photo_4" />
<input type="file" name="attached_photo_5" id="attached_photo_5" />
PHP
$photo_array = array(
$_FILES['attached_photo_1'],
$_FILES['attached_photo_2'],
$_FILES['attached_photo_3'],
$_FILES['attached_photo_4'],
$_FILES['attached_photo_5']
);
Example
Now lets say someone only uploaded images for photo 1 and 2, leaving images 3, 4, and 5 with a $_FILES error code of 4 (meaning no file was uploaded).
Example Output (using <?php print_r($photo_array); ?>):
Array
(
[0] => Array
(
[name] => test.png
[type] => image/png
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/php0JwXJc
[error] => 0
[size] => 3469655
)
[1] => Array
(
[name] => test-2.jpg
[type] => image/jpeg
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpv7qFc6
[error] => 0
[size] => 1666451
)
[2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[3] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[4] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
MAIN QUESTION:
How Can I remove all of the parent array elements where the child array's [error] => 4? Thus in the example above elements [2], [3], and [4] would be deleted or unset from the array.
At least give it a try. See the following pseudocode to how to acheive this.
foreach($photo_array as $key => $values){
if(isset($key['name']))
$photo_array[$key] = $values;
}
}