简体   繁体   中英

if else statements

I am using Laravel php framework and i have a form with 2 upload fields. The fields work normally and theres no problems. The .php code is as following.

PS. For those who are not familiar with the curly braces, its basically just <?php echo .... ?> in the blade template engine.

The code

{{  Form::label('upload_files', 'Lataa bändin LAVAKARTTA, esim .jpg (tiedosto      pakollinen)'); }}
{{  Form::file('upload_files', '', array('class' => 'form-field', 'form-text')); }}

{{  Form::label('upload_files_raider', 'Lataa bändin tekninen RAIDERI esim .txt (tiedosto pakollinen)'); }}
{{  Form::file('upload_files_raider', '', array('class' => 'form-field', 'form-text')); }}

After the form is submitted i pass the contents (files) to my upload directory.

In my Controller i have this code:

$a = $formsubmit['esiintyva_artisti'];

// if no file dont upload
// $a is used to give the filename a ending that correspons to a user

if($_FILES['image']['error'] == 0){
//if(!isset($_FILES)){
    Input::upload('upload_files', 'public/uploads', $a . '-artistin-tai-bandin-lavakartta.jpg');
};

if($_FILES['image']['error'] == 0){
//if(!isset($_FILES)){
    Input::upload('upload_files_raider', 'public/uploads', $a . '-artistin-tai-bandin-raideri.txt');
};

Heres the problem:

When i submit the form WITHOUT any uploaded files i get an error:

Fatal error: Call to a member function move() on a non-object in /var/www/...../laravel/input.php on line 230

Thanks in advance community!

The error is probably coming from you checking the $_FILES['image'] array when it hasn't been set, since you use upload_files and upload_files_raider in your form. Try checking for an upload file by using Input::file('upload_files'):

if( !is_null(Input::file('upload_files')) ) {
    Input::upload('upload_files', 'public/uploads', $a . '-artistin-tai-bandin-lavakartta.jpg');
}

if( !is_null(Input::file('upload_files_raider')) ) {
   Input::upload('upload_files_raider', 'public/uploads', $a . '-artistin-tai-bandin-raideri.txt');
}

The problem was easily solved just by checking for NULL, if statements as:

        if ($file_upload == NULL) 
    {
        return View::make('site.form')->with('message', 'Choose the file to be uploaded!');
    }
    else 
    {
        Input::upload('file_upload', 'public/uploads', $date.' '.$upload_file_name);
    }

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