trouble creator
trouble creator

Reputation: 127

Image upload code isnt working

I am trying to simply place an image in the folder using image upload in php but it is not working, please help me what is the problem with it.. thanx :)

this is my code for 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");
    }
?>

this is 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);
?>

Upvotes: 0

Views: 136

Answers (2)

Jesse Rice
Jesse Rice

Reputation: 48

Need to use the uploaded file's temp name in the move_uploaded_file() function. The temp name contains the path to the file saved in temporary storage

Upvotes: 0

ipoga
ipoga

Reputation: 394

I am not completely certain about this, but I think you need to use tmp_name in the move_uploaded_file, as the file receives a temporary name and location untill moved.

Like so:

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

Hope it works... Everything else did seem perfectly ok to me.

Upvotes: 3

Related Questions