开发者

Require upload of a file in HTML/PHP form

I'm trying to require a file upload in a form, but cannot get it to work. Any ideas? I'd rather echo the php error on the page vs. a javascript popup. Thanks for taking a look:

<?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 />&l开发者_StackOverflow中文版t;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>


See this tutorial it have all that you need.

To sum up:

  • Use enctype="multipart/form-data" and method="POST" in the <form> tag.
  • In PHP use $_FILES['uploadedfile']['name'] to read original name ("uploadedfile" is the name of your file input - "file1" in your example).
  • In PHP use $_FILES['uploadedfile']['tmp_name'] to read server side temp file name.
  • In PHP use $_FILES['uploadedfile']['error'] to get the error (if any) see there for possible codes.
  • Also see the PHP manual for more info.

In your exemple use this form instead:

<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>

In "nextpage.php":

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜