Codeigniter 2.0:使用不同名称的表单输入上传各种文件
Codeigniter 2.0: Upload various files with form inputs with different names
我在这个问题上花了几个星期,不得不向你寻求帮助,我正在使用 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)
改变这个
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 */
}
为此
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();
} else {
$this->upload->data();
}
}
/* More code */
}
我在这个问题上花了几个星期,不得不向你寻求帮助,我正在使用 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)
改变这个
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 */
}
为此
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();
} else {
$this->upload->data();
}
}
/* More code */
}