71 en ligne Comparateur | Sites | Forum | PhpBB | Actu | Glossaire | Codes | Tips | Liens | Livres | Lettre  

 Recherche

 Newsletter





 

Upload image

Code PHP par jalss



  <Accueil>  Déjà 671 codes ! 
AJAX [4]AS 2.0 [1]ASP [110]ASP.NET [29]CSharp [7]CSS [13]
D [1]Delphi [5]DHTML [3]Divers [5]HTML [16]JavaScript [151]
JSP [9]Maths [1]PHP [252]PHP5 [18]PowerShell [19]Regex [4]
SQL [2]V.Basic [4]VBScript [11]VS.NET [5]XML [1]



jalssPHP - Upload image

c code permet l'upload d'image de votre DD vers votre site web.


Pour pouvoir le télécharger, connectez-vous ! ;)
<?php

/* loading language file */
function get_translations($lngfile) {

  if (file_exists($lngfile)) {
    $lines = file($lngfile);
    while (list(,$line) = each($lines)) {
      list($key,$val)= explode("=",$line);
          $phrases[$key] = $val;
    }
    return $phrases;
  } else {
    return false;
  }
}
/* translate a string */
function translate($str,$vals="") {

  global $phrases;

  if ($phrases) {
    $p_str = $phrases[$str];
        if ($p_str == "") {
          $p_str = $str;
        }
  } else {
    $p_str = $str;
  }

  if (gettype($vals)=="array") {
    $p_str = sprintf($p_str,$vals[0],$vals[1],$vals[2],$vals[3],$vals[4],$vals[5],$vals[6]);
  }

  return (trim($p_str));
}

$my_max_file_size         = "102400"; # in bytes
$image_max_width        = "800";
$image_max_height        = "800";
$the_path                        = "image/";

$registered_types = array(
                                        "application/x-gzip-compressed"         => ".tar.gz, .tgz",
                                        "application/x-zip-compressed"          => ".zip",
                                        "application/x-tar"                     => ".tar",
                                        "text/plain"                            => ".html, .php, .txt, .inc (etc)",
                                        "image/bmp"                             => ".bmp, .ico",
                                        "image/gif"                             => ".gif",
                                        "image/pjpeg"                           => ".jpg, .jpeg",
                                        "image/jpeg"                            => ".jpg, .jpeg",
                                        "application/x-shockwave-flash"         => ".swf",
                                        "application/msword"                    => ".doc",
                                        "application/vnd.ms-excel"              => ".xls",
                                        "application/octet-stream"              => ".exe, .fla (etc)"
                                        ); 

$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg");

# --

function form($error=false) {

global $PHP_SELF,$my_max_file_size,$lng;

        if ($error) print $error . "<br><br>";

        print "\n<form ENCTYPE=\"multipart/form-data\"  action=\"upload.php3\" method=\"post\">";
        print "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
        print "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
        print "\n<P>".translate("Upload images")."";
        print "\n<BR>NOTE: ".translate("Max file size is")." " . ($my_max_file_size / 1024) . "KB";
         print "\n<br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"35\"><br>";
        print "\n<input type=\"submit\" Value=\"Upload\">";
        print "\n</form>";

} # END form

# --

if (!ereg("^4",phpversion())) {
        function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
                for ($i=0; $i < count($haystack); $i++) {
                        if ($haystack[$i] == $needle) {
                                return true;
                        }
                }
        }
}

# --

function validate_upload($the_file) {

global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;

        $start_error = "\n<b>Error:</b>\n<ul>";

        if ($the_file == "none") { # do we even have a file?

                $error .= "\n<li>You did not upload anything!</li>";

        } else { # check if we are allowed to upload this file_type

                if (!in_array($the_file_type,$allowed_types)) {
                        $error .= "\n<li>The file that you uploaded was of a type that is not allowed, you are only
                                                allowed to upload files of the type:\n<ul>";
                        while ($type = current($allowed_types)) {
                                $error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
                                next($allowed_types);
                        }
                        $error .= "\n</ul>";
                }

                if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {

                        $size = GetImageSize($the_file);
                        list($foo,$width,$bar,$height) = explode("\"",$size[3]);

                        if ($width > $image_max_width) {
                                $error .= "\n<li>Your image should be no wider than " . $image_max_width . " Pixels</li>";
                        }

                        if ($height > $image_max_height) {
                                $error .= "\n<li>Your image should be no higher than " . $image_max_height . " Pixels</li>";
                        }

                }

                if ($error) {
                        $error = $start_error . $error . "\n</ul>";
                        return $error;
                } else {
                        return false;
                }
        }
} # END validate_upload

# --


function list_files() {

global $the_path;

        $handle = dir($the_path);
        print "\n<b>Uploaded files:</b><br>";
        while ($file = $handle->read()) {
                if (($file != ".") && ($file != "..")) {
                        print "\n" . $file . "<br>";
           }
        }
        print "<hr>";
}

# --

function upload($the_file) {

global $the_path,$the_file_name;

        $error = validate_upload($the_file);
        if ($error) {
                form($error);
        } else { # cool, we can continue
                if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
                        form("\n<b>Something barfed, check the path to and the permissions for the upload directory</b>");
                } else {
                        list_files();
                        form();
                }
        }
} # END upload

# --

############ Start page

print "<html>\n<head>\n<title>Upload example</title>\n</head>\n<body>";
echo("<table width=\"0%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">
  <tr align=\"left\"> 
    <td><font face=verdana size=2>");
switch($task) {
        case 'upload':
                upload($the_file);
        break;
        default:
                form();
}
echo("</font></td>
  </tr>
</table>");
print "\n</body>\n</html>";

?>

Proposé par jalss le 03/11/2004 [vu 8747 fois]
Commentaires

Pour participer, il faut d'abord vous identifier !
280 visiteurs ont déjà participé dont :
DB77 [46]fmarie [26]Fabrice69 [24]Didier [22]Bisou [20]
tedheu [20]CedX [18]mohman [17]romuluslepunk [15]foxmaster [13]
jreaux62 [13]Vonscott [11]iubito [10]Redo [9]Balin [8]
poppy [7]fmaunier [6]cyrillus [6]pseudomenace [6]CrazyCat [5]
antoun [5]Pozzy [5]TBliss [5]Aurelien [5]daoudi [5]
Ours [5]bahdot [5]david96 [5]Chrigou [4]debackp [4]
lex [4]hyoucef [4]Jips [4]zut69 [3]salem [3]
berguerand01 [3]phenixbd [3]twins20 [3]kandal [3]mhaido [3]
mcorgnet [3]philouonline [3]master-killer [3]crocxx [3]Maclearner [3]
laetyboop [3]Poucet [3]olivierland94 [3]

Didier Stockez et partagez gratuitement vos documents !!!

Au fil des news  
ASP.NET - Générateur de clefs de chiffrement
Hebergement - Dedibox - Serveurs dédiés à bas prix
Outils - ZoneCheck - Validateur de configuration DNS
Outils - Inno Setup
Outils - Doxygen - Générateur de documentation
Outils - TortoiseSVN
Composants - NUnit - Tests unitaires en .NET
Editeurs - MonoDevelop
.NET - Silverlight sur Linux
Divers - DSource.org - Open Source development for D
Divers - Vala Programming Language
Divers - D Programming Language
PowerShell - Afficher les informations de profil d'un user
PHP - Lister les combinaisons de p objets pris parmi n.
Utiliser un MOSS 2007 Search avec SPS 2003 - Comment utiliser un MOSS Search avec SPS Portail
Microsoft Office SharePoint Server 2007 (MOSS) fournit de nombreuses fonctionnalités qui ne sont ...
Modules - DHTMLgoodies.com
Dreamweaver CS3 + Php + Mysql - Objet formulaire - Les fondamentaux Part-2
La suite des fondamentaux, le bouton radio, la liste-menu (select) et la case cocher, sont ...
AJAX - Heure, Date et Fête du jour.
Magazines - Le n°114 de Programmez est disponible
JavaScript - Drag and Drop Javascript
Adobe - Mise à jour du plug-in Photoshop Camera Raw
Adobe - ADOBE lance un appel à candidature.....
PHP5 - Compteur de visites (hit) paramétrable (v1.1.0.a)
[MAJ] [PHP] Un calendrier très complet -
Remplacement de balises pour le PHP5 et modifications du script pour l'affichage des jours afin ...
Magazines - Le n°113 de Programmez est disponible
PHP - Compteur de visites (hit) paramétrable
Amélioration du Search MOSS : Recherche avancée - Modification de la page de recherche avancée
Dans la série des optimisations potentielles sur le moteur de recherche de MOSS, nous verrons cette ...
Dreamweaver CS3 + Php + Mysql - Ajout-Modif-Suppression. Les fondamentaux Part-1
Suite à de nombreuses questions sur le forum, je pense que ce sujet sur les comportements d'ajout, ...
SharePoint et Silverlight - Premiers pas - Lecture de données SharePoint et intégration
Silverlight et SharePoint sont deux technologies récentes et connaissant un essor significant. En ...
Développement d'applications SharePoint - depuis une Workstation XP ou Vista
Lorsque que l'on développe pour SharePoint, on se retrouve dans un dilemme auquel nous avons tous ...
JavaScript - sudoky - jeux du sudoku en javascript
Adobe - Creative Suite 4 en Français
PHP - La 8ème édition du Forum PHP est lancée !
Magazines - Le n°112 de Programmez est disponible
PHP - Interpreteur de commande SQL en PHP
Listes liées (dépendantes) : avec ou sans XMLHTTPR - Les délires «Ajax or not Ajax»
Comme toujours, c'est à la suite d'un coup de pouce que je me suis décidé à faire ce tuto. Merci à ...
Amélioration du Search MOSS : Les scopes - Quelques possibilités d'amélioration du Search
Lorsqu'on installe le moteur de recherche de MOSS et qu'on le configure basiquement, on veut ...
SharePoint - Lister les templates utilisés - Identifier les définitions utilisées par vos sites
Un site SharePoint est créé à partir d'un modèle ou définition de site. Mais comment savoir après ...
PowerShell - Profile avec la participation de mon ami Tigrou :)
Adobe - Lancement de la CS4, tous à vos agendas !
  Tutorial : HTML | Scripting | ASP-PHP | ASP.net | SQL Server | XML
Sharepoint | XAML | Pocket | Dreamweaver | VML | Divers
  Scripts : Scripting | ASP-PHP | ASP.net | Divers
  Boutique | Annuaire | Bannières | Météo | Tribune | Partenariats | Recherches
v4.3 © Didier YVER
2001-2009
 
 

Corpo Sciences de Reims Partitions gratuites Carte, météo, annonces
Codes Sources DotNet Project Wygwam El Roubio MVP The Inquirer FR ASP-magazine CodePPC TechNet Groupes Utilisateurs Microsoft