Pergunta

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));
}
Foi útil?

Solução 2

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

Outras dicas

You can tell if a file was really uploaded by checking the value of the "error" field in the array where you have the details for the file.

Just the way you are using the "tmp_name" field to get the temporary name to move the uploaded file you can check on the same level if the file was successfully uploaded.

The value for that field is 0 (or better, it is the value of the constant UPLOAD_ERR_OK) if everything was ok.

So all you need to do is to wrap your call to the move_uploaded_file function in an if statement, something like this:

if (UPLOAD_ERR_OK == $_FILES['imagem']['error'])

Later edit: be sure to use a similar logic to tell if you need to update the name in the database also.

You can see that on the manual page for the move_uploaded_file function there's an example using a similar logic to tell if the upload was successful. The only difference is that in that example multiple files are uploaded and under the "error" key they have an array, while you will have a scalar (int) value.

one way would be to measure the size of the uploaded picture and if it's smaller then let's say 1KB do not move the file and update the DB

if($_FILES['file']['size'] > 1024)   // bigger then 1 KB

In this way you can also prevent toe uploading of huge files.

if (  (isset($_POST["MM_update"])) &&
      ($_POST["MM_update"] == "form1") &&
      ($_FILES['file']['size'] > 1024 )) && 
      ($_FILES['file']['size'] < 1024000 ))
{

  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));
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top