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条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?