i am trying to use the File upload feature in my application ..
i am having a Form with many fields of type Text,textarea and including File upload FIeld.
I have kept a Submit button at the end of the Form which on click will submit the actual value of the textbox /textarea and even the value for the Field of type File upload.
How to get the actual file that is uploaded and to save it in a location so that i can view the uploaded file later on ..
The code that i have used is,
Edit :
i have added enctype in the Form tag but not working while submission
<form method="post" action="/FormBuilder/index.php/forms/submit/93/13" id="ResultSubmit" enctype="multipart/form-data">
<div class="input file">
<label for="276">Choose Ur File To Upload</label>
<input type="file" value="" style="width: 400px;" id="276" name="Choose ur file to upload"/>
</div><br/>
<div class="input text">
<label for="277">Name</label>
<input type="text" value="" style="width: 200px;" id="277" name="Name"/>
</div> <br/>
<div class="submit"><input type="submit" value="submit"/></div>
</form>
Action Submit in the Cakephp controller is
function submit($formid = null,$fillerid=null)
{
$this->data['Result']['form_id']=$formid;
$this->data['Result']['submitter_id']=$fillerid;
$this->data['Result']['submitter']=$this->Session->read('filler');
echo "submitter: ".$this->Session->read('filler');
$results=$this->Form->hasResults($this->data);
echo http_build_query($_POST);
if(empty($results)){
foreach ($_POST as $key => $value):
if(is_array($value)){
$value = implode('', $_POST[$key]);
$this->data['Result']['value']=$value;
}
else{
$this->data['Result']['value']=$value;
}
$this->data['Result']['form_id']=$formid;
$this->data['Result']['submitter_id']=$fillerid;
$this->data['Result']['label']=Inflector::humanize($key);
$this->data['Result']['submitter']=$this->Session->read('filler');
$this->Form->submitForm($this->data);
endforeach;
$this->Session->setFlash('Your entry has been submitted.');
$this->Invite->updateAll(array('Invite.filled'=>"'Yes'"),array('Invite.id'=>"$fillerid"));
}else{
$this->Session->setFlash('You have already filled the Form .');
}
}
In /app/models/upload.ctp:
function beforeSave()
{
if (!empty($this->data['Upload']['File']) && is_uploaded_file($this->data['Upload']['File']['tmp_name']))
{
if (!move_uploaded_file($this->data['Upload']['File']['tmp_name'], 'some_location/' . $this->data['Upload']['File']['name']))
{
return false;
}
$this->data['Upload']['name'] = $this->data['Upload']['File']['name'];
$this->data['Upload']['type'] = $this->data['Upload']['File']['type'];
$this->data['Upload']['size'] = $this->data['Upload']['File']['size'];
}
return true;
}
In /app/controllers/uploads_controller.php:
function add()
{
if (!empty($this->data))
{
if ($this->Upload->save($this->data))
{
$this->Session->setFlash('File upload successful.');
$this->redirect('/uploads');
}
}
}
In app/views/uploads/add.ctp:
echo $form->create('Upload');
echo $form->input('Upload.File', array('type' => 'file'));
echo $form->submit('Upload the file');
echo $form->end();
Related
I have a form to edit product_Details, after I press save button it run updateProduct.php which is update product_detail table successfully, the problem is : success message displayed on a new page, I just want it to be displayed beside save button, please advice and I thank you very much
here is code of my form:
<form action = "updateproduct.php">
<!-- \\\\\\\\\\\\\\\\\\\START Product Name\\\\\\\\\\\\\\\\\\\\ -->
<div class="mb-3 mt-3">
<label for="product_id" class="form-label">Product Name</label>
<?php $strquery2="SELECT Name, ID FROM products WHERE ID='". htmlspecialchars($row['product_id'])."'";
$result2 = mysqli_query($conn, $strquery2);
$row2 = mysqli_fetch_array($result2)
?>
<input type="text-area" class="form-control" name="productname" value= <?= htmlspecialchars($row2['Name']) ?>>
<input type="text" class="form-control" name="ID" value= <?= htmlspecialchars($row2['ID']) ?> hidden>
</div>
<!-- \\\\\\\\\\\\\\\\\\\END Product Name\\\\\\\\\\\\\\\\\\\\ -->
<div class="mb-3 mt-3">
<label for="discription" class="form-label">Discription</label>
<input type="text" class="form-control" name="discription" value= "<?= htmlspecialchars($row['discription']) ?>">
</div>
<input type="submit" value="Save">
</form>
Bellow is my updateproduct.php file
<?php $mysqli = new mysqli("localhost", "root", "", "test")
or die('Error connecting to MySQL server.');
$id=mysqli_real_escape_string($mysqli, $_REQUEST['ID']);
$comments=mysqli_real_escape_string($mysqli, $_REQUEST['discription']);
$query= "UPDATE product_details SET
discription = '$comments'
WHERE product_id='$id'";
if(mysqli_query($mysqli, $query)){
echo '<script>alert("Product added successfully")</script>';
}
else{
echo "ERROR: Hush! Sorry $sql. "
. mysqli_error($conn);
}
// Close connection
mysqli_close($mysqli);
?>
im new to php. Recently im doing a page where you can create a miniblog and uplolad a file. But when i click the submit button, theres error that said "Unable to load File class" can anyone help me to see where i do wrong? thankyou in advance.
this is my form (view) code where user submit their files
<form enctype="multipart/form-data" action = "<?= base_url().'admin/blog/addblog_post' ?>" method="post">
<div class="form-group">
<input type="file" class="form-control" name="file" placeholder="Add a file">
</div>
<br>
<button type="submit" class="btn btn-primary">Add mini blog</button>
</form>
This one is from the controller where the addblog function is located
function addblog_post()
{
print_r($_POST);
print_r($_FILES);
if($_FILES)
{
//Image can be uploaded
$config['upload_path'] = './assets/uploads/blogs';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('file', $config);
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
die("Error");
// $this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo "<pre>";
print_r($data);
// $this->load->view('upload_success', $data);
}
}
else
{
//Image cannot passed
}
}
There's no library with the name "file" in CI
if you want to upload a file you can make use of File upload library
change
$this->load->library('file', $config);
to
$this->load->library('upload', $config);
for more info go through Libraries list and File Upload Library
I'm trying to upload multiple images to my database and then show it on another page, but when I upload them, I can see only one image and not the rest.
This is my html
<div class="form-group">
<label for="inputGroupFile01">Upload foto's</label>
<input type="file" name="image" class="form-control-file" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01" multiple>
</div>
and this is my controller to save it
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalExtension();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$request->file('image')->move('storage/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
And this is my code to show it
<a download="retourmelding_{{$retour->firmaname}}" href="{{ asset('storage/images/'.$retour->images) }}" title="Foto">
<img alt="Foto" src="{{ asset('storage/images/'.$retour->images) }}"></a>
So uploading works perfectly, I also made my input multiple, and when I choose images only the first image is uploaded to my database, and that is the only picture I can see.
Where is the problem?
1) Add enctype="multipart/form-data" to your form tag
<form method="post" action="YOUR URL GOES HERE" enctype="multipart/form-data">
{{csrf_field()}}
//...
</form>
2) The name of your file input must be image[]
<input type="file" name="image[]" class="form-control-file" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01" multiple>
3) Backend Validation
$this->validate($request, [
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
4) Insert multiple images in the database
//...
if($request->hasfile('image'))
{
foreach($request->file('image') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$myModel = new App\MyModel();
$myModel->filename = json_encode($data);
$myModel->save();
//...
Sorry I am new to WordPress and still having difficulty to retrieve my data from my custom database. So far this is my code for submitting data to my wp_datareg table.
<?php
/*
Template Name: Data-Register
*/
get_header();
?>
<?php
If($_POST['Submit']) {
global $wpdb;
$Data1=$_POST['Data1'];
$Data2=$_POST['Data2'];
if($wpdb->insert(
'wp_datareg',
array(
'Data1' => $Data1,
'Data2' => $Data2
)
) == false) wp_die('Database Insertion Failed');
else echo '<h2>Database Insertion Succesful</h2><p />';
?>
<?php
}
else // else we didn't submit the form so display the form
{
?>
<
<h4>Data Registration Form</h4>
<form action="" method="post" id="addcourse">
<p><label>Put Data1:<input type="text" name="Data1" size="30" /></label></p>
<p><label>Put Data2: <input type="text" name="Data2" size="30" /></label></p>
</div>
<input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
</form>
What I want to add is put another text form and search button where user can search a data of Data1 and edit its value on the form, (please see the image)
Sorry for being such a noob.
PLEASE SEE THIS IMAGE
Thank you.
br
We can fetch data from wp_datareg table using this code, however putting back the data to the form is my problem.
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_datareg" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->Data1;?></td>
</tr>
I have a form with a file input field, I created a label for the input and made the input display none. and whenever I upload pictures it automatically submits the form. But the problem is it return the wrong ID with the link if I submit like this, if I just have a normal submit buttons it works.
#foreach($wedstrijden as $wedstrijd)
<a class="verslag-manager-button" data-toggle="modal" data-target="#verslagModal{{ $wedstrijd->id }}" href="">
<p>Verslag </p>
</a>
--modal #verslagModal{{ $wedstrijd->id }}
<form action="/manager/fotos/upload/{{ $wedstrijd->id }}" class="upload" method="post" enctype="multipart/form-data">
<label class="btn btn-default btn-success" for="my-file-selector">
<input onchange="this.form.submit()" id="my-file-selector" multiple name="sourceImage[]" type="file" style="display:none;">
Upload Foto
</label>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input style="display: none" type="submit" value="Upload Image" name="submitBtn">
</form>
--modal
#endoforeach
In my route file I have the following route
Route::post('manager/fotos/upload/{id}', 'managerController#postFotoUpload');
But in my controller it always returns the latest id.
if I inspect my code in the browser the form url shows /manager/fotos/upload/16 but if I dump the $id in my controller it return 17.
public function postFotoUpload($id, Request $request)
{
foreach($request->file('sourceImage') as $foto)
{
$file = $foto;
$destinationPath = 'img/' . $id . '/';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$foto = new Foto();
$foto->wedstrijd_id = $id;
$foto->foto = $filename;
$foto->save();
}
Session::flash('upload', true);
Return Redirect::to('/manager');
}