gpt4 book ai didi

javascript - 如果值为空则提交 file=type(upload) 错误

转载 作者:行者123 更新时间:2023-12-03 08:20:20 24 4
gpt4 key购买 nike

我刚刚意识到我的网络的弱点,我尝试在不直接输入数据的情况下上传文件,如果没有文件上传,我立即提交就会出错。

这是我的 Controller :

function upload(){
if ($this->input->post('save')) {
$fileName = $_FILES['import']['name'];

$config['upload_path'] = 'C:\files\/';
$config['file_name'] = $fileName;
$config['allowed_types'] = 'xls|xlsx|csv|ods|ots';
$config['max_size'] = 10000;

$this->load->library('upload');
$this->upload->initialize($config);

if(! $this->upload->do_upload('import') )
$this->upload->display_errors();

$media = $this->upload->data('import');
$inputFileName = 'C:\files\/'.$media['file_name'];
// Read your Excel workbook
try {
$inputFileType = IOFactory::identify($inputFileName);
$objReader = IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
};

// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 2; $row <= $highestRow; $row++){
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);

这是我的观点提交

<input type="file" id="import"  size="21" class="file-loading" name="import"  value="<?php echo set_value('import'); ?>"/>

<script type='text/javascript'>
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}
return true;
}
</script>

最佳答案

Controller 上的if ($this->input->post('save')) {之后添加if条件

public function upload(){

if ($this->input->post('save')) {

$fileName = $_FILES['import']['name'];

if($fileName){ # If $fileName exists

# And add your rest code here....
}
}
}

关于javascript - 如果值为空则提交 file=type(upload) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33773095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com