PHP Classes

PHP Upload File Save to Directory with Unique Name: Save an uploaded file with a unique name

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-26 (10 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 63 All time: 10,429 This week: 36Up
Version License PHP version Categories
savefile 1.0.0GNU General Publi...5.1.0HTTP, PHP 5, Files and Folders, Global
Description 

Author

This package can save an uploaded file with a unique name.

It provides a function that takes an entry from the $_FILES array with the details of a file uploaded to the Web server using the current PHP script.

The script processes the file by storing it in a given destination directory.

It changes the uploaded file with a name and a number that can be increased if another file already exists with the same file name.

Picture of ra br
Name: ra br <contact>
Classes: 1 package by
Country: El Salvador El Salvador
Age: ???
All time rank: 44765 in El Salvador El Salvador
Week rank: 227 Up1 in El Salvador El Salvador Up

Example

<?php

function saveFile($file, $path) {
   
$count = 0;
   
$originalFileName = $file['name'];
   
$fileName = $originalFileName;

    while (
file_exists($path . '/' . $fileName)) {
       
$count++;
       
$fileName = pathinfo($originalFileName, PATHINFO_FILENAME) . '(' . $count . ').' . pathinfo($originalFileName, PATHINFO_EXTENSION);
    }

    if (!
file_exists($path)) {
       
mkdir($path, 0777, true);
    }

   
move_uploaded_file($file['tmp_name'], $path . '/' . $fileName);

    if (
file_exists($path . '/' . $fileName)) {
        return
$fileName;
    } else {
        return
false;
    }
}

/**
 * How to USE
 */

/*
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $uploadDir = 'uploads';
    $uploadedFile = $_FILES['file'];

    $savedFileName = saveFile($uploadedFile, $uploadDir);

    // Here you can save the information to a DataBase
}
*/


  Files folder image Files (1)  
File Role Description
Accessible without login Plain text file saveFileWithUniqueName.php Example Function to verify unique name in uploaded file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:63
This week:0
All time:10,429
This week:36Up