ebeth
ebeth

Reputation: 163

Connecting HTML and PHP

I am trying to upload a file into a folder called directory. I have the HTML code for an uploading form and once i've selected the desired file and I press upload, the PHP code is displayed in the browser and the file is not uploaded at all.

I am new to PHP. I have done HTML and CSS before. Possibly a little bit of Javascript, but not PHP. I put my uploader.php in a separate file than the HTML code. They are both below:

myHTML.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml">
<body>

<?php
include 'uploader.php';
?>

<p>  
<h1> Advanced Tablet Based Visualization of Volumetric Data </h1>
</p>

 <p>
 Available files:
 </p>
 HELLO WORLD!!
 <form action="">
 <input type="button" value="Open"> <input type="button" value="Delete">
 </form>

 <hr />

 <p>
 <!-- Upload file source code: http://www.tizag.com/phpT/fileupload.php -->

 <form enctype="multipart/form-data" action="uploader.php" method="POST">
 <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

 <br /> <br />
 <hr />
 Last Updated: October 24, 2011

 <br />
 Sponsored by:
 University of Notre Dame <img src="ndLogo" alt="ND logo" width="32" height="32"s />        
 <br />
 </p>

 </html>
 </body>

uploader.php:

// Where the file is going to be placed 
$target_path = "uploads/";

 /* Add the original filename to our target path.  
 Result is "uploads/filename.extension" */  
 $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

 $target_path = "uploads/";

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

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!";
}

Upvotes: 2

Views: 154

Answers (3)

Nasreddine
Nasreddine

Reputation: 37798

This won't be work inside an HTML file (unless the server is configured to do so):

<?php
    include 'uploader.php';
?>

You should change the extension from html to php

Upvotes: 1

Dany Khalife
Dany Khalife

Reputation: 1850

What i see is that in your first file (which is an HTML file) you have PHP code That doesn't work that way :) Unless you setup your web server to treat HTML files as PHP, you will need to rename your first file to *.php This should get you going

Upvotes: 1

Fosco
Fosco

Reputation: 38506

The upload.php file needs to start with the php start tag.

<?php

Also the .html file should have the .php extension, as most web-servers use the file extension to determine how to handle a request.

Upvotes: 6

Related Questions