2015-12-17 54 views
2

我想創建一個html表單,它可以將任何類型的文件(特定圖像,pdf,doc和文本文件)上傳到服務器。這是否可能使用單一功能。如果是,那麼如何?如何使用PHP 5將任何類型的文件上傳到服務器?

+1

上傳PHP中使用的文件'move_uploaded_file'閱讀http://php.net/manual/en/function。 move-uploaded-file.php –

+1

[簡單文件上傳腳本]的可能重複(http://stackoverflow.com/questions/9207379/simple-file-upload-script) – Machavity

回答

0

=>嘗試上傳任何類型的文件,這個代碼..

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

那麼,Thanxx。有用。 – Pankkaj

+0

任何時候博士......! –

0

輸入類型爲'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..!"; 
相關問題