1

First, sorry for my bad english ...

The function is_uploaded_file always return false. Just before, I try this :

var_dump($file);
echo "<br>".$file['tmp_name'];

and display :

array(5) { ["name"]=> string(11) "cp_0001.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpmg5V5o" ["error"]=> int(0) ["size"]=> int(317616) }
/tmp/phpmg5V5o

No errors (0), 777 permission for the folder ... I tried realpath (from : Why does image upload fail php's is_uploaded_file check?), but still doesn't work.

Full fonction :

function fileUpload($file, $path_dir, $max_size)
  {
    var_dump($file);
    echo "<br>".$file['tmp_name'];
    // Si le fichier a bien été upload sur le serveur
    if(is_uploaded_file($file['tmp_name']))
    { 
      // Si il n'y a pas de caractères invalides
      if(preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $file['tmp_name']))
      {  
        // Si il n'y a pas d'erreurs lors d'upload
        if($file['error'] == 0)
        { 
          // Si la taille est < $max_size
          if($file['size'] <= $max_size)
          { 
            $infosfichier = pathinfo($file['name']);
            $extension_upload = $infosfichier['extension'];
            $extensions_autorisees = array('jpg', 'jpeg', 'png');
            // Si l'extension est autorisé   
            if(in_array($extension_upload, $extensions_autorisees)) 
            {                  
              // Si le fichier a bien été déplacé    
              global $max_id;   
              $dest = $path_dir.'even_'.$max_id['max'].'.'.$extension_upload;
              if(move_uploaded_file($file['tmp_name'], $dest))
                return true;                      
              else
              { 
                $_SESSION['erreur'] = "Erreur lors de l'upload : ".$dest;  
                return false;                                  
              }
            } else 
            {
              $_SESSION['erreur'] = "Le fichier dois etre une image au format JPG/JPEG/PNG";  
              return false;                                     
            }
          } else 
          {
            $_SESSION['erreur'] = "Fichier supérieur a ".$max_size." Ko.";   
            return false;
          }
        } else 
        {
          $_SESSION['erreur'] = "Erreur de l'envoi de la photo.";  
          return false;
        }   
      } else 
      {
        $_SESSION['erreur'] = "Nom de fichier invalide";   
        return false;
      }
    } else 
    {
      $_SESSION['erreur'] = "Erreur lors de la vérification du fichier";  
      return false;
    }
  }

Call of function :

if(isset($_FILES['photo']) AND !empty($_FILES['photo']['name']))
    $var = fileUpload($_FILES['photo'],'banniere/','512000');   

Thanks

Community
  • 1
  • 1
  • 1
    Possible duplicate of [Why does image upload fail php's is\_uploaded\_file check?](http://stackoverflow.com/questions/5202306/why-does-image-upload-fail-phps-is-uploaded-file-check) – Tom Apr 08 '16 at 22:31
  • Same error but the solution given doesn't work for me :) – mike thomson Apr 09 '16 at 08:27
  • Try `is_file($file['tmp_name'])` and `is_readable($file['tmp_name'])`, `is_readable('/tmp/')` – what results do those give? – CBroe Apr 09 '16 at 08:34
  • And do you have PHP error reporting enabled? – CBroe Apr 09 '16 at 08:35

1 Answers1

0

Bonjour, I found the problem ! It was my fault, again ! I'm including a file at the top of each form to avoid automatic returns with F5.

<?php
if(!empty($_POST) OR !empty($_FILES))
{
    $_SESSION['sauvegarde'] = $_POST ;
    $_SESSION['sauvegardeFILES'] = $_FILES ;

    $fichierActuel = $_SERVER['PHP_SELF'] ;
    if(!empty($_SERVER['QUERY_STRING']))
    {
        $fichierActuel .= '?' . $_SERVER['QUERY_STRING'] ;
    }
    header('Location: ' . $fichierActuel);
    exit;
}

if(isset($_SESSION['sauvegarde']))
{
    $_POST = $_SESSION['sauvegarde'] ;
    $_FILES = $_SESSION['sauvegardeFILES'] ;

    unset($_SESSION['sauvegarde'], $_SESSION['sauvegardeFILES']);
}
?>

But I still doesn't understand why, can someone explain ? Tjks