Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file didn't uploaded using html input type file

I want to upload a file using HTML input type file, but it doesn't working. The file doesn't uploaded to the expected folder and database (i want to store the file name to the table). It gives me no error appear when I inspect from browser. When I click the "Upload" button, the url is like http://localhost/mywebsite/ajax/upload? , method GET, Status Code 200 OK. What did I do wrong?

I am using mac, xampp, phpmyadmin, chrome.

Here's my view :

<form action="<?php echo site_url('admin/upload')?>" enctype="multipart/form-data">
  <input type="file">
    <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-upload" aria-hidden="true" ></span> Upload
    </button>
</form>

The controller :

function upload()
{
if (!empty($_FILES)) 
 {
    $filename = $_FILES["file"]["name"];
    $file_basename = substr($filename, 0, strripos($filename, '.'));
    $file_ext = substr($filename, strripos($filename, '.'));
    $tempFile = $_FILES['file']['tmp_name'];
    $data = "admin".uniqid().$file_ext;
    $targetPath = getcwd() . '/kirim_undangan/';
    $targetFile = $targetPath . $data ;
    move_uploaded_file($tempFile, $targetFile);

$data_user = array(
  'request_id'  => $request_id,
  'status'      => '1',
  'filename_admin' => $uploadedFileName,
);
$this->load->model('excel');
$this->excel->upload_excel($data_user);
 }  
}

The model :

function upload_excel($allDataInSheet,$request_id)
{
  $regex = "~\d{5}~";
  array_shift($allDataInSheet);
  foreach ($allDataInSheet as $key)     
  {
     preg_match($regex, $key['B'], $result);       
     $data = array(
     'request_id'         =>   $request_id,
     'to_name'            =>   $key['A'],
     'to_phone'           =>   $key['C'],
     'to_address'         =>   $key['B'],
     'to_zipcode'         =>   $result[0], 
     'tariff'             =>   '0'
     );
  $this->db->insert('excel', $data);
  $this->db->update('request',$request_id);
  }
}
like image 704
may Avatar asked Dec 07 '25 14:12

may


1 Answers

You have to add a name to the input

<form method="post" action="<?php echo site_url('admin/upload')?>" enctype="multipart/form-data">
  <input type="file" name="file">
    <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-upload" aria-hidden="true" ></span> Upload
    </button>
</form>
like image 74
Dieter Pollier Avatar answered Dec 10 '25 03:12

Dieter Pollier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!