Upload

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

php class for uploading files

License

i-jurij/upload

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Only for single or multiple file uploads in next format: A) or B)
$this __construct normalize $_ FILES_array: 
$this->files = ['input_name_for_single_upload' => [ 0 => ['name', 'full_path', 'type', 'tmp_name', 'error', 'size'] ], 'input_name_for multiple_uploads' => [ 0 => ['name', 'full_path', 'type', 'tmp_name', 'error', 'size'], 1 => ['name', 'full_path', 'type', 'tmp_name', 'error', 'size'] ] ] 
$this->isset_data(): check if $this->files not empty (this means in $_FILES is also not empty) 
therefore, after creating an instance of the class and checking the existence of the input data, there are always two foreach, and then $this->execute($input, $key, $file) eg isset_data()) < foreach ($load->files as $input => $input_array) < print 'Input "'.$input.'":
'; // SET the vars for class if ($input === 'file') < $upload->propeties = ''; > foreach ($input_array as $key => $file) < print 'Name "'.$file['name'].'":
'; // PROCESSING DATA if ($load->execute($input, $key, $file) && !empty($load->message)) < print $load->message; print '
'; > else < if (!empty($load->error)) < print $load->error; print '
'; > continue; > > > >
$this->execute(): check input data: $this->dest_dir required check error: in FILES check_dest_dir: $this->create_dir default false, $this->dir_permissions default 0755; check_file_size: if user not set $this->file_size - default 1024000B (1MB), set in bytes eg 2*100*1024 (200KB) check_mime_type: if user not set $this->file_mimetype -default any, string or array, 'audio' or ['image/bmp', 'audio', 'video'], if user set full mimetype eg imge/bmp - the class also check the extension check_extension: if user not set $this->file_ext -default any, string or array, eg 'jpg', ['.png', '.webp', 'jpeg'] check_new_file_name: use $this->translit_ostslav_to_lat, for other - replace with $this->translit_to_lat move_upload: upload file to dir (dir = tmp dir if user set $this->processing or $this->tmp_dir, else - dir = dest dir) 
Working example (index.php in the same folder as class folder) :          isset_data()) < ?>

Back

files as $input => $input_array) < //print_r($input_array); print '
'; print 'Input "'.$input.'":
'; foreach ($input_array as $key => $file) < if (!empty($file['name'])) < if (mb_strlen($file['name'], 'UTF-8') < 101) < $name = $file['name']; >else < $name = mb_strimwidth($file['name'], 0, 48, ". ") . mb_substr($file['name'], -48, null, 'UTF-8'); >print 'Name "'.$name.'":
'; > // SET the vars for class $load->create_dir = true; // let create dest dir if not exists if ($input === 'file') < $load->dest_dir = 'upload_files'; // where upload file after postprocessing $load->tmp_dir = ''; // temporary dir for upload file before postprocessing $load->dir_permissions = 0777; // permissions for dest dir $load->file_size = 3*100*1024; //300KB - size for upload files = MAX_FILE_SIZE from html $load->file_permissions = 0666; // permissions for the file being created $load->file_mimetype = ['text/php', 'text/x-php', 'text/html']; // allowed mime-types for upload file $load->file_ext = ['.php', 'html']; // allowed extension for upload file $load->new_file_name = ''; // new name of upload file, string 'filename' or array ['filename, 'noindex'] - where noindex for input with multiple uploads (but you must get different name for file) $load->replace_old_file = false; // replace old file in dest dir with new upload file with same name > if ($input === 'picture') < $load->default_vars(); $load->dest_dir = 'upload_files'; $load->file_size = 1*1000*1024; //1MB $load->file_mimetype = ['image/jpeg', 'image/pjpeg', 'image/png', 'image/webp']; $load->file_ext = ['.jpg', '.jpeg', '.png', '.webp']; > if ($input === 'pictures') < $load->default_vars(); $load->dest_dir = 'upload_files'; $load->tmp_dir = 'tmp'; $load->dir_permissions = 0777; $load->file_permissions = 0666; $load->file_size = 1*1000*1024; //1MB $load->file_mimetype = ['image/jpeg', 'image/pjpeg', 'image/png', 'image/webp']; $load->file_ext = ['.jpg', '.jpeg', '.png', '.webp']; $load->new_file_name = ['zzz', 'index']; // ['', 'noindex'], 'new_filename' $load->replace_old_file = true; > // PROCESSING DATA if ($load->execute($input_array, $key, $file)) < if (!empty($load->message)) < print $load->message; print '
'; > > else < if (!empty($load->error)) < print $load->error; print '
'; > continue; > // // some command for processing a file located in a tmp_dir or dest_dir // > > //CLEAR FOLDER - file will be deleted in a directory specified by user if (!$load->del_files_in_dir('tmp')) < if (!empty($load->error)) < print $load->error; print '
'; > > > else < ?>

?>

About

php class for uploading files

Читайте также:  Glpi nginx php fpm

Источник

verot.net

class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.

class.upload.php

upload.jpg

This PHP script uploads files and manipulates images very easily. The perfect script to generate thumbnails or create a photo gallery! It can convert, resize and work on uploaded images in many ways, apply effects, add labels, watermarks and reflections and other image editing features. You can use it for files uploaded through an HTML form, a Flash uploader, XMLHttpRequest or on local files. It uses the GD library. This script is released under the GPL Version 2, and has been downloaded a few million times in the last few years! If your project is not GPL, commercial licenses are available.

Please note that the lastest code and documentation is now on GitHub!

What is it?

An example will be more useful than any blurb I can write :

namespace Verot\Upload; $foo = new Upload($_FILES['form_field']); if ($foo->uploaded) < // save uploaded image with no changes $foo->process('/home/user/files/'); if ($foo->processed) < echo 'original image copied'; >else < echo 'error : ' . $foo->error; > // save uploaded image with a new name $foo->file_new_name_body = 'foo'; $foo->process('/home/user/files/'); if ($foo->processed) < echo 'image renamed "foo" copied'; >else < echo 'error : ' . $foo->error; > // save uploaded image with a new name, // resized to 100px wide $foo->file_new_name_body = 'image_resized'; $foo->image_resize = true; $foo->image_convert = 'gif'; $foo->image_x = 100; $foo->image_ratio_y = true; $foo->process('/home/user/files/'); if ($foo->processed) < echo 'image renamed, resized x=100 and converted to GIF'; $foo->clean(); > else < echo 'error : ' . $foo->error; > >

What does it actually do in this example?

This example uses a file uploaded via a form. With this single file, it will :

  • copy the file without any modification
  • copy the file after having changed its name to ‘foo’
  • copy the file, resizing it to x=100, y=auto, converting it to GIF
Читайте также:  Financial data analysis with python

How does it work?

  • the class constructor upload handles a uploaded file (it can also handle a local file)
  • some optional parameters can be set up to act on the file during the process
  • the process is called with as an argument the destination directory on the server. If some parameters have been set up, the class will rename, resize, convert files and images
  • when the uploaded file is not needed anymore, we can delete it using clean.

Why use this class?

  • with one uploaded file, you can do as many copies, convertions, resizing you want.
  • you can resize each image dimension, or both, keeping the image ratio or not.
  • you can choose to resize an image only if it is bigger -or smaller- than the wanted sizes
  • you can manipulate the image in many ways, play with colors, add borders, labels and watermarks.
  • its use is simplistic but powerful
  • you can work on uploaded files or local files, useful for batch processing
  • XMLHttpRequest and Flash uploads are fully supported
  • MIME type detection is very comprehensive
  • more than 60 documented configuration settings
  • a lot of variables are set up during the process. You can retrieve all these values after a process.
  • error messages are understandable, and a variable log allows you to see what the class does.
  • it is translated in more than 25 languages
  • it is already widely used on Internet
  • it is free 🙂

Is it out there on Internet?

The class.upload.php source code has been downloaded well over 1 000 000 times over the last ten years. It is also a key part of the popular K2 content component for Joomla!, downloaded over 2 000 000 times, and has been implemented in numerous CMS and frameworks!

Читайте также:  Java приложения для студентов

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

validate and upload files more easily using php

License

Inambe/php-file-uploading-class

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Validate and upload files on server more easily using PHP.

All you need to get started with this class is, include or autoload this class into you project/file.

We can start using this class by adding this simple code into your form submiting code block. [ file(containing form) dirctory is default upload directory ]

$a = new Upload("input_field_name"); $a->do_upload(); 

Set upload directory (optional)

We can pass second parameter(string) for defining upload directory. [ Don’t forgot the ending slash in directory name ]

$a = new Upload("input_field_name","uploaded_files/"); $a->do_upload(); 

Maximum file size validation (optional)

We can add maximum file size(Megabytes) parameter in order to validate file size. [ default=5MB ]

$a = new Upload("input_field_name","directory_to_upload/",10); $a->do_upload(); 

File extension validation (optional)

We can pass an array of allowed extensions to validate file extension. [ default = [«jpg»,»png»,»gif»] ]

$a = new Upload("input_field_name","directory_to_upload/",10,["png","jpg"]); $a->do_upload(); 

Checking for errors and warnings

we can use errors() method to get array of errors and warnings. Remember do_upload() will return false if there will be any error or warning.

$a = new Upload("input_field_name","directory_to_upload/",10,["png","jpg"]); if($a->do_upload())< $a->file_name(); // will return file name $a->file_url(); // will return file url(current) // file is uploaded do whatever you want to do >else< foreach ($a->errors() as $error) < echo $error."
"; > die(); >

check test.php for complete working example.

Источник

Оцените статью