-1

这是我的 HTML 代码:

<form action="php/sp1img.php" method="post" class="form-horizontal" />
    <label class="control-label">Изображение:</label>
    <div class="controls">
        <div class="fileupload fileupload-new" data-provides="fileupload">
            <span class="btn btn-file">
                <span class="fileupload-new">Изберете файл</span>
                <span class="fileupload-exists">Промени</span>
                <input name="file" type="file" class="default" />
            </span>
            <span class="fileupload-preview"></span>
            <a href="#" class="close fileupload-exists" data-dismiss="fileupload" style="float: none"></a>
        </div>
        <button name="submit" type="submit" class="btn blue">Запази!</button>
    </div>
</form>

您可以实时预览它:http ://www.dsaidov.com/velto/admin/sponsors.php 这是我的 sp1img.php 脚本:

<?php
session_start();
$con = mysql_connect("localhost","dsaidov","denismm778");
mysql_query("SET NAMES UTF8");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("velto", $con);
$sql = mysql_query("TRUNCATE TABLE sp1img");
$qry= "INSERT INTO `sp1img` (`ImgCode`) VALUES ('". $_FILES["file"]["name"] ."')";

if (!mysql_query($qry,$con))
  {
  die('Error: ' . mysql_error());
  }   
  $files = glob('upload1/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
}
      $sql = mysql_query("TRUNCATE TABLE sp1img");
      $qry= "INSERT INTO `sp1img` (`ImgCode`) VALUES ('". $_FILES["file"]["name"] ."')";

      if (!mysql_query($qry,$con))
  {
  die('Error: ' . mysql_error());
  }
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    header("location: ../sponsors-inv.php");
    }
  else
    {
    /*echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    $sql = mysql_query("TRUNCATE TABLE sp1img");
      $qry= "INSERT INTO `sp1img` (`ImgCode`) VALUES ('". $_FILES["file"]["name"] ."')";*/

    if (file_exists("upload1/" . $_FILES["file"]["name"]))
      {
      header("location: ../sponsors-ex.php");

      $sql = mysql_query("TRUNCATE TABLE sp1img");
      $qry= "INSERT INTO `sp1img` (`ImgCode`) VALUES ('". $_FILES["file"]["name"] ."')";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload1/" . $_FILES["file"]["name"]);

      }
    }
  }
else
  {
  echo "Invalid file";
  }
  mysql_close($con);
header("location: ../sponsors.php");
exit();

mysql_close($con);
?>

我在 php 方向创建了一个名为 upload1 的文件夹,我希望在其中上传图像,我还有一个名为 sp1img 的数据库表,列名“ImgCode”,我想在其中保存图像文件名。两者都不起作用。(不上传,不更新数据库)。提前致谢。

4

1 回答 1

1

你的form标签不见了enctype='multipart/form-data'

<form action="php/sp1img.php" method="post" class="form-horizontal" enctype='multipart/form-data' />

我检查了任何其他代码,这是文件上传的第一步,如果有任何进一步的问题,请在此处发布。

于 2013-03-15T08:50:59.453 回答