需要以HTML / PHP格式上传文件

时间:2010-07-14 18:46:12

标签: php file-upload require

我正在尝试在表单中要求上传文件,但无法使其正常工作。有任何想法吗?我宁愿回应页面上的php错误与javascript弹出窗口。谢谢你看看:

<?php 


// initialize $file1
$file1 = $_POST['file1'];


 // check upload of $file1
 if ($file1 == '' )  { 
$error = "Please upload an image";
 } 


?>


<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Require upload of an file</title>
</head>

<body>

<?php if ($error) {echo $error;} ?>

<br /><br /><br />

<form id="form1" name="form1" method="post" action="nextpage.php">

<input type="file" size="8" name="file1" />

<input name="Submit" type="Submit" />

</form>



</body>
</html>

1 个答案:

答案 0 :(得分:2)

请参阅this tutorial它拥有您需要的一切。

总结一下:

  • enctype="multipart/form-data"标记中使用method="POST"<form>
  • 在PHP中使用$_FILES['uploadedfile']['name']来读取原始名称(“uploadedfile”是文件输入的名称 - 在您的示例中为“file1”)。
  • 在PHP中使用$_FILES['uploadedfile']['tmp_name']来读取服务器端临时文件名。
  • 在PHP中使用$_FILES['uploadedfile']['error']来获取可能代码的错误(如果有)see there
  • 另请参阅PHP manual了解详情。

在您的例子中使用此表格:

<form id="form1" name="form1" method="post" action="nextpage.php" enctype="multipart/form-data">
    <input type="file" size="8" name="file1" />
    <input name="Submit" type="Submit" />
</form>

在“nextpage.php”中:

//Use $_FILES['file1'] to check the file upload...
print_r($_FILES['file1']);