dongpo5264 2010-07-14 18:46
浏览 48

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

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 /><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条回答 默认 最新

  • duanbi1888 2010-07-14 18:49
    关注

    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']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?