用一个表单上传文本和图像,用PHP将路径和文本存储在数据库中
上周我一直在写这段代码,这让我绞尽脑汁。我在论坛上到处搜索,在这个特定的主题上只能找到很少的内容 我想用一个表单上传文本和图像。图像被上传到目录(upload/),而图像路径和文本则被插入到数据库表中(upgrade.commentials)。索引、上传器php和上传文件夹都存在于www.mywebsite.com/testicials 在执行表单时,我收到一个“已连接到$ftp_服务器,用户$USERNAME已保存” 存储在:upload/“中,但未上载照片,存储在DB中的路径没有标题。但所有其他信息都会提交到数据库 我在TextWrangler中打开了文件_upload.php,它没有给我任何错误。与戈达迪一起主持 除了SQL注入的主要漏洞之外, 为什么我不能上传图片 这是我到目前为止的资料,请帮忙 文件_upload.php用一个表单上传文本和图像,用PHP将路径和文本存储在数据库中,php,image,forms,text,path,Php,Image,Forms,Text,Path,上周我一直在写这段代码,这让我绞尽脑汁。我在论坛上到处搜索,在这个特定的主题上只能找到很少的内容 我想用一个表单上传文本和图像。图像被上传到目录(upload/),而图像路径和文本则被插入到数据库表中(upgrade.commentials)。索引、上传器php和上传文件夹都存在于www.mywebsite.com/testicials 在执行表单时,我收到一个“已连接到$ftp_服务器,用户$USERNAME已保存” 存储在:upload/“中,但未上载照片,存储在DB中的路径没有标题。但所有
已解决:
下面是工作代码;将文件/图像上载到ftp目录,将路径存储在数据库表中,存储当前日期和表单中的文本—所有这些都来自一个表单
我在网上搜索了几个星期,寻找一种简洁的方法,同时在数据库的一行上提交所有这些信息。只能拼凑起来,给你们
对于初学者:
1) 在html daw中创建2个文件。Index.php和文件_upload.php。索引将是放置html的地方,文件_upload.php文件是添加php代码的地方。Php文件通常以
ID行必须设置为主键和INT。其余应设置为具有特定数量字符的Varchar(由您选择)
4) 在index.php和文件_upload.php的相同位置创建上载文件夹。请确保并添加上载文件夹的文件权限,以禁止或允许公共编辑
5) 将“我的代码”中的“”与您的网站和页面目录一起切换
在下面的例子中,upgrade是数据库名,而Condictional是表名
文件_upload.php
做了类似的事情:
HTML:
<?php
include '../controllers/session.php';
//get new add space
$querysps="INSERT INTO `advertisements`( `advertname`, `active`) VALUES ('',0);
";
require('../../database.php');
$statement = $db->prepare($querysps);
$statement->execute();
//$dummyadd = $statement->fetchAll();
$statement->closeCursor();
$newspace=$db->lastInsertId();
//echo $newspace;
?>
<?php include 'includes/navigation.php';?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Dashboard
<small>Advertisements</small>
</h1>
<ol class="breadcrumb">
<li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">View Advertisements</li>
</ol>
<ul class="">
<li><!-- search form -->
<form action="?" method="get">
<div class="input-group custom-search-form">
<?php
// echo '<label>Live Search:</label>';
$search = isset($_POST['get']) ? $_POST['get'] : '';
echo '<input type="text" class="form-control" placeholder="Search Adds" onkeyup="showResultfs(this.value)" name="search" value="' .$search .'" /><span class="input-group-btn">
<button class="btn btn-default" href="?reset" type="button">
<i class="fa fa-search"></i>
</button>';
echo ' <a class="btn tdn" href="?reset"><b>Clear</b> </a>';
echo '<br />';
//echo '<input type="submit" name="submit" value="Submit" />';
// echo '<label>No JavaScript</label>';
echo '<br /><br />';
?>
</div>
</form>
</li>
</ul>
<?php
if(empty($notifmsg)==true){
}else{
echo " <div class=\"alert alert-success alert-dismissable\">
<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">X</button>
".$notifmsg."
</div>";
}
?>
</section>
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- Custom Tabs -->
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Create Add</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form action="../controllers/addmaker.php" method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="box-body">
<input type="hidden" name="addid" value="<?php echo $newspace;?>">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Add Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="addname" id="inputEmail3" required placeholder="Add name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Air To</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="airto" required id="inputPassword3" placeholder="">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Add Picture</label>
<div class="col-sm-10">
<input class="btn btn-primary btn-sm" type="file" id="file" name="file" required accept="image/jpeg" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="clear" class="btn btn-default">Clear</button>
<!-- <button type="submit" class="btn btn-info pull-right">Create Add</button> -->
<input type="submit" name="submit" class="btn btn-info pull-right" value="submit" />
</div>
<!-- /.box-footer -->
</form>
</div>
</div>
<!-- /.col -->
</div>
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php include 'includes/footer.php'?>
<script type="text/javascript">
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
<script type="text/javascript">
function showResultfs(str)
{
if (str.length==0)
{
document.getElementById("livesearchfs").innerHTML="";
// document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("livesearchfs").innerHTML=xmlhttp.responseText;
// document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
// xmlhttp.open("GET","db-results.php?q="+str,true);
xmlhttp.open("POST","../controllers/fs-results.php?q="+str,true);
xmlhttp.send();
}
</script>
<!-- jQuery 2.2.3 -->
<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="../bootstrap/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="../plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/app.min.js"></script>
<!-- Sparkline -->
<script src="../plugins/sparkline/jquery.sparkline.min.js"></script>
<!-- jvectormap -->
<script src="../plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script src="../plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<!-- SlimScroll 1.3.0 -->
<script src="../plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- ChartJS 1.0.1 -->
<script src="../plugins/chartjs/Chart.min.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="../dist/js/pages/dashboard2.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
</body>
</html>
您的上载文件夹可写吗?你能把权限改成777吗?但不要用777生产我不记得了,但我认为666应该足够了writing@engvrdrFileZilla不让我更改权限,我明天早上会打电话给Godaddy。你们看到编码有什么问题吗?@kateobrein我认为这不是最好的方法,但应该可以。您可以查看gd扩展并重新创建映像。您的代码所在的是windows server吗?@engvrdr刚刚联系到godaddy并更改了权限,现在我收到500个错误。如果我删除enctype=“multipart/form data”500,错误消失,但我仍然会得到“文件未上载”自我报告。我的主机是windows,plesk panel。您能检查路径是否正确吗?或者,如果您的文件_upload.php位于推荐文件夹中,您可以使用uuu DIR_uuu.'/upload/“代码>以获取完整路径
<form method="post" enctype="multipart/form-data" action="/testimonials/file_upload.php">
<table>
<tr>
<td width="250">Name</td>
<td>
<input name="fname" type="text" id="fname" /><br />
</td>
</tr>
<tr>
<td width="250">Email: (will not be publicized)</td>
<td>
<input name="email" type="text" id="email" /><br />
</td>
</tr>
<tr>
<td width="250">Client Type</td>
<td id="mainselection">
<select name="type" id="type">
<option></option>
<option value="Residential">Residential</option>
<option value="Business">Business</option>
</select>
</td>
</tr>
<tr>
<td width="250">Comments</td>
<td>
<textarea id="content" name="content" rows="10" cols="50" style="border-style:groove;box-shadow: 4px 4px 4px 4px #888888;"placeholder="Please describe your experience"></textarea>
</td>
</tr>
<tr>
<td width="250">Image</td>
<td>
<input name="image" type="file" id="file">
</td>
</tr>
<tr>
<td width="250"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Testimonial">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['add']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$db_name = 'upgrade';
$tbl_name = 'Testimonials';
$ftp_user = '';
$ftp_pass = '';
$ftp_server = "";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_user, $ftp_pass);
// check connection
if ((!$ftp_conn) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$Fname = $_POST['fname'];
$Email = $_POST['email'];
$Content = $_POST['content'];
$Type = $_POST['type'];
$uploadDir = 'http://www.yourwebsite.com/directory/'.'upload/';
$fileName = $_FILES['image']['name'];
$filePath = $uploadDir . $fileName;
if(move_uploaded_file($_FILES["image"]["tmp_name"],"upload/".$_FILES["image"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "INSERT INTO $tbl_name(fname,email,content,image,type,submission_date) VALUES ('$Fname','$Email','$Content','$filePath','$Type',curdate())";
if(mysql_query($query_image))
{
echo "Stored in: " . "upload/" . $_FILES["image"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
else{echo 'File not uploaded';}
}
?>
<form method="post" enctype="multipart/form-data" action="/testimonials/file_upload.php">
<table>
<tr>
<td width="250">Name</td>
<td>
<input name="fname" type="text" id="fname" /><br />
</td>
</tr>
<tr>
<td width="250">Email: (will not be publicized)</td>
<td>
<input name="email" type="text" id="email" /><br />
</td>
</tr>
<tr>
<td width="250">Client Type</td>
<td id="mainselection">
<select name="type" id="type">
<option></option>
<option value="Residential">Residential</option>
<option value="Business">Business</option>
</select>
</td>
</tr>
<tr>
<td width="250">Comments</td>
<td>
<textarea id="content" name="content" rows="10" cols="50" style="border-style:groove;box-shadow: 4px 4px 4px 4px #888888;"placeholder="Please describe your experience"></textarea>
</td>
</tr>
<tr>
<td width="250">Image</td>
<td>
<input name="image" type="file" id="file">
</td>
</tr>
<tr>
<td width="250"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Testimonial">
</td>
</tr>
</table>
</form>
<?php
include '../controllers/session.php';
//get new add space
$querysps="INSERT INTO `advertisements`( `advertname`, `active`) VALUES ('',0);
";
require('../../database.php');
$statement = $db->prepare($querysps);
$statement->execute();
//$dummyadd = $statement->fetchAll();
$statement->closeCursor();
$newspace=$db->lastInsertId();
//echo $newspace;
?>
<?php include 'includes/navigation.php';?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Dashboard
<small>Advertisements</small>
</h1>
<ol class="breadcrumb">
<li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">View Advertisements</li>
</ol>
<ul class="">
<li><!-- search form -->
<form action="?" method="get">
<div class="input-group custom-search-form">
<?php
// echo '<label>Live Search:</label>';
$search = isset($_POST['get']) ? $_POST['get'] : '';
echo '<input type="text" class="form-control" placeholder="Search Adds" onkeyup="showResultfs(this.value)" name="search" value="' .$search .'" /><span class="input-group-btn">
<button class="btn btn-default" href="?reset" type="button">
<i class="fa fa-search"></i>
</button>';
echo ' <a class="btn tdn" href="?reset"><b>Clear</b> </a>';
echo '<br />';
//echo '<input type="submit" name="submit" value="Submit" />';
// echo '<label>No JavaScript</label>';
echo '<br /><br />';
?>
</div>
</form>
</li>
</ul>
<?php
if(empty($notifmsg)==true){
}else{
echo " <div class=\"alert alert-success alert-dismissable\">
<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">X</button>
".$notifmsg."
</div>";
}
?>
</section>
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- Custom Tabs -->
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Create Add</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form action="../controllers/addmaker.php" method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="box-body">
<input type="hidden" name="addid" value="<?php echo $newspace;?>">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Add Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="addname" id="inputEmail3" required placeholder="Add name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Air To</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="airto" required id="inputPassword3" placeholder="">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Add Picture</label>
<div class="col-sm-10">
<input class="btn btn-primary btn-sm" type="file" id="file" name="file" required accept="image/jpeg" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="clear" class="btn btn-default">Clear</button>
<!-- <button type="submit" class="btn btn-info pull-right">Create Add</button> -->
<input type="submit" name="submit" class="btn btn-info pull-right" value="submit" />
</div>
<!-- /.box-footer -->
</form>
</div>
</div>
<!-- /.col -->
</div>
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php include 'includes/footer.php'?>
<script type="text/javascript">
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
<script type="text/javascript">
function showResultfs(str)
{
if (str.length==0)
{
document.getElementById("livesearchfs").innerHTML="";
// document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("livesearchfs").innerHTML=xmlhttp.responseText;
// document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
// xmlhttp.open("GET","db-results.php?q="+str,true);
xmlhttp.open("POST","../controllers/fs-results.php?q="+str,true);
xmlhttp.send();
}
</script>
<!-- jQuery 2.2.3 -->
<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="../bootstrap/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="../plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/app.min.js"></script>
<!-- Sparkline -->
<script src="../plugins/sparkline/jquery.sparkline.min.js"></script>
<!-- jvectormap -->
<script src="../plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script src="../plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<!-- SlimScroll 1.3.0 -->
<script src="../plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- ChartJS 1.0.1 -->
<script src="../plugins/chartjs/Chart.min.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="../dist/js/pages/dashboard2.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
</body>
</html>
<?php
include 'session.php';
$addid=$_POST['addid'];
$addname=$_POST['addname'];
$airto=$_POST['airto'];
$newDate = date('Y-m-d H:i:s', strtotime($airto));
$query="UPDATE `advertisements` SET `advertname`='$addname',`aituntill`='$newDate',`active`=1 WHERE `advertid`=$addid";
echo $query."<br>";
saveadddata($query);
handlepic($addid);
function saveadddata($query){
require '../../database.php';
$statement = $db->prepare($query);
$statement->execute();
$statement->closeCursor();
}
function handlepic($addid){
echo "In pic maker"."<br>";
if (isset($_POST['submit'])==true)
{
echo "If passed"."<br>";
$userid=$addid;
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.doc','.docx','.rtf','.pdf','.jpg','.jpeg');
$pathholder="../../adds/img/".$userid;
echo $pathholder."<br>";
if (in_array($file_ext,$allowed_file_types) && ($filesize < 5000000))
{
//$pathholder="Ruerenamed";
if (!file_exists($pathholder)) {
mkdir($pathholder, 0777, true);
}
//make new directory
//mkdir("$pathholder");
// Rename file
$newfilename = $userid . $file_ext;
// if (file_exists("uploads/" . $newfilename))
// {
// // file already exists error
// echo "You have already uploaded this file.";
// }
// else
// {
move_uploaded_file($_FILES["file"]["tmp_name"], "$pathholder/" . $newfilename);
//echo "File ".$pathholder."/".$newfilename." uploaded successfully.";
$_SESSION['serverFeedback']="Advertisement created! ";
header("Location: ../pages/index.php");
//include 'ownerprofile.php';
//}
}
elseif (empty($file_basename))
{
$newfilename = $userid.".jpg";
//$pathholder="Ruerenamed";
// file selection error
if (!file_exists($pathholder)) {
mkdir($pathholder, 0777, true);
}
$file = '../../defaultpictures/me.jpg';
$newfile = $pathholder."/".$newfilename;
if (!copy($file, $newfile)) {
// echo "failed to copy". $file."into ". $newfile;
}else{
// echo "copied ".$file ."into ". $newfile;
}
//echo "Please select a file to upload.";
$_SESSION['serverFeedback']="Advertisement created with default! ";
header("Location: ../pages/index.php");
//include 'ownerprofile.php';
}
elseif ($filesize > 5000000)
{
// file size error
//echo "The file you are trying to upload is too large.";
$_SESSION['serverFeedback']="Advertisement picture is too large to upload! ";
header("Location: ../pages/index.php");
//include 'ownerprofile.php';
}
else
{
// file type error
//echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
unlink($_FILES["file"]["tmp_name"]);
$_SESSION['serverFeedback']="Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
header("Location: ../pages/index.php");
//include 'ownerprofile.php';
}
}else {
echo "If failed"."<br>";
}
}
?>