Jump to content

Single Image Upload to $user template


mr-fan
 Share

Recommended Posts

i've searched many times for just upload single images on user template without creating a new page...and best found was the gist from soma:

https://gist.github.com/somatonic/4150974

But i've stucked don't get the upload working on this....if i did a var_dump on the upload array it is emty...hit my head on the wall while searching the issue...

my edited code with some debug messages:

<?php //color the code somas code below

// front-end form example with multiple images upload
// add new page created on the fly and adding images
if($input->post->user_img){
// tmp upload folder for additional security
$upload_path = $config->paths->root . "tmp_uploads/";
 // new wire upload
$u = new WireUpload('user_img');
$u->setMaxFiles(1);
$u->setOverwrite(true);
$u->setDestinationPath($upload_path);
$u->setValidExtensions(array('jpg', 'jpeg'));
// execute upload and check for errors
$files = $u->execute();
  //just some checks whats going on...$files is empty  $upload_path seems to be right!
  $successMessage .= var_dump($files);
  $successMessage .= $upload_path.'<br>';
if(!$u->getErrors()){
// add images upload
foreach($files as $filename) {
  $successMessage .= $filename;
$user->user_img = $upload_path . $filename;
}
// save page
$user->save();
// remove all tmp files uploaded
foreach($files as $filename)
//unlink($upload_path . $filename);
$successMessage .= "<p class='message'>Files {$filename} uploaded!</p>";
} else {
// remove files
foreach($files as $filename)
//unlink($upload_path . $filename);
// get the errors
foreach($u->getErrors() as $error) $errorMessage .= "<p class='error'>$error</p>";
}
}

And here is the form itself...it is in a var to echo the whole thing...with some options ans so on...

// display a user settings form
$settingsForm = "
<!-- general form elements -->
<div class='box box-primary'>
    <div class='box-header'>
        <h3 class='box-title'>Voreinstellungen ändern:</h3>
    </div><!-- /.box-header -->
    <!-- form start -->
    <form name='settings' role='form' action='./' method='post'>
        <div class='box-body'>
            <div class='form-group'>
                <label for='user_an'>Auswahl Auftragnehmer</label>
                <select name='user_an' class='form-control' id='user_an'>
                    ".$optionsLu."
                </select>
            </div>
            <div class='form-group'>
                <label for='user_mr'>Auswahl Maschinenring</label>
                <select name='user_mr' class='form-control' id='user_mr'>
                    ".$optionsMr."
                </select>
            </div>
            <div class='form-group'>
                <label for='exampleInputFile'>Profilbild ändern</label>
                <input name='user_img' type='file' id='user_img' accept='image/jpg,image/jpeg'>
                <p class='help-block'>Nur jpg Bilder sind erlaubt!</p>
            </div>
        </div><!-- /.box-body -->

        <div class='box-footer'>
            <button type='submit' class='btn btn-primary'>Ändern</button>
        </div>
    </form>
</div><!-- /.box -->
";

Really have no clue why the upload array is emtpy?

Best regards mr-fan

  • Like 1
Link to comment
Share on other sites

Hi mr-fan

  1. You need to add enctype="multipart/form-data" to your form tag
  2. Is "user_img" the name of your file input? If so, then I guess $input->post->user_img would return null, since this key is inside PHP's global $_FILES variable (not in $_POST)

Edit: LostKobrakai wins :) Do you ever sleep?

Cheers

  • Like 3
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...