duan3601 2014-02-11 14:56
浏览 59
已采纳

仅在选择文件时move_uploaded_file

I would appreciate if someone could help me with this issue. The problem is that i have a page where i upload an image and it´s description, but when i update the image description and keep the filefield blank when i press submit to update the description, my image disappears because i left the filefield blank, so it replaces my previous image for a blank.

Is there any way to update the description field and only update the image only if filefield is set?

Thanks in advance.

Heres my code:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

  move_uploaded_file($_FILES['imagem']['tmp_name'],"../images/slide/".($_FILES['imagem']['name']));

  $updateSQL = sprintf("UPDATE tab_imagens SET imagem=%s, titulo=%s, idUser=%s WHERE idImagem=%s",
                      GetSQLValueString("images/slide/".($_FILES['imagem']['name']), "text"),
                       GetSQLValueString($_POST['titulo'], "text"),
                       GetSQLValueString($_POST['idUser'], "text"),
                       GetSQLValueString($_POST['idImagem'], "int"));

  mysql_select_db($database_ligar, $ligar);

  $Result1 = mysql_query($updateSQL, $ligar) or die(mysql_error());
  $updateGoTo = "verImagensSlide.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

Thank you all for your responses! This is how i solved it:

//do this if there is a file in filefield
if( !empty( $_FILES[ 'imagem' ] ) && !empty( $_FILES[ 'imagem' ][ 'tmp_name' ] )){
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

      move_uploaded_file($_FILES['imagem']['tmp_name'],"../images/slide/".($_FILES['imagem']['name']));

  $updateSQL = sprintf("UPDATE tab_imagens SET imagem=%s, titulo=%s, idUser=%s WHERE idImagem=%s",
                      GetSQLValueString("images/slide/".($_FILES['imagem']['name']), "text"),
                       GetSQLValueString($_POST['titulo'], "text"),
                       GetSQLValueString($_POST['idUser'], "text"),
                       GetSQLValueString($_POST['idImagem'], "int"));


  mysql_select_db($database_ligar, $ligar);

  $Result1 = mysql_query($updateSQL, $ligar) or die(mysql_error());

  $updateGoTo = "verImagensSlide.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}}
//do this if there is NO FILE in filefield
else
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE tab_imagens SET titulo=%s, idUser=%s WHERE idImagem=%s",
                       GetSQLValueString($_POST['titulo'], "text"),
                       GetSQLValueString($_POST['idUser'], "text"),
                       GetSQLValueString($_POST['idImagem'], "int"));


  mysql_select_db($database_ligar, $ligar);

  $Result1 = mysql_query($updateSQL, $ligar) or die(mysql_error());

  $updateGoTo = "verImagensSlide.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
  • 写回答

3条回答 默认 最新

  • duanqiang9212 2014-02-11 15:04
    关注
    if( !empty( $_FILES[ 'imagem' ] ) && !empty( $_FILES[ 'imagem' ][ 'tmp_name' ] )
        move_uploaded_file($_FILES['imagem']['tmp_name'],"../images/slide/".($_FILES['imagem']['name']));
    

    should do the trick.

    Make sure you also change the SQL query by using the same if-statement. It should update the image paths and names in the table

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?