我使用CI 2.2制作了一个带有附件/文件上传的简单表单。除了在iphone safari浏览器上,一切都很好。当人们附加来自iphonesafari的图像时,文件名会变成image.jpg。这会导致来自不同用户的附件不断显示相同的图像的问题。
我使用了CI提供的文件名加密,但仍然没有成功。
这是型号:
-
从字段获取值的函数:
function check_field() { $ship_date = $this->input->post('date'); $image_data = $this->upload->data(); $field_data = [ 'ship_date' => $ship_date, 'ship_boxes' => $this->input->post('boxes'), 'ship_attach' => $image_data['file_name'], 'image_path' => $image_data['file_path'], 'ship_req_fund' => $this->input->post('option'), 'ship_amount_req' => $this->input->post('amount'), 'ship_emp_id' => $this->session->userdata('id') ]; $this->db->insert('shipment', $field_data); }
附加功能
function attachment() { $config['upload_path'] = 'uploads/'; $config['allowed_types'] = 'gif|jpg|png|zip|rar|pdf'; $config['max_size'] = '3000'; $config['max_width'] = '2000'; $config['max_height'] = '2000'; $config['encrypt_name'] = true; $this->upload->initialize($config); }
将消息发送到电子邮件:
function send_message() { $config = [ 'protocol' => 'mail', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => '*******@gmail.com', 'smtp_pass' => '*******', 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ]; $image_data = $this->upload->data(); $image_link = $image_data['client_name']; $message = '<h3>Below is the shipment data you have submitted.</h3>'; $message .= '<p><b>Shipment date</b>: ' . $this->input->post('date') . '</p>'; $message .= '<p><b>Number of boxes</b>: ' . $this->input->post('boxes') . '</p>'; if($this->upload->data()) { $message .= '<p></b>Attachment</b>: <a href="' . base_url() . 'uploads/' . $image_link . '">Click Here</a></p>'; } else { $message .= '<p></b>Attachment</b>: No attachment</p>'; } if($this->input->post('option') == 1) { $message .= '<p><b>Request fund</b>: Yes</p>'; $message .= '<p><b>Amount of funds requested</b>: $' . $this->input->post('amount') . '</p>'; } else { $message .= '<p><b>Request fund</b>: No</p>'; } $this->email->initialize($config); $this->email->set_newline("'r'n"); $this->email->from('*****@gmail.com', '*****'); $this->email->to($this->session->userdata('email')); $this->email->subject('Shipment detail'); $this->email->message($message); $this->email->send(); } }
有人在过去遇到过同样的问题,或者我把它设置错了方向?如果这真的是由浏览器引起的,有什么方法可以解决iphonesafari中的文件上传问题?
谢谢,
为名称创建一个UUID。依赖客户提供的名称是唯一的不是一个好的做法。
我用这个函数来解决你遇到的同样的问题-
<?php
get_filename($filename)
{
$fparts= explode(".",$filename);
$extension = $fparts[count($fparts)-1];
unset($fparts[count($fparts)-1]);
$filenamewoextension=implode(".", $fparts);
counter=0;
while(file_exists($filename))
{
$filename = $filenamewoextension . $counter . $extension;
}
return $filename;
}