简体   繁体   中英

Files not uploading to php directory

I'm trying to create a file upload program in php, that consists of a file_upload.php and upload.php. The problem is after a user logs in and uploads a file, an array or file path is displayed instead of the file. The lines below should create the directory path then echo the file to the page:

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

But instead it's printing out the following:

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

How do I get it to display the files from the directories rather than the paths?

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);

?>

There are many way to accomplish this; an easy method would be to use a foreach loop to output only the filenames.

fileupload.php

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM