Uploading picture with php and SQL
I want the user to upload a profile picture!
I want to upload the file (picture), store the string/path and filename in my db and move the file to my images folder! BUT I DON'T HAVE A CLUE HOW TO DO THIS!
I have a couple of files you can have a look at and see what I'm trying to do...
picUpload.php (code that uploads the picture)
<?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/" . 开发者_JAVA技巧$_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!";
}
?>
then profile.php (where user uploads the pic and stores it in 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';
?>
AND HOW TO MOVE THE FILE TO MY IMAGES FOLDER? PLEASE HELP ME WITH THIS!
Thanks!
If you are having that kind of error, it's either a file permission issue or the folder "upload/" didn't exists. Check the path as well.
精彩评论