PHP Problems : Adding custom Avatar to server

Get Help, Tips & Tutorials on The Internet and Our Forum

PHP Problems : Adding custom Avatar to server

Postby Straw » Mon Aug 03, 2015 11:10 pm

Hey there,

I know there are some people around here who know about PHP. I'm oriented object PHP.

Here is my problem, you'll find code parts below. I'm making my own website where people will find my CV, my works, passions and everything. People can register and edit their profil. I started to work on an "Avatar Customization" part (which is new for me). I am having an error message from the beginning and I don't know why actually...

Image

Here's the code :

This is the form :

Code: Select all
<form action="/valentin/profil/changer_avatar/changement/" method="post">

      <label for="url" class="float">URL :</label><input type="url" name="url" placeholder="URL de l'image" size="50" /><br /><br />

      <label for="fichier" class="float">Parcourir votre ordinateur</label><input type="file" name="fichier" /><br /><br />
      <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />

      <label for="submit" class="float"><img src="/valentin/images/transparent.png" /></label><input type="submit" name="submit" value="Envoyer" />

   </form>


This is just the index file, calling a controller on clic
Code: Select all
<?php

require $_SERVER['DOCUMENT_ROOT'].'valentin/control/profil/changer_avatar/controleurClic.php';

?>



This is the controller on clic, which gives me the error...

Code: Select all
<?php
   require_once $_SERVER['DOCUMENT_ROOT'].'valentin/control/profil/changer_avatar/controleurForm.php';
   $ctrl = new ControleurChangerAvatarForm();
   
   if(isset($_FILES['fichier']) && $_FILES['fichier']['size'] != 0){
      $ctrl->changerAvatarFile($_POST['MAX_FILE_SIZE'], $_FILES['fichier']['tmp_name'], $_FILES['fichier']['error'], $_FILES['fichier']['name'], $_FILES['fichier']['size']);   
   }
   else{
      if(!(isset($_FILES['fichier']) && $_POST['url'] != null)){
         die('Erreur lors du transfert, mauvaise URL ou taille du fichier incorrecte.');
      }

      $ctrl->changerAvatarUrl($_POST['url']);
   }

?>


SHOW | "If you need the main controller..."
Code: Select all
<?php

require_once $_SERVER['DOCUMENT_ROOT'].'valentin/modele/utilisateur.php';

class ControleurChangerAvatarForm {

   private $utilisateur;

   public function __construct(){
      $this->utilisateur = new Utilisateur();
   }

   public function changerAvatarFile($max, $tmp, $erreur, $nom, $taille) {
      session_start();

      if(isset($_SESSION['statut']) && $_SESSION['statut'] == 'connecte'){
         
         $titre = 'Changement d\'avatar';

         $pseudo = $_SESSION['pseudo'];

         if($erreur > 0) die('Erreur lors du transfert');
         if ($taille > $max) die('Le fichier est trop gros.');

         $extensionsValides = array('jpg', 'jpeg', 'gif', 'png');

         $extensionUpload = strtolower(substr(strrchr($nom, '.'), 1));

         if(in_array($extensionUpload, $extensionsValides)){
            $path = sha1(uniqid(rand(), true));
            $name = $_SERVER['DOCUMENT_ROOT'].'valentin/images/avatars/{$pseudo}.$path.{extensionUpload}';
            $url = '/valentin/images/avatars/{$pseudo}.$path.{$extensionUpload}';
            $resultat = move_uploaded_file($tmp, $name);

            if($resultat) echo 'Transfert réussi';
            $this->utilisateur->ajouterAvatar($url, $pseudo);
            header('location: /valentin/profil/');
         }

         else echo 'Erreur dans l\'extension du fichier.';
      }
         
   }

   public function changerAvatarUrl($url){
      session_start();

      $pseudo = $_SESSION['pseudo'];
      $this->utilisateur->ajouterAvatar($url, $pseudo);
      header('location: /valentin/profil/');
   }
}


I haven't checked on google and I refuse to do it for now, I guess that I have enough knowledge to find out by myself, I just need... advices ? A tip or something if you find out my error :)

Here is what my website looks like if you want to see ^^ It's far from being done, I've just put the main stuff in there for now :)

Image

Cheers :music:
User avatar
Straw
.
 
Posts: 8430
Joined: Fri Jun 17, 2011 2:35 pm
Location: France, Laval
Home-Game: Counter Strike - Global Offensive
Favourite Weapon: Spoon
Wolfenstein ET Name: #ET|Straw|
Steam Name: Mansikka
Clan Wars: 1

Re: PHP Problems : Adding custom Avatar to server

Postby MaTT » Tue Aug 04, 2015 12:00 am

Sounds like it's a problem with your PHP configuration. You need to increase the max file size if you haven't already in your php.ini file. It's been a while since i've looked into the file myself so I can't tell you where to find it, but I know it's in there, hopefully that will do something.

Also looking at your code, I don't see why you need this line in your form:
Code: Select all
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />

This basically acts as a variable that is being stored in HTML, then grabbed by PHP later on with $_POST['MAX_FILE_SIZE'] and then used by the function changerAvatarFile in the variable $max. This is a little inefficient and dangerous. You can just store the max file size directly in a PHP variable in the changerAvatarFile function, and that's it. Why is it dangerous? Well since HTML is client-side code, a malicious user could change the file size to whatever they wanted with the right tools; as large as they wanted which could end up filling up the web server hard drive, suck up your bandwidth and possibly even crash the web server. PHP is server-side so it is never seen client-side, it's much more secure.
;)

With regards,
MaTT
User avatar
MaTT
.
 
Posts: 3769
Joined: Fri Jun 17, 2011 12:07 pm
Location: England, East Sussex
Home-Game: Wolfenstein Enemy Territory
Favourite Weapon: 1.4 Turbo
Wolfenstein ET Name: #ET|TitaN*|
Steam Name: Mattipoika
Clan Wars: 0

Re: PHP Problems : Adding custom Avatar to server

Postby Straw » Tue Aug 04, 2015 10:50 am

Alright, thanks for your quick answer. I'll try to fix that.

About the max fil size, I'll edit that too, thanks for letting me know :D
User avatar
Straw
.
 
Posts: 8430
Joined: Fri Jun 17, 2011 2:35 pm
Location: France, Laval
Home-Game: Counter Strike - Global Offensive
Favourite Weapon: Spoon
Wolfenstein ET Name: #ET|Straw|
Steam Name: Mansikka
Clan Wars: 1


Return to Web & Forum Help

  • Advertisement