donoworuq450547191
donoworuq450547191
2013-11-16 18:00

如果数据库中没有映像,则尝试上载映像时出错

已采纳

I created a function to upload images to my db table, but it gives me an error when the image field in the table is empty, here's the code:

$consulta_img = "SELECT `poster`, `screenshot1`, `screenshot2`, `screenshot3`, `screenshot4`, `screenshot5`, `screenshot6`, `screenshot7`, `screenshot8` FROM `serie` WHERE `id`='$_POST[id_editar]'";
$imagenes = mysql_query ($consulta_img);
$imagen = mysql_fetch_array($imagenes);


function actualizar_imagen($imagen_cargada){
    $fecha=date("Y_m_d_H_i_s_");
    if($_FILES[$imagen_cargada]['name'] != ""){
        global $imagen;
        $imagen_actual = $imagen[$imagen_cargada];
        unlink("../publico/img/".$imagen_actual);
        $imagen_final = $fecha . $_FILES[$imagen_cargada]['name'];
        move_uploaded_file($_FILES[$imagen_cargada]['tmp_name'], '../publico/img/' . $imagen_final);
    }else{
        $imagen_final = $fecha . $_FILES[$imagen_cargada]['name'];
        move_uploaded_file($_FILES[$imagen_cargada]['tmp_name'], '../publico/img/' . $imagen_final);
    }
    return $imagen_final;
}

The error i'm getting back is this:

 Warning: unlink(../publico/img/2013_11_16_15_56_53_CIMG1884.JPG) [function.unlink]: No such file or directory in C:\AppServ\www\dejavu\administrador\editar.php on line 19

And it's completely logical, that image does not exist, but that means that the else is not working, and I can't figure out why.


Here is the UPDATE query part:

$campo = $_GET['img_borrar'];
$consulta_levantar_imagen = "SELECT `$campo` FROM `serie` WHERE `id`= $_GET[id_usuario_eli_img]";
$imagenes = mysql_query($consulta_levantar_imagen);
$imagen = mysql_fetch_array($imagenes);
$nombre_imagen = $imagen[$campo];

$consulta = "UPDATE `serie` SET  `$campo` = '' WHERE `id` = $_GET[id_usuario_eli_img]";
$borrar_img_ok = mysql_query($consulta);
unlink("../publico/img/".$nombre_imagen);

if($borrar_img_ok){
    header("Location:listado.php?editado=ok");
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzituo5530 dongzituo5530 8年前

    What you most likely trying to do is the following:

    function actualizar_imagen($imagen_cargada) {
        global $imagen;
    
        if ($imagen[$imagen_cargada] != '') {
            // If no value in the database
            $imagen_actual = $imagen[$imagen_cargada];
            unlink("../publico/img/".$imagen_actual);
        }
    
        // Following part can be outside if-statement, because you call it
        // wheter the image already exists or not
        $fecha=date("Y_m_d_H_i_s_");
        $imagen_final = $fecha . $_FILES[$imagen_cargada]['name'];
        move_uploaded_file($_FILES[$imagen_cargada]['tmp_name'], '../publico/img/' . $imagen_final);
    
        return $imagen_final;
    }
    

    What you currently have is

    if ($_FILES[$imagen_cargada]['name'] != "") {
         // If the name of the uploaded file is not empty
         unlink(...);
    }
    
    点赞 评论 复制链接分享

相关推荐