如何使用PHP5将任何类型的文件上传到服务器


How to upload any type of file to the server using PHP 5?

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

=>尝试此代码上传任何类型的文件。。

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

输入类型为"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..!";