How to upload any type of file to the server using

2019-07-10 07:10发布

问题:

I want to create a html form which can upload any type of file(to be specific image, pdf, doc and text files) to server. Is it possible using single function for this. If yes then how?

回答1:

=> Try this code for upload any type of file ..

//HTML PAGE

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

try this to all file upload.. //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.


回答2:

The html form with input type 'file' first uploads the file to a temporary path on the server, from this temporary path , we have to move it to a folder path on our server.

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

Try this for the server side '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..!";