2017-03-22 23 views
1

我是新來的PHP,因此無法找出我的問題。 我有一個連接到提到的php文件的HTML表單。此表單工作正常。問題在於php文件。 HTML表單是:如何設置我的目標文件夾上傳與變量,在PHP中?

<!DOCTYPE html> 
    <html> 
    <body> 

    <form action="upload.php" method="post" enctype="multipart/form-data"> 
    Select image to upload: 
    <input type="file" name="fileToUpload" id="fileToUpload"> 
    <input type="submit" value="Upload Image" name="submit"> 
    </form> 

    </body> 
    </html> 

而我叫upload.php的PHP文件是:

<?php 
    $uid = $_POST['uid']; 
    $target_dir = "FILES OF:$uid"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
    echo "File is an image - " . $check["mime"] . "."; 
    $uploadOk = 1; 
    } else { 
    echo "File is not an image."; 
    $uploadOk = 0; 
    } 
    } 
    ?> 

在這裏,你可以在PHP代碼的第一線看到,目標目錄設置。但我希望它與變量稱爲

$uid 

這是鏈接到我的數據庫中的uid字段的用戶名。我的意思是說,我希望它能夠在名爲「FILES OF:$ uid」的文件夾中上傳文件。 我有這些文件夾已經創建,但想要一種方式,文件將直接進入該文件夾。 有沒有人知道一種方法來做到這一點?

+0

副本,如果你想爲每個用戶u必須使用數據庫和登錄,所以當用戶登錄時,你的用戶ID,你可以讓DIR不同的文件夾基於該用戶標識。使用這種方法你不能這樣做,因爲'$ _POST'變量從表單的輸入中讀取名字,並且在那裏你沒有任何用戶名。 – Mario

回答

1

嗨我有鏈接,你可以檢查你的代碼是否正確或不如果你不想檢查鏈接,然後檢查下面的代碼。

image upload to specific folder

下面的代碼是從給定鏈路

<?php 
    $target_dir = "uploads/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"]); 
     if($check !== false) { 
      echo "File is an image - " . $check["mime"] . "."; 
      $uploadOk = 1; 
     } else { 
      echo "File is not an image."; 
      $uploadOk = 0; 
     } 
    } 
    // Check if file already exists 
    if (file_exists($target_file)) { 
     echo "Sorry, file already exists."; 
     $uploadOk = 0; 
    } 
    // Check file size 
    if ($_FILES["fileToUpload"]["size"] > 500000) { 
     echo "Sorry, your file is too large."; 
     $uploadOk = 0; 
    } 
    // Allow certain file formats 
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
    && $imageFileType != "gif") { 
     echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
     $uploadOk = 0; 
    } 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 
     echo "Sorry, your file was not uploaded."; 
    // if everything is ok, try to upload file 
    } else { 
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
      echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
     } else { 
      echo "Sorry, there was an error uploading your file."; 
     } 
    } 
    ?> 


    [1]: https://www.w3schools.com/php/php_file_upload.asp 
+0

你不能依賴mime類型,它更好地檢查圖像擴展。 – Mario

+0

不,我可以信任mime類型,它適用於我,我認爲你也可以使用我的類型。 @Mario –

+0

請在您信任之前閱讀以下內容:D http://stackoverflow.com/questions/7349473/php-file-upload-mime-or-extension-based-verification – Mario

相關問題