PHP uploader une image


Back to programmation.


moderator    (2009-03-31)

moderator

PHP uploader une image

Comment uploader une image sur un serveur Linux ?

Voilà des morceaux de code que j'utilise pour uploader une image sur le serveur en PHP, bien sûr il est possible de redimensionner, couper l'image et ainsi de suite (voir les fonctions PHP).

D'abord le formulaire :

<form enctype="multipart/form-data" action="process.php" method="post">
<input type="hidden" name="photo" value="photo" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
 
<table border="0" width="500" align="center">
  <tr>
    <td width="75">&nbsp;</td>
    <td>upload a photo, JPG format, max. size : 10 Mb<font size="1"><br><br></font>
      <input type="file" size="22" name="photo"> &nbsp; &nbsp; <input type="submit" name="send" value="Send" /></td>
  </tr>
</table>
</form>


Ensuite dans le fichier process.php vous devez vérifier l'image et la sauver, ici je vérifie seulement que l'image soit assez "carrée" puis je la redimensionne, la copie et en change ses droits :

if (!empty($_POST['photo']))
{
@unlink($photo_directory.$photo_file);

if (is_uploaded_file($_FILES['photo']['tmp_name']))
{
    $img_size = GetImageSize($_FILES['photo']['tmp_name']);

    if ($img_size[0] / $img_size[1] <= 1.5 AND $img_size[0] / $img_size[1] >= 0.666)
{
    rename($_FILES['photo']['tmp_name'], $photo_directory.$photo_file);

    chmod($photo_directory.$photo_file,0755);

    list($photo_width,$photo_height) = getimagesize($photo_directory.$photo_file);
 
    $photo_resource = imagecreatefromjpeg($photo_directory.$photo_file);

    if ($photo_width > 220) {  $new_height = $photo_height * 220 / $photo_width;  $new_width = 220; }
else  {  $new_height = $photo_height;  $new_width = $photo_width;  }

    $photo_resource_resampled = imagecreatetruecolor($new_width, $new_height);

    imagecopyresampled($photo_resource_resampled, $photo_resource, 0, 0, 0, 0, $new_width, $new_height, $photo_width, $photo_height);
    imagejpeg($photo_resource_resampled,$photo_directory.$photo_file);
}
}
}




(more options below)

*e1e614    (2009-04-22 14:19:46)
Merci !

Merci pour le bout de code, pile ce que je voulais.

Il y aurait moyen de rendre le code plus lisible dans le forum avec des couleurs ?


Voir aussi


ficgs
Plus de sources

Vous devez vous enregistrer pour consulter ces sources, vous pourrez alors changer leur ordre en cliquant sur les icones les précédant.



admin
Autres sources

Ce projet est collaboratif, vous pouvez remonter les sources suivantes dans la liste si vous les trouvez utiles.


 PHPScripts - Portions de scripts PHP / MySQL - Upload d'image
phpscripts fr > portions/portion.

 Upload d'image en PHP - Damien ALEXANDRE portfolio - Web-développeur Intégrateur
damienalexandre > Upload d image en

// Pas de 5, ne pas demander pourquoi ^^ (voir doc...
// un chtit regex pour remplacer tous ce qui n'est ni chiffre ni...

 PostImage.org - Image hosting, free photo sharing
postimage

You can upload images in following formats: jpg, png, gif, bmp, psd,...
Images that contain nudity need to be marked clearly by selecting...

 GetAFreelancer.com - PHP/AJAX Image Hosting
getafreelancer > projects

 Script PHP - * et la redimensionner | Fotozik
fotozik > wordpress/web/script

 [PHP] Upload de fichiers - La solution
commentcamarche > faq/sujet 889

 ES FC Malley Galerie Photos - Mettre une photo en ligne
esmalley > Galerie/upload.

 Portail PHP / * sur le serveur et stocker son nom une bdd
phportail > forum/ftopic1934.

 Formation Php: Upload d'image vers un serveur
formation php.blogspot > 2007/09/upload dimage vers un serveur 08

 Removing Width/Height from the Image Uploader | Weblog Tools Collection
weblogtoolscollection > archives/2008/05/29/removing widthheight from the image

Managing Trackbacks and Pingbacks in Your WordPress...
Plugin Deactivation Issues Solved With Actions and...

 Posteet: Pear::Quickform, callback de vérification des dimensions d'une image avant upload [images] [pear] [php] [upload]
posteet > view/54

Pear::Quickform, callback de vérification des dimensions d'une image...

 mootools forums / Fancy Upload by digitarald
forum.mootools > viewtopic.

  * depuis une url avec Php - Alsacréations
forum.alsacreations > topic 20 28925 1 Uploader une image depuis une url avec

if (!preg_match('#Content-Length: ([0-9]+)[^ /][s]+#i', $data,...
Bonjour, j'aimerai savoir si il existe une fonction ou bien une...

 TYPO3 3.7.0: typo3_src-3.7.0/typo3/ext/rte/app/locallang_rte_select_image.php Source File
doc typo3.ameos > 3.7.0/locallang rte select image 8

 WebMotionUK - DEMO PHP & Jquery image upload & crop
webmotionuk > jquery/image upload crop.

function preview(img, selection) { var scaleX = 100 /...

 Joomla! • View topic - Datso Gallery impossible d'uploader des images
forum.joomla > viewtopic.

Je ne comprends pas d'où vient ce pb??? Comment le solutionner...
Bonjour Je viens d'installer Datsogallery mais voila lorsque...

 La webcam
plevenon meteo > webcam description

 Free File Hosting Made Simple - MediaFire
mediafire

More than just a file hosting web site, MediaFire has the features...
An account allows you to manage, update, organize, and secure...

 migrated from 1.5.1 to 1.5.9 cannot add photos anymore | Gallery
gallery.menalto > node/81743

All logos, trademarks and content in this site are property of...

 Upload d'une image par url.. - Page : 1 - PHP - Programmation - FORUM HardWare.fr
forum.hardware > hfr/Programmation

[JAVA ou ACTIVE X] Cherche soultions pour resize image +...
Utiliser http et gérer, utiliser les fonctions de lecture de...
pour télécharger des images à partir d'une url alors...

 How To Develop in PHP: Create an Avatar/Profile Image Upload (Part 3) - Howcast - Recherche vidéo sur Truveo
fr.truveo > How To Develop in

 TinyMCE Forum / getting some {#history.special_folder_title} displayed in Imagemanager
tinymce.moxiecode > punbb/viewtopic.

 L'upload de fichier (sécurisé)
vulgarisation informatique > upload

!empty($_FILES['fichier_source']['tmp_name']) AND...
//Si le Type est JPEG (correspond au chiffre 2) on copie...

 Impossible d' * - Forum des développeurs
developpez > forums/d304118

 Mosaic Maker: Create a photo mosaic from your digital photographs
bighugelabs > flickr/mosaic.

 [PHP] Can't upload PNG image throught IE - Web Hosting Talk - The largest, most influential web hosting community on the Internet
webhostingtalk > showthread.

Cloud Host Terremark Will Expand Data Center in Brazil to Meet Cloud...
• Get your questions answered by community gurus and industry...

 [PHP] Renommer une image uploadée [Archive] - La Communauté Informatique NDFR.net
ndfr > forums/archive/index.

Oui mais tant qu'à vouloir renommer auant utiliser la fonction...
Une question au passage Zyk : si jamais tu as deux personnes qui ont...
J'disais ça pour faire remarquer que la recherche du dernier id ou...

 Coppermine Java Image Uploader - CoppermineWiki
frantzk.free > WikiFree/index.

Dernière modification de cette page le 9 septembre 2010 à...

 Probleme upload image avec formulaire sur une base
presence pc > forum/ppc/Programmation/probleme upload image formulaire base suje

1/ c'est quoi le problème ? 2/ et les blob c'est pour les...
Bonjour g créé une base de donnée avec Nom, Prénom, PHOTO...

 Download Fie Upload Using Php Source Codes, Fie Upload Using Php Scripts - Upload PHP by Franciscocharrua.com, Easy PHP Upload by Olaf Lederer and mor...
sourcecodeonline > list?q=fie upload using

This program is built by using PHP that allows the car resellers,...
The OSSuite is a script written using PHP which is a combined program...

 IImage Browser | skriker
fredfred > skriker/index.

similarly copied and modified the various thumb cases and thumblink...
уборка, чистка, cleaning, клининг, clean,...
2) В файле phpWaterMark вначале есть...



Response  
 

Guest name   (option)     Register
Please sum : 4493 + five  




Trackbacks : If you talked about this article in your blog or website, you may instantly get a backlink 
There's no trackback at the moment.