图片上传代码无效

时间:2012-08-28 18:54:56

标签: php mysql html5

我想在php中使用图片上传简单地将图片放在文件夹中,但它不起作用,请帮我解决它的问题.. thanx:)

这是我setup.php

的代码
<?php include("../includes/config.php"); ?>
<?php
    if ($_SESSION["isadmin"])
    {

        $con=mysql_connect($dbserver,$dbusername,$dbpassword);
        if (!$con) { die('Could not connect: ' . mysql_error()); }

        mysql_select_db($dbname, $con);

        $result = mysql_query("SELECT * FROM setup WHERE (id=".$_SESSION["id"].")");
        while($row = mysql_fetch_array($result))
        {
            $title = $row['title'];
            $theme = $row['theme'];
        }
        mysql_close($con);
?>
<!DOCTYPE HTML>
<html>
<head>
    <title>Admdin Home</title>
    <link rel="StyleSheet" href="css/style.css" type="text/css" media="screen">
</head>
<body>
    <?php include("includes/header.php"); ?>
    <?php include("includes/nav.php"); ?>
    <?php include("includes/aside.php"); ?>
    <div id="maincontent">

        <div id="breadcrumbs">
            <a href="">Home</a> >
            <a href="">Setup</a> >
            Customization
        </div>
        <h2>Customize</h2>
        <?php
            if (isset($_GET["status"]))
            {
                if($_GET["status"]==1)
                {
                    echo("<strong>Customization Done!</strong>");
                }
                if($_GET["status"]==2)
                {
                    echo("<strong>Customization Error!!</strong>");
                }
            }
        ?>
        <form method="post"  action="setup-action.php" enctype="multipart/form-data" >
            <label>Title Of Your Organization:</label>  <input type="text" name="title" value="<?       php echo $title; ?>" /> <br /> <br />
            <label>Select Theme</label>
            <select name="theme" value="<?php echo $theme; ?>">
                <option value="Default">Default</option>
                <option value="Dark">Dark</option>
                <option value="White">White</option>
            </select>
            <br /> <br />
            <label>Choose Your Logo Here</label><input type="file" name="file"/><br /> <br />      
            <input type="submit" name="Upload" value="Upload" />
        </form>
    </div>

</body>
<?php include("includes/footer.php"); ?>
</html>
<?php
    }
    else
    {
        header("Location: ".$fullpath."login/unauthorized.php");
    }
?>

这是setup-action.php

<?php include("../includes/config.php");?>
<?php
if(isset($_FILES["file"]))
{
    if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] ==     "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg"))
   && ($_FILES["file"]["size"] < 1000000))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }

        if (file_exists("../graphics/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["name"], "../graphics/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "../graphics/" . $_FILES["file"]["name"];
        }
     }
}
else
{
    echo "Invalid file";
}
?>
<?php
    $title=$_POST["title"];
    $theme=$_POST["theme"];
    $con=mysql_connect($dbserver,$dbusername,$dbpassword);
    if (!$con) { die('Could not connect: ' . mysql_error()); }

    mysql_select_db($dbname, $con);
    $result=mysql_query("SELECT * FROM setup WHERE id=".$_SESSION['id']);
    $num_rows = mysql_num_rows($result);
    if ($num_rows > 0)
    {
        {
            mysql_query("UPDATE setup  SET title='".$title."' , theme='".$theme."'WHERE     id=".$_SESSION['id']);
            header("Location:setup.php?status=1");
        }
    }
    else {
        header("Location:setup.php?status=2");
    }
    mysql_close($con);
?>

2 个答案:

答案 0 :(得分:3)

我不完全确定这一点,但我认为你需要在move_uploaded_file中使用tmp_name,因为文件会收到临时名称和位置,直到移动。

像这样:

move_uploaded_file($_FILES["file"]["tmp_name"],"your/path/".$_FILES["file"]["name"]);

希望它有效......其他一切对我来说似乎都很好。

答案 1 :(得分:0)

需要在move_uploaded_file()函数中使用上传文件的临时名称。临时名称包含保存在临时存储中的文件的路径