laravel5中上传文件中如何上传文件和获取文件

How to upload files and get files in uploads files in laravel5

我无法将文件上传到上传文件夹。我没有使用 public 文件夹,我删除了 public 文件并在根目录中移动了我的 Laravel.

文件夹结构
app
bootstrap
config
css
database
fonts
images
storage
tests
vendor
uploads
.env
.htaccess
index

我的AashishController.php

<?php 
namespace App\Http\Controllers;
use Input;
use Mail;

class AashishController extends Controller
{
public function index()
{

return view('in');  

}
public function contact()
{
    return view('contact'); 
}
public function postcontact()
{
     $name = \Input::get('name');
     $email = \Input::get('email');
     $phone = \Input::get('phone');
     $message1 = \Input::get('message');
     $file1 = Input::file('file1');

     $userr=$file1;

    //Create directory if it does not exist
    if(!is_dir("uploads/".$userr."/")) {
        mkdir("uploads/".$userr."/");
    }

    // Move the uploaded file
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$userr."/". $_FILES["file"]["name"]);

    $to ="email@example.com";
    $subject = "hi";
    $mail_body = '<html>
        <body bgcolor="#573A28" topmargin="25">
        Put HTML content here with variables from PHP if you like
        Variable display Example: 
        </body>
    </html>';
    $headers  = "From:email@example.com";
    $headers .= "Content-type: text/html";
    mail($to, $subject, $mail_body, $headers);
    return view('test')->with('name', $name);
}
}

如何从该目录中获取文件并将其传递给视图?

你有任何错误吗?

您可能忘记了使用 chmod 777 授予对上传文件夹的写入权限。

编辑:从 mkdir 路径中删除最后一个斜杠。

并确保 $userr 变量包含 dd($userr);

的有效值

哦,好的,我现在明白了。

1- 您试图发送 object 而不是向 mkdir 函数发送字符串。

2- 即使我假设您正在尝试将文件名发送到 mkdir,为什么您需要创建一个以该文件名命名的文件夹?只需使用时间戳或一些类似于 rand(100,999) 的随机值来创建子文件夹。

3- 你的 $userr 包含一个 object 这样的:

object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]
  private 'test' => boolean false
  private 'originalName' => string 'test.jpg' (length=22)
  private 'mimeType' => string 'image/jpeg' (length=10)
  private 'size' => int 667220
  private 'error' => int 0

所以没有必要将它用作 object 甚至声明它。仅使用 Input::file('file1')。

public function postcontact()
{
     $name = \Input::get('name');
     $email = \Input::get('email');
     $phone = \Input::get('phone');
     $message1 = \Input::get('message');
     $file1 = Input::file('file1');


    $timestamp = time();
    //Create directory if it does not exist
    if(!is_dir("uploads/".$timestamp)) {
        mkdir("uploads/".$timestamp);
    }

$sitepath = $_SERVER['DOCUMENT_ROOT'];

        // Move the uploaded file
Input::file('file1')->move($sitepath.'/uploads/'.$timestamp.'/', Input::file('file1')->getClientOriginalName());

    $to ="email@example.com";
    $subject = "hi";
    $mail_body = '<html>
        <body bgcolor="#573A28" topmargin="25">
        Put HTML content here with variables from PHP if you like
        Variable display Example: 
        </body>
    </html>';
    $headers  = "From:email@example.com";
    $headers .= "Content-type: text/html";
    mail($to, $subject, $mail_body, $headers);
    return view('test')->with('name', $name);
}

为了访问您视图中的文件夹路径,我总是喜欢使用这种样式:

$data['name'] = $name;
$data['filepath'] = "uploads/".$timestamp."/".Input::file('file1')->getClientOriginalName();
return view('test',$data); 

在您看来,您可以使用 blade 模板引擎::

访问这些变量
name {{ $name }} <br />
filepath {{ $filepath }} <br />

或使用原始 php 变量:

name <?php echo $name; ?> <br />
filepath <?php echo $filepath; ?> <br />

我猜你没有用表格发送文件,你的表格应该是:

{{ Form::open(["url"=>"/someurl", "files"=>true,])}    

粘贴您正在发布数据的视图

并且在将它们移动到上传文件夹之前,您应该检查文件类型。

而不是

$sitepath = $_SERVER['DOCUMENT_ROOT'];

使用

$sitepath = public_path();