-4
我想讓用戶上傳個人資料圖片!用php和SQL上傳圖片
我想上傳文件(圖片),將字符串/路徑和文件名存儲在我的數據庫中,並將文件移動到我的圖像文件夾!但我沒有分類如何做到這一點!
我有幾個文件,你可以看看,看看有什麼我試圖做...
picUpload.php(代碼上傳圖片)
<?php
if(($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 50000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return code: " . $_FILES["file"]["error"] . "<br/>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br/>";
echo "Size: " . $_FILES["file"]["size"]/1024 . " Kb<br/>";
if(file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists!";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file!";
}
?>
然後profile.php(其中用戶上載的知情同意並存儲在DB ???)
<?php
//create_cat.php
include 'connect.php';
include 'header.php';
if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1)
{
//the user is not an admin
echo '<br/>';
echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> to view your profile.';
echo '<br/><br/>';
}
else
{
$sql = mysql_query("SELECT * FROM users");
if ($users = mysql_fetch_array($sql)){
echo 'Profile of '.$_SESSION['user_name'];
echo '<br/><br/>';
echo '<h2>Info about you:</h2>';
echo '<br/>';
echo 'Logged in as '.$_SESSION['user_name'];
echo '<br/><br/>';
echo 'Your email address: '.$_SESSION['user_email'].' (1 = admin || 0 = user)';
echo '<br/><br/>';
echo 'Your user level: '.$_SESSION['user_level'].' (1 = admin || 0 = user)';
echo '<br/><br/>';
echo 'Your friends: //friends come here';
echo '<br/><br/>';
}
//////////////////////////////////////////////////////////////////////////////
//////////////////// STORE STRING IN DATABASE????? //////////////////
//////////////////////////////////////////////////////////////////////////////
echo 'Do you want a profile picture? Uplaod image!';
echo '<form action="picUpload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename: </label>
<input type="file" name="file" id="file"/>
<br />
<input type="submit" name="submit" value="Submit" />
</form>';
/////////////////////////////////////////////////////////////
//HOW DO I INSERT/store the string/path in my database now???
/////////////////////////////////////////////////////////////
$sql = "INSERT INTO users('pic_location')
VALUES('" . mysql_real_escape_string($_POST['pic_location']) . "',NOW(),0)";
/*$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'Something went wrong while registering. Please try again later.';
//echo mysql_error(); //debugging purposes, uncomment when needed
}
else
{
echo 'Succesfully registered. You can now <a href="signin.php">sign in</a> and start sharing links.';
}*/
//echo 'Welcome, ' . $_SESSION['user_name'] . '! <br /><br/><br/><a href="index.php"><b>Proceed to the link sharing</b></a>.';
///////////////////////////////
/// adding users as friends ///
///////////////////////////////
//while($user = mysql_fetch_array($result))
//echo $user['user_name'].'
//<a href="addfriend.php?user='.$user['id'].'">ADD TO FRIENDS</a><br/>';
//NOW I WANT TO MAKE A SPECIFIC "ADD AS FRIEND" LINK NEXT TO EACH USER
}
include 'footer.php';
?>
AN D如何將文件移動到我的圖像文件夾? 請幫助我這個!
謝謝!
您發佈的代碼有什麼問題?你有什麼具體問題? – Herbert
看看這個(http://www.w3schools.com/php/php_file_upload.asp) – ravi404
我上傳時出現這個錯誤! –