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

别等时光非礼了梦想. 提交于 2019-12-24 01:18:26

问题


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


来源:https://stackoverflow.com/questions/34326754/how-to-upload-any-type-of-file-to-the-server-using-php-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!