这是我的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 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);
?>
我在php方向创建了一个名为upload1的文件夹,我想要上传图像,我还有一个名为sp1img的数据库表,列名为“ImgCode”,我希望保存图像文件名。两者都没有用。 (没有上传,DB中没有更新)。 提前谢谢。
答案 0 :(得分:1)
您的form
代码缺少enctype='multipart/form-data'
<form action="php/sp1img.php" method="post" class="form-horizontal" enctype='multipart/form-data' />
我dint检查了其他任何代码,这是文件上传的第一步,如果有任何进一步的问题,请在此处发布。