用php和SQL上传图片

时间:2011-10-15 11:30:31

标签: php sql

我希望用户上传个人资料照片!

我想上传文件(图片),将字符串/路径和文件名存储在我的数据库中,然后将文件移动到我的images文件夹中!但是我没有这个怎么做呢!

我有几个你可以查看的文件,看看我正在尝试做什么......

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

如何将文件移动到我的图像文件夹? 请帮助我!

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您遇到此类错误,则可能是文件权限问题或文件夹“upload /”不存在。检查路径。