0

我试图要求以表格形式上传文件,但无法使其正常工作。有任何想法吗?我宁愿在页面上回显 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>
4

1 回答 1

3

请参阅本教程,它拥有您所需要的一切。

总结一下:

  • 在标签中使用enctype="multipart/form-data"和。method="POST"<form>
  • 在 PHP 中使用$_FILES['uploadedfile']['name']读取原始名称(“uploadedfile”是您的文件输入的名称 - 在您的示例中为“file1”)。
  • 在 PHP 中用于$_FILES['uploadedfile']['tmp_name']读取服务器端临时文件名。
  • 在 PHP 中用于$_FILES['uploadedfile']['error']获取错误(如果有) ,请参阅此处以获取可能的代码。
  • 另请参阅PHP 手册以获取更多信息。

在您的示例中,请改用此表单:

<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']);
于 2010-07-14T18:49:35.570 回答