如果不存在,如何在PHP中上传文件

时间:2015-05-17 09:55:19

标签: php mysql upload

我在PHP.net中对此有所了解,但我找不到解决方案。我知道如何将文件上传到Mysql和PHP中的新行,但如果我不上传任何内容,我不知道如何更新行而不删除以前上传的文件。

例如,我有这样的表格:

LOGO

Name of Business

Image 1

例如:我在MYSQL中有这个:

LOGO: /img/logo1.png

NAME: business1

IMAGE 1: ""

如果我没有在LOGO中上传任何内容并且我正在编辑此业务(留空LOGO的输入文件),我想这样做,不要上传我的徽标并设置空白(“”)。< / p>

这是PHP的代码:

define("MAX_SIZE", "2000");

function getExtension($str) {
    $i = strrpos($str, ".");
    if (!$i) {
        return "";
    }
    $l = strlen($str) - $i;
    $ext = substr($str, $i + 1, $l);
    return $ext;
}

$errors = 0;

$image = $_FILES['foto0']['name'];
$image1 = $_FILES['foto1']['name'];

if ($image) {

    $filename = stripslashes($_FILES['foto0']['name']);

    $extension = getExtension($filename);
    $extension = strtolower($extension);

    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {

        $errors = 1;
        $falloExtension = true;
    } else {

        $size = filesize($_FILES['foto0']['tmp_name']);

        if ($size > MAX_SIZE * 2024) {
            $errors = 1;
            $falloLimite = true;
        }


        $image_name = uniqid() . '.' . $extension;

        $newname = "img/detalles/" . $image_name;
        $newname2 = "img/detalles/" . $image_name;

        $copied = copy($_FILES['foto0']['tmp_name'], $newname2);

        if($copied) {
            $copiar = "UPDATE negocios SET logo='$newname' WHERE id=$numNegocio";
            $resultado = $mysqli->query($copiar);
            //header("location: ../anunciate.php");
        } else {
            //header("location: ../anunciate.php?fallo=true");
        }
        }
    }

    if ($image1) {

    $filename = stripslashes($_FILES['foto1']['name']);

    $extension = getExtension($filename);
    $extension = strtolower($extension);

    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {

        $errors = 1;
        $falloExtension = true;
    } else {


        $size = filesize($_FILES['foto1']['tmp_name']);

        if ($size > MAX_SIZE * 2024) {
            $errors = 1;
            $falloLimite = true;
        }

        $image_name = uniqid() . '.' . $extension;

        $newname = "img/detalles/" . $image_name;
        $newname2 = "img/detalles/" . $image_name;

        $copied = copy($_FILES['foto1']['tmp_name'], $newname2);

        if($copied) {
            $copiar = "UPDATE galerias SET imagen1='$newname' WHERE negocios_id=$numNegocio";
            $resultado = $mysqli->query($copiar);
            //header("location: ../anunciate.php");
        } else {
            //header("location: ../anunciate.php?fallo=true");
        }
        }
    }

  if(isset($_POST['modificanegocio'])) {
  $nombrePOST = $_POST['nombre'];
  $categoriaPOST = $_POST['categoria'];
  $direccionPOST = $_POST['direccion'];
  $telefonoPOST = $_POST['telefono'];
  $correoPOST = $_POST['correo'];
  $descripcionPOST = $_POST['descripcion'];
  $horarioPOST = $_POST['horario'];
  $paginawebPOST = $_POST['paginaweb'];
  $keywordsPOST = $_POST['keywords'];
  $latPOST = $_POST['lat'];
  $longPOST = $_POST['long'];
  $facebookPOST = $_POST['facebook'];
  $googlePOST = $_POST['google'];
  $twitterPOST = $_POST['twitter'];
  $insagramPOST = $_POST['insagram'];
  $logoPOST = $_POST['logo'];

  if($modificaNegocio = $mysqli->query("UPDATE negocios SET name = '$nombrePOST, logo = '$logoPOST', image1 = $image1POST WHERE id = $id"))  {
      $modifica = true;
  } else {
      $modifica = false;
  }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个拆分MYSQL代码:

  $query = "UPDATE negocios SET nombre = '$nombrePOST'";
  if($image != "" || $image != null) {
      //NOW I WILL UPLOAD THE IMAGE HERE AND UPLOAD THE QUERY
  }
  $query .= " WHERE id = $id";
  if($modificaNegocio = $mysqli->query($query))  {
      $modifica = true;
  } else {
      $modifica = false;
  }
}