2012-11-02 24 views
0

我有一個網站在用戶上傳自己的個人資料圖片,我已經存儲了PIC文件夾中和他們聯繫,反映在數據庫 檔案相片如何顯示的是,在我有地方鏈接顯示照片?顯示用戶上傳使用PHP

上傳照片

define ("MAX_SIZE","1000"); 
    function getExtension($str) 
    { 
     $i = strrpos($str,"."); 
     if (!$i) { return ""; } 
     $l = strlen($str) - $i; 
     $ext = substr($str,$i+1,$l); 
     return $ext; 
    } 

    $errors=0; 
    $image=$_FILES['image']['name']; 

    if ($image) 
    { 

$filename = stripslashes($_FILES['image']['name']); 
$extension = getExtension($filename); 
$extension = strtolower($extension); 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") 
    && ($extension != "gif")&& ($extension != "JPG") && ($extension != "JPEG") 
    && ($extension != "PNG") && ($extension != "GIF")) 
{ 
    echo '<h3>Unknown extension!</h3>'; 
    $errors=1; 
} 
else 
{ 
    $size=filesize($_FILES['image']['tmp_name']); 

    if ($size > MAX_SIZE*1024) 
    { 
     echo '<h4>You have exceeded the size limit!</h4>'; 
     $errors=1; 
    } 

    $image_name=time().'.'.$extension; 
    $newname="photo/".$image_name; 

    $copied = copy($_FILES['image']['tmp_name'], $newname); 
    if (!$copied) 
    { 
     echo '<h3>Copy unsuccessfull!</h3>'; 
     $errors=1; 
    } 
    else echo '<h3>uploaded successfull!</h3>'; 

    //mysql_query("insert into tutor (photo) values('".$newname."')"); 
    $myphoto= ".$newname."; 
} 

代碼插入代碼

$mysqli = new mysqli("localhost", "***", "***", "***"); 
    if ($mysqli->connect_errno) { 
     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .       $mysqli->connect_error; 
    } 

    /* Prepared statement, stage 1: prepare */ 
    if (!($stmt = $mysqli->prepare("INSERT INTO `table` (`photo`) VALUES ( ?)"))) { 
     echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; 
    } 

    /* Prepared statement, stage 2: bind and execute */ 

    if (!$stmt->bind_param('s',$myphoto)) { 
     echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 

    if (!$stmt->execute()) { 
     echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 

回答

0

您需要將您的用戶以某種方式鏈接到圖片,例如添加一個外鍵,用戶表,然後插入當前用戶ID以及圖片。

在上傳/插入頁面你有類似的東西落得

// Assuming you're saving your logged in user's data in sessions 
$current_user_id = $_SESSION['user_id']; 
// Assuming you already have a $mysqli variable with an open connection 
$mysqli->exec("INSERT INTO picture_table SET user = $current_user_id, picture = '$picture_file';"); 

然後,您的個人資料/顯示頁面上,你必須使用類似的東西從數據庫中獲取它

// Assuming you already have a $mysqli variable with an open connection 
$q = $mysqli->query("SELECT picture WHERE user = $current_user_id"); 
$row = $q->fetch_object(); 

printf('<img src="http://yoursite.com/yourpath/%s" />', $row->picture); 
+0

我有用於檢索圖象$ RS碼=的mysql_query( 「選擇從導師*」); \t如果($ RS) \t \t而($行= mysql_fetch_array($ RS)) \t \t { \t \t?> \t \t

+0

問題已解決... thanx :) –