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 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 56 This week: 2All time: 10,477 This week: 45Up
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

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:56
This week:2
All time:10,477
This week:45Up