Hi, I am having a problem with my file upload. I have a folder with my fileform.html and upload_file.php and a folder "upload" where my uploaded file stored.

This is the body of my fileform.php:

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

This is my upload_file.php:

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    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 "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

When I try upload an image I get this:

"Upload: image123.jpg
Type: image/jpeg
Size: 470.512695312 Kb
Stored in: C:\Windows\Temp\php2820.tmpInvalid fileInvalid file "

and no image is being uploaded. Can someone please please help me out and tell me where I am going wrong I would be very greatful. Thank you.

Member Avatar for diafol

To where are you trying to upload? Any references will be relative to this page (or parent page if this is an include file) if they don't begin with a '/'.

I have to say, you have a lot of duplication here. You could wrap all this up into about 10 lines.

To where are you trying to upload? Any references will be relative to this page (or parent page if this is an include file) if they don't begin with a '/'.

I have to say, you have a lot of duplication here. You could wrap all this up into about 10 lines.

a folder called "upload" in the same directory. I don't understand how i can do this?

Member Avatar for diafol

This looks suspiciously like the W3Schools example script. Check to see if your folder is writable.

$to_path = "upload/";
$to_path .= basename( $_FILES['file']['name']); 

if(move_uploaded_file($_FILES['file']['tmp_name'], $to_path)) {
    echo "I just uploaded " . basename($_FILES['file']['name']). 
    " for you";
} else{
    echo "Brick wall mate! You can stick your file up your... nostril.";
}
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.