gpt4 book ai didi

php - 验证是否选择了要在 codeigniter 中上传的文件

转载 作者:可可西里 更新时间:2023-11-01 13:10:22 24 4
gpt4 key购买 nike

您好,我在上传文件部分遇到了一些问题。基本上我有一个带有上传标志部分的表格,如果用户没有选择标志,他/她仍然可以同时更新他/她的个人资料,用户也可以选择上传标志,但它有一些限制。现在我不知道如何验证该部分以了解他/她是否选择了图像。到目前为止,这是我在 CI Controller 中的代码:

if (//CHECK IF THERE IS A FILE FOR UPLOAD){
$new_name = $data['id'];

$config['upload_path'] = './Logos/';
$config['allowed_types'] = 'jpg|png|gif';
$config['max_size'] = '100';
$config['max_width'] = '100';
$config['max_height'] = '100';

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

if ( ! $this->upload->do_upload())
{
array_push($data['error_message'], "You have the following errors in your entry:\n");
array_push($data['error_message'], "- Logo must be 100x100 pixels in size not exceeding 100KB with JPG,PNG or GIF format");
array_push($data['error_message'], "\nLogo upload failed.");
$data['check_error'] = true;
}
else
{
$data = array('upload_data' => $this->upload->data());

//DO UPDATE PART HERE
$file = $data['upload_data']['file_name'];
rename($config['upload_path'] . $file, $config['upload_path'] .$new_name.'.jpg');
//GO TO SETTINGS
$this->load->helper('url');
redirect($data['base'].'settings');
}
}

if 中的部分是我想设置验证的地方。我尝试了一些技巧,但所有这些都无法正常工作。

这是我的 View 部分,如果某个 ID 的 Logo 已经存在,则只显示他/她的 Logo ,否则显示默认 Logo :

 <tr>
<td><p class="titles">Logo</p></td>
<td>
<div>
<input type="text" id="fileName" class="file_input_textbox" readonly="readonly">

<div class="file_input_div">
<input type="button" value="Browse" class="file_input_button" />
<input type="file" class="file_input_hidden" onchange="javascript: document.getElementById('fileName').value = this.value" id="upload" name="userfile" />
</div>
</div>
</td>
</tr>

<tr>
<td>
</td>
<td>
<p class="titles">100px x 100px jpg, png or gif only.</p>
</td>
</tr>

<tr>
<td><p class="titles">Current Logo</p></td>
<td>
<img src="<?php if(is_array(@getimagesize($base."Logos/".$id.".jpg"))){echo $base."Logos/".$id.".jpg";}else{echo $base."Logos/default.jpg";} ?>" style="margin:0px 0px 0px 90px;"/>
</td>
</tr>

最佳答案

在获取 CI 的上传类来完成这项工作之前,我通常使用 native PHP 来检查文件是否正在上传:

if (isset($_FILES['upload_field_name']) && is_uploaded_file($_FILES['upload_field_name']['tmp_name'])) {
//load upload class with the config, etc...
$this->load->library('upload', $config);
}

关于php - 验证是否选择了要在 codeigniter 中上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13287931/

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