简体   繁体   中英

How to upload photo in database and in folder with php


Hello, I'm new in this forum ande I'm new in php. I'm creating one project and i want to upload photo into database and that photo to be stored in another folder. I found one tutorial, but it talks just for the username which he entered, not the username that is in database for example in the "member.php" shown below . Can someone help me by correcting my PHP code and to make it work for my database? Please someone let help me, i need it immidiately...

In this style my database is created


id | fullname | username | password | filelocation |


1 | noname | malitet | 3423@!#4 | |


i want for users that exist on the database, for example this user "malitet" is logged in and this user to upload avatar...

Here is my php code

the file name is 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>

";

?>

here is my connect.php

<?php
$error = "Unable to connect";
$connect = mysql_connect("localhost","root","") or die($error);
mysql_select_db('phplogin') or die ($error);
?>

here is my view.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'>";

}   

?>

this is member page into which i want to upload file...or if there exist any other way but i know its very simple, if someone can help me pls---- 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");

?>

I hope that you will help me...I'm waiting for answer...:):):)

  <?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>';
}
?>

hi there is some error . always start session before use it .Ok Use this code :

this is upload.php file

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

";
?>

================================================

this is view.php file

<?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";
}
?>

===============================================

and connect.php file is ok so you use your previous one

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