0
<?php

if(isset($_POST['pic'])){

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 300000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {


    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 "<h1>Done! Looks Great!</h1>";
    }
  }

else
  {
  echo "Invalid file";
  }
}
?>

<form action="editprofile.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<p>Image format should be png or jpg.</p>
<center><p class="submit"><input type="submit" name="pic" value="Upload Picture" /></p></center>
</form>
</div>


<p style="text-align:center; font-size:18px;">Current Picture</p>

<?php

$filename = $_FILES['file']['tmp_name'];

?>

<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>
<img src="../../upload/foto.PNG" class="picture"/>

I am getting an error like undefined index - file.

Error is in the last few lines.

I basically have a folder which has an image. I want it to display the only image in the folder.

1
  • The last 9 lines don't make sense except for the last one. You are using a local file path to display an image, which doesn't work. Get rid of that and you might already be fine Commented Nov 20, 2010 at 19:53

1 Answer 1

1

If you open the page for the first time, no form has been sent yet and $_FILES is therefore empty. You try to access $_FILES even in case of first load. This is the faulty line:

$filename = $_FILES['file']['tmp_name'];

You should check that $_POST["pic"] is set before accessing the $_FILES variable (just as you have done on the top of the code).

Sign up to request clarification or add additional context in comments.

7 Comments

oh..thanks...and how do i display the file that has been uploaded by the user earlier and then replace it by the new one uploaded by the user?
You have to remember which files the user uploaded. You can use database, files or sessions for this, depending on your needs. If you keep the list of all uploaded files by the users, sort them by date and pick the top one. If you store just the last one, simply overwrite whenever a new file is successfully uploaded.
sorry for asking a dumb question, but Im a newbie. How do i store the images in the database?
You first have to set up a database server, such as MySQL. Then you need to create database tables that will store the information (in your case that will be users and files). After that, you need to connect with PHP to the server and store/read the data as you need. You will need to learn SQL to work with databases. It's quite complex to explain this is an answer, look for MySQL & PHP tutorials, such as this one: freewebmasterhelp.com/tutorials/phpmysql
I do understand database systems, but how do i insert the content (file) into the db? I have created a mediumblob for the image in the db and using the insert function, but that does not work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.