简体   繁体   中英

Uploading images. Cannot make it work. PHP/MySQL

this is my HTML code:

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

You can preview it live on: http://www.dsaidov.com/velto/admin/sponsors.php And here is my sp1img.php scirpt:

<?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);
?>

I have a folder created in the php direction called upload1 where I want the image to be uploaded, I also have a DB Table called sp1img with Column name "ImgCode" where I want to be saved the image file name. Both, are not working. (No uploading, no updating in the DB). Thanks in advance.

Your form tag missing enctype='multipart/form-data'

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

I dint checked the any other code, this is the first step of the file upload, post here if any further issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM