2016-10-02 100 views
0

我是PHP新手,仍在学习它......今天我无法弄清楚为什么文件要从目标目录 - $ target_dir上传。我改变了$ target_dir很多但我总是得到相同的结果..代码看起来很好..将文件上传到目标文件夹时出现问题

任何想法?

谢谢。

function avatarUpload(){ 

    $target_dir = "../uploads/avatars/"; 
    $target_file = 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["avatar"])) 
    { 

     $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)) 
    { 
     do 
     { 
      $rand = rand(100,10000); 
      $target_file = $rand .= $target_file; 
     } 
     while(file_exists($target_file)); 
    } 

    // Check file size 
    if ($_FILES["fileToUpload"]["size"] > 5000000000) 
    { 
     //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; 

    } else { 

     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)== true) 
     { 

      // echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
      return $target_file; 

     } else 
     { 
      // echo "Sorry, there was an error uploading your file."; 

      return $target_file; 

     } 
    } 


    return $target_file; 
    } 
+0

什么是上传? – Cyclotron3x3

+0

一切都好,只是该文件夹是错过..我正在将文件的根.. –

+0

检查您的路径,尝试将其更改为绝对,然后运行。 '../'似乎造成问题 – Cyclotron3x3

回答

0

你忘了追加目标目录文件名。

$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]); 
+0

ahhhh ..你有权利....谢谢! –

+0

如果您的问题得到解决,您会介意接受答案。这将有助于解决类似问题的未来读者 – Cyclotron3x3