Published on : 05.05.2010 Category : PHP Classes Viewed : 93 times.
It’s sometimes cumbersome to handle uploaded files – checking if it is really uploaded, moving and renaming. Why not writing all these chores into a class and make our own file upload script?
First we are going to create a simple class to handle uploaded files and move them to some place we designate for convenient access. The PHP Class (PHP5, Uploaded.class.php)
Code:
<?php class Uploaded { private $field_name = ''; // $field_name is the name of the input in uploading form public function __construct($field_name) { $this -> field_name = $field_name; } // $path is the path to the directory where // the uploaded file you want stored. // $primary_name is the primary part of the // file name you want the new name of the uploaded file to be // such as 'song' with 'song.mp3'. The extensions part of the // new name will be determined from that of the uploaded one. public function getFileName($path, $primary_name = '') { if (empty($path)) { return false; } if (empty($primary_name)) { // Use microtime() as temporary file name if no $primary_name is given $primary_name = microtime(); } if (is_uploaded_file($_FILES[$this -> field_name]['tmp_name'])) { $client_name = basename($_FILES[$this -> field_name]['name']); $ext = substr($client_name, strrpos($client_name, '.')); $server_name = $primary_name.$ext; if (file_exists($path.$server_name)) { // deleting any existing files with the same name here // so that the old file can be updated this way. unlink($path.$server_name); } if (move_uploaded_file($_FILES[$this -> field_name]['tmp_name'], $path.$server_name)) { // The new name of the uploaded file will be returned // for access and retrieval of it. return $server_name; } } return false; } } ?>
Fair enough, now we know browsers are going to send upload.php a file accessible in $_FILES via ‘photo’. The PHP (upload.php)
Now we will use the Uploaded class (Uploaded.class.php) to handle the file sent from the HTML uploading form above.
Code:
<?php require_once('Uploaded.class.php'); if (isset($_POST['submit'])) { $myPhoto = new Uploaded('photo'); $photoFileName = $myPhoto -> getFileName('uploaded/photos/', 'new_name'); } ?>
The uploaded file will now be all ready and accessible in uploaded/photos/new_name.ext (ext is whatever file extension the original file has) with $photoFileName containing its new name.