如何用php上传数据库和文件夹中的照片
How to upload photo in database and in folder with php
你好,我是这个论坛的新手,也是php的新手。我正在创建一个项目,我想将照片上传到数据库中,并将该照片存储在另一个文件夹中。我找到了一个教程,但它只针对他输入的用户名,而不是数据库中的用户名,例如下面显示的"member.php"。有人能帮我纠正我的PHP代码并让它为我的数据库工作吗?请有人帮我,我马上就需要。。。
在这种风格下,我的数据库被创建为
id |全名|用户名|密码|文件位置|
1|noname|malitet|3423@#4||
我想为数据库中存在的用户,例如,这个用户"malitet"已经登录,并且这个用户上传了头像。。。
这是我的php代码
文件名为upload.php
<?php
include ("connect.php");
$_SESSION['username']=malitet ;
$username = $_SESSION['username'];
if (isset($_POST['submit']))
{
//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if ($name)
{
//start upload process
$location = "avatars/$name";
move_uploaded_file($tmp_name,$location);
$query = mysql_query("UPDATE users1 SET imagelocation='$location' WHERE username='$username'");
die ("Your avatar has been uploaded!<a href='view.php'>Home</a>");
}
else
die ("Please select a file!");
}
echo "Welcome, ".$username."!<p>";
echo "Upload your image
<form action='upload.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='myfile'><input type='submit' name='submit' value='Upload'>
</form>
";
?>
这是我的connect.php
<?php
$error = "Unable to connect";
$connect = mysql_connect("localhost","root","") or die($error);
mysql_select_db('phplogin') or die ($error);
?>
这是我的看法.php
<?php
include ("connect.php");
$username = $_SESSION['username'];
$query = mysql_query("SELECT * FROM users1 WHERE username='$username'");
if (mysql_num_rows('$query')==1)
die ("User not found!");
else
{
$row = mysql_fetch_assoc($query);
$location = $row['imagelocation'];
echo "<img src='$location' width='100' height='100'>";
}
?>
这是我想上传文件的会员页面。。。或者,如果有其他方式,但我知道它很简单,如果有人能帮助我,请----member.php
<?php
session_start();
if (isset($_SESSION['username']))
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Log out</a><br><a href='changepassword.php'>Change password</a>";
else
die ("You must be logged in! <a href='index.php'>Click here</a> to return to login page");
?>
我希望你能帮助我…我在等待答案……:):):
hi有一些错误。总是在使用之前启动会话。好的使用此代码:
这是upload.php文件
<?php
session_start();
include ("connect.php");
$_SESSION['username'] = 'malitet';
$username = $_SESSION['username'];
if (isset($_POST['submit']))
{
//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if ($name)
{
//start upload process
$location = "avatars/" . $name;
move_uploaded_file($tmp_name, $location);
$sql = "UPDATE users1 SET imagelocation='$location' WHERE username='$username'";
$query = mysql_query($sql);
echo "Your avatar has been uploaded!<a href='view.php'>Home</a>";
}
else die("Please select a file!");
}
echo "Welcome, " . $username . "!<p>";
echo "Upload your image
<form action='upload.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='myfile'><input type='submit' name='submit' value='Upload'>
</form>
";
?>
========================================
这是view.php文件
<?php
include ("connect.php");
session_start();
$username = $_SESSION['username'];
$sql = "SELECT * FROM users1 WHERE username='$username'";
$query = mysql_query($sql);
if (mysql_num_rows($query) == 1)
{
$row = mysql_fetch_assoc($query);
$location = $row['imagelocation'];
echo "<h1>Welcome</h1><br/>";
echo "<img src='$location' width='100' height='100'>";
}
else
{
echo "User Not found";
}
?>
==============================================
并且connect.php文件是可以的,所以您使用以前的
<?php
$images_arr = array();
//This is the directory where images will be saved
$target_dir = "uploads/";
$target = $target_dir.$_FILES['photo']['name'];
//$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];
// Connects to your Database
mysql_connect("Localhost", "remote", "remote123") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO dbProfiles (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
//Tells you if its all ok
echo "The file ". $target_dir.$_FILES['photo']['name']. " has been uploaded, and your information has been added to the directory";
$images_arr[] = $target;
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" href="" type="image/x-icon" />
<!--iOS/android/handheld specific -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Upload multiple images create thumbnails and save path to database with php and mysql">
<meta name="keywords" content="php, mysql, thumbnail,upload image, check mime type">
<meta name="author" content="Shahrukh Khan">
<title>Upload multiple images create thumbnails and save path to database with php and mysql - thesoftwareguy</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
.files{height: 30px; margin: 10px 10px 0 0;width: 250px; }
.add{ font-size: 14px; color: #EB028F; border: none; }
.rem a{ font-size: 14px; color: #f00; border: none; }
.submit{width: 110px; height: 30px; background: #6D37B0; color: #fff;text-align: center;}
</style>
<script src="jquery-1.9.0.min.js"></script>
<script>
$(document).ready(function() {
$(".add").click(function() {
$('<div><input class="files" name="user_files[]" type="file" ><span class="rem" ><a href="javascript:void(0);" >Remove</span></div>').appendTo(".contents");
});
$('.contents').on('click', '.rem', function() {
$(this).parent("div").remove();
});
});
</script>
</head>
<body>
<form name="f1" action="index.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="image_form_submit" value="1"/>
<div id="container">
<div id="body">
<div class="mainTitle" >Upload multiple images create thumbnails and save path to database with php and mysql</div>
<div class="height20"></div>
<article>
<div class="height20"></div>
<div style="width: 380px; margin: 0 auto;">
<h3 style="text-align: center;">Image will be resized to 100px X 100px </h3>
<p>Please Enter the Band Members Name.</p>
<p> Band Member or Affiliates Name:</p>
<input type="text" name="nameMember"/>
<p>
Please Enter the Band Members Position. Example:Drums.
</p>
<p>
Band Position:
</p>
<input type="text" name="bandMember"/>
<p>
Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<p>
Please Enter any other information about the band member here.
</p>
<p>
Other Member Information:
</p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
<p>
Please Enter any other Bands the Member has been in.
</p>
<p>
Other Bands:
</p>
<input type="text" name="otherBands" size=30 />
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>
<img src="<?php echo $target; ?>" alt="" style=" width:100px; height:100px;"/>
<?php
if(!empty($images_arr)){ $count=0;
foreach($images_arr as $image_src){ $count++?>
<ul class="reorder_ul reorder-photos-list">
<li id="image_li_<?php echo $count; ?>" class="ui-sortable-handle">
<a href="javascript:void(0);" style="float:none;" class="image_link"><img src="<?php echo $image_src; ?>" alt=""style=" width:100px; height:100px;"></a>
</li>
</ul>
<?php }
}?>
</div>
</article>
</div>
</div>
<div class="height10"></div>
</form>
</body>
</html>
<?php
function errorMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
function successMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
?>
相关文章:
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 如何将乳胶配方奶粉图像保存到Asp.net中的文件夹中
- 展开和折叠文件夹
- 在ng重复循环中显示来自不同文件夹的图像
- 如何将所有JS文件连接到一个文件夹中
- 是否可以用JavaScript显示等效的文件夹对话框
- grunt上的压缩文件夹不起作用
- 如何使用javascript获取从指定文件夹创建的所有文件的名称、大小、类型和日期
- 将Windows文件路径组织到文件夹中
- 链接/按钮从子文件夹下载文件--浏览器只是浏览
- 在更改名称时,使用ajax jsp在下拉菜单中填充多个文件夹的数据
- 我应该将MVC视图特定的javascript文件放在哪个文件夹中
- 如何将JS文件从其他文件夹链接到页面
- 将文件从应用程序文件夹复制到JQM/Phonegap应用程序中的根文件夹
- 在以前的文件夹中加载脚本
- 如何在ExtJs4应用程序上为视图、存储和模型设置自定义文件夹名称
- 如何为文件夹中的照片加载更多按钮
- 逐个查看文件夹中的照片,并根据一次击键移动到特定文件夹,具体取决于击键
- 如何上传照片但文件夹不同
- 如何用php上传数据库和文件夹中的照片