2017-03-22 25 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

相关问题