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   View files Example   View files View files (1)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-26 (6 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 62 All time: 10,433 This week: 75Up
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: 44745 in El Salvador El Salvador
Week rank: 276 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  
File Role Description
Accessible without login Plain text file saveFileWithUniqueName.php Example Function to verify unique name in uploaded file

 Version Control Unique User Downloads Download Rankings  
 0%
Total:62
This week:0
All time:10,433
This week:75Up