【问题标题】:How to upload file in PHP if not exist如果不存在,如何在 PHP 中上传文件
【发布时间】:2015-05-19 14:37:17
【问题描述】:

我在 PHP.net 中阅读了所有相关内容,但找不到解决方案。我知道如何在 Mysql 和 PHP 中将文件上传到新行,但是如果我不上传任何内容,我不知道如何更新一行并且不删除之前上传的文件。

例如,我有这个表格:

标识 企业名称 图 1

例如:我在MYSQL中有这个:

标志:/img/logo1.png 名称:企业1 图 1:“”

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

这是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;
  }
}

【问题讨论】:

    标签: php mysql upload


    【解决方案1】:

    我解决了这个拆分 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;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 1970-01-01
      • 2017-11-19
      • 2014-03-26
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多