2012-01-05 21 views
1

你好,我是新的在這個論壇ande我是新的php。我正在創建一個項目,並且我想要將照片上傳到數據庫中,並將該照片存儲在另一個文件夾中。我找到了一個教程,但它只是爲了他輸入的用戶名,而不是在數據庫中的用戶名,例如在下面顯示的「member.php」中。有人可以通過更正我的PHP代碼並使其適用於我的數據庫來幫助我嗎?請讓別人幫我,我需要它immidiately ...如何使用php上傳數據庫和文件夾中的照片

在這種風格我的數據庫中創建


ID |全名|用戶名|密碼| filelocation |


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

這裏是我的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'>"; 

} 

?> 

這是到了我要上傳的文件會員頁面...或者,如果存在任何其他的方式,但我知道它很簡單,如果有人能幫助我請---- 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"); 

?> 

我希望你能幫助我......我在等待答案... :) :) :)

+0

有有因爲它的立場,我可以通過它所有這些代碼的好一些問題,但我覺得真的是你應該嘗試找到一個很好的PHP資源來學習PHP。如果你使用這個代碼,你的系統將會有幾個非常大的漏洞,這樣用戶可以做很少量的探測來爲你的服務器或者數據庫做任何事情。 – PlexQ 2012-01-05 03:37:42

+0

我知道,但我只是在開始,需要這種形式,後來我會嘗試改變,或者如果你有一些PHP資源學習,你可以發送給我嗎?但如果你在這幫助我,我會非常感謝你...... – malitet 2012-01-05 03:51:25

回答

-1

喜有一些錯誤。 總是使用前啓動會話它.Ok 使用此代碼:

這是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文件是確定你的前一個

+0

Thnx很多,這是有幫助的,但我有一個問題,如果我Imowed:)....如何使$ _SESSION [ 'username'] ='malitet'; - 代替「malitet」找到已登錄的用戶,只有他上傳圖片...不是我寫的名字... $ username = $ _SESSION ['username'];' – malitet 2012-01-05 12:39:51

+0

這仍然是非常糟糕的代碼。您不能使用用戶特定的圖像名稱作爲本地圖像名稱。您必須在使用前轉義用戶提供的所有參數,特別是在SQL查詢和文件名中。在你自己的極端危險中使用它。 upload.php中也有一個多行字符串,它不會運行。 – PlexQ 2012-01-05 14:20:05

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