文件未上传到php目录

时间:2014-09-17 03:16:38

标签: php session file-upload directory

我正在尝试在php中创建一个文件上传程序,它由file_upload.php和upload.php组成。问题是用户登录并上传文件后,会显示数组或文件路径而不是文件。下面的行应该创建目录路径,然后将文件回显到页面:

mkdir($full_path, 0700);
echo($full_path, 0700);

但相反,它打印出以下内容:

/home/ajhausdorf/uploading//Argentina.docx/home/ajhausdorf/uploading//Argentina.docxnouploaded/tmp/phpcLfB0Y

如何让它显示目录而不是路径中的文件?

file_upload.php

<?php
        session_start();
        $username = $_SESSION['username'];
        echo $_SESSION['username'];
?>

<?php

    // view the files uploaded to the user's directory
        $user_path = sprintf("/home/ajhausdorf/uploading/%s", $userName);
           $files=scandir($user_path);
?>

upload.php的

<?php
         session_start();

         // Get the filename and make sure it is valid
         $filename = basename($_FILES['uploadedfile']['name']);
         if( !preg_match('/^[\w_\.\-]+$/', $filename) ){
            echo "Invalid filename";
            exit();
         }

         // Get the username and make sure it is valid
         $username = $_SESSION['username'];
         if( !preg_match('/^[\w_\-]+$/', $username) ){
            echo "Invalid username";
            exit();
         } 

         $full_path = sprintf("/home/ajhausdorf/uploading/%s/%s", $username, $filename);
         $dir = sprintf("/home/ajhausdorf/uploading/%s", $username);
         $user_path = sprintf("/home/ajhausdorf/uploading/%s", $userName);
            $view = scandir($user_path, 1);
               mkdir($full_path);
               echo ($full_path);

            if (file_exists($dir)) {
               mkdir($full_path, 0700);
               move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $full_path);

            } else {
               mkdir($full_path, 0700);
               echo($full_path);
               move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $full_path);
               echo "no";
            }

            if(file_exists($_FILES['uploadedfile']['tmp_name']) || is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
               echo  "uploaded";
            } 

          echo $_FILES['uploadedfile']['tmp_name'];
          print_r($view);

?>

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点;一个简单的方法是使用foreach循环仅输出文件名。

<强> fileupload.php

$user_path = sprintf("/home/ajhausdorf/uploading/%s", $userName);
$files=scandir($user_path);

foreach($files as $file) {
    if($file == "." || $file == "..") {
        continue;
    }
    echo $file;
}