16/02/2008, 23:36
|
| | | Fecha de Ingreso: junio-2006
Mensajes: 550
Antigüedad: 18 años, 7 meses Puntos: 7 | |
Re: Subir Imagen class.upload.php
Código:
<?php
// +------------------------------------------------------------------------+
// | class.upload.php |
// +------------------------------------------------------------------------+
// | Copyright (c) Colin Verot 2003-2007. All rights reserved. |
// | Version 0.25 |
// | Last modified 17/11/2007 |
// | Email [email protected] |
// | Web http://www.verot.net |
// +------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License version 2 as |
// | published by the Free Software Foundation. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the |
// | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
// | Boston, MA 02111-1307 USA |
// | |
// | Please give credit on sites that use class.upload and submit changes |
// | of the script so other people can use them as well. |
// | This script is free to use, don't abuse. |
// +------------------------------------------------------------------------+
//
/**
* Class upload
*
* @version 0.25
* @author Colin Verot <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @copyright Colin Verot
* @package cmf
* @subpackage external
*/
/**
* Class upload
*
* <b>What does it do?</b>
*
* It manages file uploads for you. In short, it manages the uploaded file,
* and allows you to do whatever you want with the file, especially if it
* is an image, and as many times as you want.
*
* It is the ideal class to quickly integrate file upload in your site.
* If the file is an image, you can convert, resize, crop it in many ways.
* You can also apply filters, add borders, text, watermarks, etc...
* That's all you need for a gallery script for instance. Supported formats
* are PNG, JPG, GIF and BMP.
*
* You can also use the class to work on local files, which is especially
* useful to use the image manipulation features.
*
* The class works with PHP 4 and 5, and its error messages can
* be localized at will.
*
* <b>How does it work?</b>
*
* You instanciate the class with the $_FILES['my_field'] array
* where my_field is the field name from your upload form.
* The class will check if the original file has been uploaded
* to its temporary location (alternatively, you can instanciate
* the class with a local filename).
*
* You can then set a number of processing variables to act on the file.
* For instance, you can rename the file, and if it is an image,
* convert and resize it in many ways.
* You can also set what will the class do if the file already exists.
*
* Then you call the function {@link process} to actually perform the actions
* according to the processing parameters you set above.
* It will create new instances of the original file,
* so the original file remains the same between each process.
* The file will be manipulated, and copied to the given location.
* The processing variables will be reseted once it is done.
*
* You can repeat setting up a new set of processing variables,
* and calling {@link process} again as many times as you want.
* When you have finished, you can call {@link clean} to delete
* the original uploaded file.
*
* If you don't set any processing parameters and call {@link process}
* just after instanciating the class. The uploaded file will be simply
* copied to the given location without any alteration or checks.
*
* Don't forget to add <i>enctype="multipart/form-data"</i> in your form
* tag <form> if you want your form to upload the file.
*
* <b>How to use it?</b><br>
* Create a simple HTML file, with a form such as:
* <pre>
* <form enctype="multipart/form-data" method="post" action="upload.php">
* <input type="file" size="32" name="image_field" value="">
* <input type="submit" name="Submit" value="upload">
* </form>
* </pre>
* Create a file called upload.php:
* <pre>
* $handle = new upload($_FILES['image_field']);
* if ($handle->uploaded) {
* $handle->file_new_name_body = 'image_resized';
* $handle->image_resize = true;
* $handle->image_x = 100;
* $handle->image_ratio_y = true;
* $handle->process('/home/user/files/');
* if ($handle->processed) {
* echo 'image resized';
* $handle->clean();
* } else {
* echo 'error : ' . $handle->error;
* }
* }
* </pre>
*
* <b>How to process local files?</b><br>
* Use the class as following, the rest being the same as above:
* <pre>
* $handle = new upload('/home/user/myfile.jpg');
* </pre>
*
* <b>How to set the language?</b><br>
* Instantiate the class with a second argument being the language code:
* <pre>
* $handle = new upload($_FILES['image_field'], 'fr_FR');
* $handle = new upload('/home/user/myfile.jpg', 'fr_FR');
* </pre>
*
* <b>How to output the resulting file or picture directly to the browser?</b><br>
* Simply call {@link process}() without an argument (or with null as first argument):
* <pre>
* $handle = new upload($_FILES['image_field']);
* header('Content-type: ' . $handle->file_src_mime);
* echo $handle->Process();
* die();
* </pre>
* Or if you want to force the download of the file:
* <pre>
* $handle = new upload($_FILES['image_field']);
* header('Content-type: ' . $handle->file_src_mime);
* header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
* echo $handle->Process();
* die();
* </pre>
*
* <b>Processing parameters</b> (reseted after each process)
* <ul>
* <li><b>file_new_name_body</b> replaces the name body (default: '')<br>
* <pre>$handle->file_new_name_body = 'new name';</pre></li>
* <li><b>file_name_body_add</b> appends to the name body (default: '')<br>
* <pre>$handle->file_name_body_add = '_uploaded';</pre></li>
* <li><b>file_new_name_ext</b> replaces the file extension (default: '')<br>
* <pre>$handle->file_new_name_ext = 'txt';</pre></li>
* <li><b>file_safe_name</b> formats the filename (spaces changed to _) (default: true)<br>
* <pre>$handle->file_safe_name = true;</pre></li>
* <li><b>file_overwrite</b> sets behaviour if file already exists (default: false)<br>
* <pre>$handle->file_overwrite = true;</pre></li>
* <li><b>file_auto_rename</b> automatically renames file if it already exists (default: true)<br>
* <pre>$handle->file_auto_rename = true;</pre></li>
* <li><b>auto_create_dir</b> automatically creates destination directory if missing (default: true)<br>
|