Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Shane McC
Shane McC
3,005 Points

Upload image but being saved as a folder?

Hi Everyone,

I'm attempting to make a new folder within my website and store user images in that specific folder. I'm running into a bit of a problem.

The image is uploading but instead of the image being uploaded(jpg, png etc..) a folder with the name of the image is being uploaded. I'm not sure what I'm doing wrong. My syntax is below. What can I do to correct my problem? Thanks

(Picture of "image" that's being saved but it's being saved as a folder?) http://tinyurl.com/orl32ts

My markup is easier to view here http://laravel.io/bin/De0nj

Below is my syntax

<?php

if($validator->passes()){

    $image = Input::file('image');
    $title = Input::get('title');
    var_dump($image->getRealPath());
    $fileName = $image->getClientOriginalName(); 
    $path3 = public_path().'/public/img/'. $fileName;
    var_dump($fileName);

    /*
    Below I'm dumping/printing the path. Everything seems to be fine.
    */
    var_dump($path3).'<br>';
    print_r($path3).'<br>';

    /* 
    Testing to see if path is writable. On the first image upload, it's not writable (because "img" does not exist)
    */
    if(is_writable($path3)){
        echo '<br>'."It's writable".'<br>';
    } else {
        echo '<br>'."It's not writeable".'<br>';
    }

    /* 
    Testing to see if makeDirectory has been made. makeDirectory returns a boolean. If makeDirectory is larger than 1, the the directory has been made
    */
    File::makeDirectory($path3, $mode = 0777, true, true);
    if (File::exists($path3) > 0){
        echo '<br>'.'It exists'.'<br>';
    } else {
        echo '<br>'.'It does not exist'.'<br>';
    }

    /* 
    Testing to see if the path is writable. The answer is yes. Why? because the directory above has been made. 
    */
    if(is_writable($path3)){
        echo '<br>'."It's writable".'<br>';
    } else {
        echo '<br>'."It's not writeable".'<br>';
    }
    die();

More code........

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Shane,

The following code is the reason you're seeing a directory rather than a file.

File::makeDirectory($path3, $mode = 0777, true, true);

Laravel has built in helpers that allows you to easily save your image to the given path using a helper method called save which is part of the Image class, also your $path3 variable doesn't need public/ as public_path() will include that as part of the path string returned.

<?php

if ($validator->passes()) {
    $image      = Input::file('image');
    $fileName   = $image->getClientOriginalName(); 
    $path3      = public_path() . '/img/' . $fileName;

    Image::make($image)->save($path3);

    // Rest of the code here...
}

Hope that helps.

Shane McC
Shane McC
3,005 Points

Chris Upjohn I appreciate the help. Such an easy solution. Part of my problem is that I over complicate things.

Your image is being saved as a directory because when you call makeDirectory

<?php

File::makeDirectory($path3, $mode = 0777, true, true);

your variable $path3 is equal to

<?php

$path3 = public_path().'/public/img/'. $fileName;

I'm guessing makeDirectory is exploding that string at forward slashes and created sub folders for each item of the resulting array. Therefore the last item in your array is your file name - and that's why you're getting a sub folder of the filename.

Shane McC
Shane McC
3,005 Points

Tom Cawthorn thanks again. Always great incite, thanks for pointing this out for me. Appreciate it