Php upload file not working

Hi all,

I’m a newbie here. I’ve tried to run a simple file upload. The problem is the browser keep on showing the php script when i press submit button. Anyway below are two files i’m working on

  1. test.html

<html>
<body>
<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>
</body>
</html>

  1. 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”] / 10240) . " kB<br>”;
echo "Stored in: " . $_FILES[“file”][“tmp_name”];
}

?>

By the way, i’m using XAMPP in windows 7. Any help would be much appreciated.

Hi @marie_jane; and welcome to the forum.

There were errors in your script which I have removed.

Try this:

test.html



<!doctype html>
<html>
<head>
 <title>Test upload</title>
</head>
<body>
  <form action="upload_file.php" method="post" enctype="multipart/form-data">
    <div>
      <label for="file" filename </label>
      <input type="file" name="file" id="file" />
      <br>
     <input type="submit" name="submit" value="submit" />
  </div>    
</form>
</body>
</html>


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"] / 10240) . " kB<br>";
  echo "Stored: " . $_FILES["file"]["tmp_name"];
  
}


Hi @John_Betong,

thanks for your reply. I have tried to use your code, however the upload_file.php code is not being executed. Instead, raw php code is displayed on the browser. see below photo.

I sure that both html form and php file is saved at c:/xampp/htdocs

Please could it be any other problem?

Hi @marie_jane;

I tested the script both on the localhost and also uploaded the files here:

http://www.johns-jokes.com/downloads/sp-c/marie-jane/test.html

Assuming the test.html displays, allows browsing, file selection then I think the upload_file.php is corrupt.

Your screen dump is still not available - "Attachments Pending Approval"

Have you tried directly calling the upload_file.php from the browser? It should display something similar:


[B]Notice[/B]:  Undefined index: file in [B]/home/john/htdocs/test/upload_file.php[/B] on line [B]2[/B]

[B]Notice[/B]:  Undefined index: file in [B]/home/john/htdocs/test/upload_file.php[/B] on line [B]8[/B]
Upload: 

[B]Notice[/B]:  Undefined index: file in [B]/home/john/htdocs/test/upload_file.php[/B] on line [B]9[/B]
Type: 

[B]Notice[/B]:  Undefined index: file in [B]/home/john/htdocs/test/upload_file.php[/B] on line [B]10[/B]
Size: 0 kB

[B]Notice[/B]:  Undefined index: file in [B]/home/john/htdocs/test/upload_file.php[/B] on line [B]11[/B]
Stored: 

If there is a problem with the file I suggest copying and pasting the contents into a new Notepad text and overwriting upload_file.php. This would eliminate any BOM file conflicts,

Hi @John_Betong,

The tested link from your link is working. but it’s not working when i’m tested it locally:injured:

I’ve tried calling upload_file.php from the browser and it’s similar to your reply. I’ve also overwriting the existing upload_file.php using new notepad tect. unfortunately the problem still persist. anyway, i attached another screen dump

Hi @marie_jane;

Php files will not run on Windows.

Either Wamp or Xampp server must be installed. Both are free, I prefer Xampp because the installation is easier.

yes, i’m currently using Xampp. I can execute simple php using echo or print statement. it’s just that the problem occurs when i’m trying to make a form handling where the browser only shows raw php code instead of handling the data

The screen dumps supplied show the files being called from the browser. HTML files will display OK but not php files.

Have you tried calling the files from your localhost server?

Http://localhost/test.HTML

Please note that the above should all be lowercase. This tablet has a mind of its own :frowning:

ahha…

now i manage to find the problem. I’ve tried calling from the local host server and guess what?? it works!!

Many thanks for your guidance…:rofl:

I am pleased that it is working ok and many thanks for letting us know.