【发布时间】:2015-05-07 12:49:24
【问题描述】:
我花了几个星期来解决这个问题,不得不向你寻求帮助,我正在使用 Codeigniter 2.0,并从 autoload.php 加载 file_upload 库,但由于某种原因无法上传文件,并且在我执行do_upload 函数时没有显示任何错误,这是我拥有的很多小代码(我输入了 9 个文件,为了您的方便,在这个例子中只显示了一个)。
查看
<?php
$atributos = array(
"id" => "form_anuncio",
"name" => "form_anuncio"
);
echo form_open_multipart("usuarios/GrabarAnuncio",$atributos);
?>
<div class="form-group">
<label for="logotipo">Logotipo
<input type="file" name="logotipo" id="logotipo" placeholder="Logotipo" />
</label>
<?php if (($datos_usuario["Logotipo"] != NULL) || ($datos_usuario["Logotipo"] != "")) { ?>
<img src="<?php echo base_url(); ?>images/logos/<?php echo $datos_usuario["idusuario"] . "_" . $datos_usuario["Logotipo"] ?>" alt="Logotipo Usuario" class="img-responsive" />
<?php } ?>
</div>
<div class="form-group">
<input type="submit" name="GuardarAnuncio" id="GuardarAnuncio" value="Guardar Anuncio" />
</div>
<?php echo form_close(); ?>
控制器
public function GrabarAnuncio()
{
$this->load->model('Usuarios_model');
$this->Usuarios_model->Grabar_anuncio();
$this->miperfil();
}
模型(只是摘录)
function Grabar_anuncio()
{
/* Lot of code */
$directorio = './images/';
$directorio1 = './images/logos/' . $ID . '/';
if (!is_dir($directorio1)) {
mkdir($directorio1, 0777, true);
}
$directorio2 = './images/fotos/' . $ID . '/';
if (!is_dir($directorio2)) {
mkdir($directorio2, 0777, true);
}
$directorio3 = './images/galerias/' . $ID . '/';
if (!is_dir($directorio3)) {
mkdir($directorio3, 0777, true);
}
if ($_FILES['logotipo']['name'] != "")
{
$config['upload_path'] = $directorio1;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = true;
$config['file_name'] = $_FILES['logotipo']['name'];
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('logotipo'))
{
//Never Enter Here
$serverError = true;
print_r($this->upload->display_errors());
exit();
}
}
/* More code */
}
如果您需要更多详细信息,请尽管询问,希望您能帮助我
更新:
这是我在上传前执行 var_dump 得到的结果
array (size=9)
'logotipo' =>
array (size=5)
'name' => string 'grinch1.png' (length=11)
'type' => string 'image/png' (length=9)
'tmp_name' => string 'G:\wamp\tmp\php5E16.tmp' (length=23)
'error' => int 0
'size' => int 58066
'FotosLocalizacion1' =>
array (size=5)
'name' => string 'anviz1.jpg' (length=10)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'G:\wamp\tmp\php5E46.tmp' (length=23)
'error' => int 0
'size' => int 219877
'FotosLocalizacion2' =>
array (size=5)
'name' => string 'anviz2.jpg' (length=10)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'G:\wamp\tmp\php5E47.tmp' (length=23)
'error' => int 0
'size' => int 172127
'FotosLocalizacion3' =>
array (size=5)
'name' => string 'anviz3.jpg' (length=10)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'G:\wamp\tmp\php5E57.tmp' (length=23)
'error' => int 0
'size' => int 136494
'FotosLocalizacion4' =>
array (size=5)
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'galeria1' =>
array (size=5)
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'galeria2' =>
array (size=5)
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'galeria3' =>
array (size=5)
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'galeria4' =>
array (size=5)
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
更新 2
在 if 语句之后(括号外)执行此操作:die(var_dump($this->upload->data()));
array (size=14)
'file_name' => string 'grinch1.png' (length=11)
'file_type' => string 'image/png' (length=9)
'file_path' => string 'G:/wamp/www/serviciosycomprasx/images/logos/127/' (length=48)
'full_path' => string 'G:/wamp/www/serviciosycomprasx/images/logos/127/grinch1.png' (length=59)
'raw_name' => string 'grinch1' (length=7)
'orig_name' => string 'grinch1.png' (length=11)
'client_name' => string 'grinch1.png' (length=11)
'file_ext' => string '.png' (length=4)
'file_size' => float 56.71
'is_image' => boolean true
'image_width' => int 280
'image_height' => int 280
'image_type' => string 'png' (length=3)
'image_size_str' => string 'width="280" height="280"' (length=24)
【问题讨论】:
-
您可能需要在上传时使用 for ($i=0; $i
-
@mustang83 你是什么意思?你有例子吗?
-
我建议在到达
do_upload之前使用die()和var_dump()来调试正在发生的事情。例如,要确保您的$directorio3完全符合您的预期,您可以使用:die(var_dump($directorio3));,这将为您提供变量的值。如果您没有看到任何输出,则意味着您需要追溯到问题所在。 -
我已经尝试过并仔细检查了目录创建,我认为错误在这一行“如果(!$this->upload->do_upload('logotipo'))”,因为我有在我的文件输入表单中,我认为用户文件的名称与@VincentWilkie 不同
-
如果你在
if ( ! $this->upload->do_upload('logotipo'))之前去die(var_dump($_FILES));会发生什么?
标签: php codeigniter file-upload upload codeigniter-2