2016-12-20 63 views
1

我有一個新的問題。我想從我的網頁上傳圖像到服務器上的文件夾(我正在使用XAMPP)。PHP上傳圖片不可用

我嘗試了很多從php.net和w3schools.com這兩個例子,但它不工作,我不知道爲什麼。 -_-

這是我現在使用的代碼:

$target_dir = "./uploads/images/"; 
      $target_file = $target_dir . basename($_FILES["thumbnail"]["name"]); 
      $uploadOk = 1; 
      $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
      // Check if image file is a actual image or fake image 
      if(isset($_POST["submit"])) { 
       $check = getimagesize($_FILES["thumbnail"]["tmp_name"]); 
       if($check !== false) { 
        $text = $text . "File is an image - " . $check["mime"] . "."; 
        $uploadOk = 1; 
       } else { 
        $text = $text . "File is not an image."; 
        $uploadOk = 0; 
       } 
      } 
      // Check if file already exists 
      if (file_exists($target_file)) { 
       $text = $text . "Sorry, file already exists."; 
       $uploadOk = 0; 
      } 
      // Check file size 
      if ($_FILES["thumbnail"]["size"] > 500000) { 
       $text = $text . "Sorry, your file is too large."; 
       $uploadOk = 0; 
      } 
      // Allow certain file formats 
      if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
      && $imageFileType != "gif") { 
       $text = $text . "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
       $uploadOk = 0; 
      } 
      // Check if $uploadOk is set to 0 by an error 
      if ($uploadOk == 0) { 
       $text = $text . "Sorry, your file was not uploaded."; 
      // if everything is ok, try to upload file 
      } else { 
       if (move_uploaded_file($_FILES["thumbnail"]["tmp_name"], $target_file)) { 
        $text = $text . "The file ". basename($_FILES["thumbnail"]["name"]). " has been uploaded."; 
       } else { 
        $text = $text . "Sorry, there was an error uploading your file."; 
       } 
      } 

這是W3Schools的爲例。

變量$文本只是在URL中看到發生了什麼。 該文件的inputfield被稱爲:縮略圖

我以爲我不從頁面獲取的圖像,因爲當我寫這篇文章:

$text = $text . "Sorry, there was an error uploading your file: ". basename($_FILES["thumbnail"]["name"]). " Text "; 

我得到之間沒有任何東西:文件:&文本

我希望有人能幫我解決我的問題。編輯=====================

下面是HTML內容:

<form id="postform" name="postform" action="?track=7" method="POST"> 
    <label for="thumbnail" class="forinput">Thumbnail <span class="info">Das Bild wir auf 250px : 250px skaliert</span></label> 
    <input class="addMovieFormFile" id="thumbnail" type="file" name="thumbnail"> 
    <input type="submit" value="FILM HINZUFÜGEN" class="button"> 
</form> 
+1

告訴我們您的HTML內容 – Blueblazer172

+0

安置自己的HTML表單 –

+0

@ Blueblazer172 我在編輯部分這樣做:-) – derMatt

回答

0

你缺少了 「ENCTYPE =」 的multipart/form-data的

請在您的表單如使用此

<form id="postform" name="postform" action="?track=7" method="POST" enctype="multipart/form-data"> 

如果不使用此功能,則無法檢索文件數據。

希望這會爲你工作。

+0

謝謝@Dinesh帕爾 – derMatt

+0

@derMatt最受歡迎 –