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 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题