demo demo
demo demo

Reputation: 5

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

I am not able to upload files the the uploads folder. I am not using public folder, I removed the public files and moved in root directors my folders sturucture for Laravel.

app
bootstrap
config
css
database
fonts
images
storage
tests
vendor
uploads
.env
.htaccess
index

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

How to get the file from that directory and pass it to the view?

Upvotes: 0

Views: 1464

Answers (2)

mirza
mirza

Reputation: 5793

Are you getting any errors ?

You may forgot the give write permission with chmod 777 to uploads folder.

edit: Remove the last slash from mkdir path.

And make sure $userr variable contains a valid value with dd($userr);

Oh ok i see now.

1- You are trying to send an object instead of sending a string to mkdir function.

2- And even i assume that you are trying to send filename to mkdir why do you need to create a folder named with that filename ? Just use timestamp or some random value with similar to rand(100,999) for creating subfolder.

3- And your $userr contains an object like this:

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

so there is no point to use it as an object or even declaring it. Use just Input::file('file1') instead.

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

and for accessing path of folder in your view, i'm always preferring use this style:

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

in your view you can access those variables with blade template engine::

name {{ $name }} <br />
filepath {{ $filepath }} <br />

or by using raw php variables:

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

Upvotes: 1

Khan Shahrukh
Khan Shahrukh

Reputation: 6361

I guess you are not sending files with the form, your form should be :

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

paste your view which is posting data

And you should check the file type before moving them into a upload folder.

instead of

$sitepath = $_SERVER['DOCUMENT_ROOT'];

use

$sitepath = public_path();

Upvotes: 0

Related Questions