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

时间:2015-12-17 04:42:24

标签: php forms file upload

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

2 个答案:

答案 0 :(得分:2)

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

// HTML PAGE

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

尝试将此文件上传到所有文件中 // PHP PAGE

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.

答案 1 :(得分:0)

带有输入类型&#39;文件&#39;的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>

在服务器端尝试此操作&#39; uploads.php&#39;:

 $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..!";