![]() |
![]() |
Hawkfield: web development & web designYour partner in IT - Gent Belgium
Let users upload imagesPHP has made if very easy for us to implement uploading of images into our websites. Wheter it is for a galery script, forum avatars or anything else, it is very easy to implement. The first thing you need is a form where the users can select there images. An example is below, but you can have other elements on it, or even multiple files. <form name="UploadExample" enctype="multipart/form-data" action="upload.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="40000" /> File to upload: <input name="file" type="file" /> <input type="submit" value="Upload" /> </form> This is the most basic form you will need to upload files. Save this into any HTML file. "multipart/form-data" is needed for uploading data, without it, the form will not work. "upload.php" is the script we will use to handle the upload. "MAX_FILE_SIZE" is used to specify the maximum alowed size (in bytes) of the file. In upload.php you have something liek this: if (is_uploaded_file($_FILES['file']['tmp_name'])) { // This is to make sure the file was actually uploaded if ($_FILES['file']['size'] < 40000) { // The file is within the limit of 40 KB if (@imagecreatefromgif($_FILES['file']['tmp_name']) || @imagecreatefromjpeg($_FILES['file']['tmp_name']) || @imagecreatefrompng($_FILES['file']['tmp_name'])) { // This is to ensure that the file is really an image. // There are other ways to do this, but they depend on what the brower sends you. move_uploaded_file ($_FILES['file']['tmp_name'], '/home/usr/YourPath/DesiredFilename); // In the end, this moves the file to the place you want. } else { print ("Only upload JPEG, PNG or GIF files"); } } else { print ("Your file is to big, choose a smaller image."); } } else { print ("Oopsie, there was an error."); } |
||||||