2015-04-04 132 views
0

我无法上传文件到uploads文件夹。我没有使用公共文件夹,我删除了公共文件,并在根导向器中移动了我为Laravel创建的文件夹。如何在laravel5上传文件并获取上传文件中的文件

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 protected]"; 
    $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 protected]"; 
    $headers .= "Content-type: text/html"; 
    mail($to, $subject, $mail_body, $headers); 
    return view('test')->with('name', $name); 
} 
} 

如何从该目录下的文件,并把它传递给看法?

回答

1

您是否收到任何错误?

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

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

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

哦,好吧,我现在看到的有效值。

1-您尝试发送对象而不是将字符串发送到mkdir函数。

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

3 - 和你的$ userr包含这样的对象:

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 

所以没有点把它作为一个对象,甚至宣称它。改用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 protected]"; 
    $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 protected]"; 
    $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); 

在您看来,您可以访问刀片模板引擎::

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

或通过使用原始PHP变量:

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

HTTP://msaashish.wc。lt /检查现场url的错误 – 2015-04-04 16:42:59

+0

你可以编辑我的控制器并粘贴它 – 2015-04-04 16:50:52

+0

你可以粘贴完整的控制器 – 2015-04-04 16:53:56

0

我猜你没有与形式发送文件,你的形式应该是:

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

粘贴您的视图,其发布数据

而你将它们转移到一个文件夹上传前,应检查文件类型。

代替

$sitepath = $_SERVER['DOCUMENT_ROOT']; 

使用

$sitepath = public_path(); 
+0

使用公共路径而不是文档根的优点是什么? – motto 2015-04-04 17:31:00

+0

为什么要在使用框架时使用核心内容 – 2015-04-04 17:34:04

相关问题