how can i let image overwriting when uploading? and if uploading an image titeld different how can i make it to change to specific file tite?im trying to make an image replacement to change images on the web page like for example logo.

how can i make the file name change to the one i already uploaded lets say i have logo.png and i uploaded logo444.png how can i force it to rename to logo.png?

Member Avatar for diafol

look up the php manual on file uploading. should be straightforward.

but i cant fix the problem.theres no error but the logo is not uploading this is my code for action

<?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"]["logo.png"]))
          {
              echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
              move_uploaded_file($_FILES["file"]["name"],
              "../graphics/" . $_FILES["file"]["logo.png"]);
              echo "Stored in: " . "../graphics/" . $_FILES["file"]["logo.png"];
          }
    }
  }
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);
?>

and this is html form

<?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");

    }
?>
Member Avatar for LastMitch

@Riu 2009

how can i make the file name change to the one i already uploaded lets say i have logo.png and i uploaded logo444.png how can i force it to rename to logo.png?

If you upload logo.png and echo it : logo.png

If you upload logo444.png and echo it : logo444.png

I hope you can see the differences.

In order to overwrite a file that name has to be the same name

If you upload logo.png and echo it : logo.png

If you upload the same name but different file logo.png and echo it : logo.png overwrite the first one.

but i cant fix the problem.theres no error but the logo is not uploading this is my code for action

I'm not quite sure what you asking right now? You are asking 2 different questions.

1) Rename file

or

2) Logo not loading

or

3) Either is not working?

either is not working but what i need is to upload a file with whatever name and then my script overrite that file with logo.png which is already saved n is displayed on page as a logo.
thankyou for the help

Member Avatar for LastMitch

Riu 2009

either is not working

What do you want to do? I mean if you can't upload the logo.png, then it's pointless what you mention above.

I think you need to fixed the upload form first. I can't really help you if you just keep telling me solve your code when you can't upload anything?

yup u r right sorry for that..let me correct it then i will go further.thank you :)

hi, i've changed my code and now it is saving the image in graphics folder but not overriting it with the existing image..please help, here's my code

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 />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><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");

    }
?>

and this is my code for setup-action.php

<?php include("../includes/config.php");?>
<?php
$target_path = "../graphics/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name'], 'logo.jpg');

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
}
else
{
    echo "There was an error uploading the file, please try again!";
}
?>
<?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);
?>

any kind of help will be appreciated :)

$target_path = "../graphics/logo.png";
and remove the below line

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.