0

我正在尝试使用 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);
?>
4

2 回答 2

3

我对此并不完全确定,但我认为您需要在 move_uploaded_file 中使用 tmp_name,因为该文件在移动之前会收到一个临时名称和位置。

像这样:

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

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

于 2012-08-28T19:52:50.337 回答
0

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

于 2012-08-28T20:30:06.930 回答