2016-04-28 87 views
1

我嘗試了很多,但還不能得到解決,請幫助我,因爲我無法上傳我的文件代碼如下所示:無法上傳文件在PHP

<?php 
$allowedExts = array("PSD", "JPG", "JPEG", "GIF", "PNG", "AI", "ZIP", "RAR", "PDF", "DOC", "DOCX", "XLS", "XLSX", "PPT"); 
    $extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION); 

    if (($_FILES["file"]["size"] < 200000) && array_search($extension, $allowedExts)!==false) { 
     if ($_FILES["file"]["error"] > 0) 
     { 
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
     } 
     else 
     { 
      echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
      echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
      echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br/>"; 
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 

      if (file_exists("uploaded_files/" . $_FILES["file"]["name"])) 
      { 
       echo $_FILES["file"]["name"] . " already exists. "; 
      } 
      else 
      { 
       move_uploaded_file($_FILES["file"]["tmp_name"], 
       "common/uploaded_files" . $_FILES["file"]["name"]); 
       echo "Stored in: " . "common/uploaded_files" . $_FILES["file"]["name"]; 
      } 
     } 
    } else { 
     echo "Invalid file"; 
    } 

    ?> 
下面

是我的PHP的形式代碼:

<input type ="file" name= "file" id ="file"/> 
+4

您的表單是否具有'

'? – Pupil

+0

是的,它是在我的形式dere但仍然我得到錯誤無效的文​​件 –

+0

這意味着如果條件不起作用,你是上傳一個大文件'> 200000' – Nehal

回答

0

使用下面的代碼,供大家參考..

$filename = basename($_FILES["file"]["name"]); 
$target_dir = "../../file/"; 
$target_file = $target_dir . basename($_FILES["file"]["name"]); 

if(isset($_POST["submit"])) {  
     move_uploaded_file($_FILES["file"]["tmp_name"], $target_file); 
     echo "</br>The file ". basename($_FILES["file"]["name"]). " has been uploaded."; 
     } 
0

您allowedExts不匹配小寫,試試這個小的修改。

$extension = strtoupper(pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION));