连接HTML和PHP

时间:2011-10-30 22:06:54

标签: php html upload

我正在尝试将文件上传到名为directory的文件夹中。我有一个上传表单的HTML代码,一旦我选择了所需的文件,我按下上传,PHP代码显示在浏览器中,文件根本没有上传。

我是PHP的新手。我以前做过HTML和CSS。可能是一点点Javascript,但不是PHP。我将uploader.php放在一个单独的文件中,而不是HTML代码。它们都在下面:

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

3 个答案:

答案 0 :(得分:6)

upload.php文件需要以php开始标记开头。

<?php

此.html文件也应该具有.php扩展名,因为大多数Web服务器使用文件扩展名来确定如何处理请求。

答案 1 :(得分:1)

我看到的是,在您的第一个文件(HTML文件)中,您有PHP代码 这不起作用:) 除非您将Web服务器设置为将HTML文件视为PHP,否则您需要将第一个文件重命名为* .php 这应该让你去

答案 2 :(得分:1)

这在HTML文件中不起作用(除非服务器配置为这样做):

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

您应该将扩展名从html更改为php