【问题标题】:How to upload any type of file to the server using PHP 5? [closed]如何使用 PHP 5 将任何类型的文件上传到服务器? [关闭]
【发布时间】:2016-03-23 11:00:53
【问题描述】:

我想创建一个 html 表单,它可以将任何类型的文件(特定的图像、pdf、doc 和文本文件)上传到服务器。是否可以为此使用单个功能。如果是那怎么办?

【问题讨论】:

标签: php forms file upload


【解决方案1】:

=> 试试这个代码上传任何类型的文件..

//HTML页面

<li class="text">File Upload </li>
<li><input type="file" name="file" value="" class="input"  ></li>

尝试所有文件上传.. //PHP页面

if(isset($_POST['submit'])!=""){
  $name=$_FILES['file']['name'];
  $size=$_FILES['file']['size'];
  $type=$_FILES['file']['type'];
  $temp=$_FILES['file']['tmp_name'];
  $caption1=$_POST['caption'];
  $link=$_POST['link'];
  move_uploaded_file($temp,"upload/".$name);// set your folder name to store all file.

【讨论】:

  • 嗯,谢谢。它有效。
【解决方案2】:

输入类型为'file'的html表单首先将文件上传到服务器上的一个临时路径,从这个临时路径,我们必须将它移动到我们服务器上的一个文件夹路径。

 <form action="uploads.php" method="post" enctype="multipart/form-data" id="imageform">
  <div class="ak"> Upload file :<input type="file"  name="imagech1" id="filess" class="filess" /></div>  
   <div id="trans1"></div>
<input type='submit' value='Submit'>
   </form>

在服务器端“uploads.php”试试这个:

 $path = "img/uploads/";

 if(isset( $_FILES['imagech1']) and $_SERVER['REQUEST_METHOD'] == "POST")
    {$name = $_FILES['imagech1']['name']; //recieves the file by the name of the input type'file'

        if(strlen($name))
            {
$ext=strrchr( $name,".");//extension of the file

$allowed=array("(",")"," "); //allowed characters in the name
$name=str_replace($allowed,"_",$name);//replace unallowed with _
$actual_image_name = $name;
$tmp = $_FILES['imagech1']['tmp_name'];//assign temperory path of file to $tmp

                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {


                                echo "file uploaded";
                            }
                        else
                            echo "failed";



            }}

        else
            echo "Please upload file..!";

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    相关资源
    最近更新 更多