CodeIgniter Forums

Full Version: File upload
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,


I'm using
PHP Code:
$file->move(WRITEPATH.'uploads'$fileName); 
to upload a file.

It's working but how to overwrite/replace the uploaded file with the same name?


Thank you
Here, I just wrote this real quick works but could use some more error checking.

Place this in a helper and load it in the BaseController helpers array.

PHP Code:
/**
 *  renameFile ()
 * -----------------------------------------------------------------------
 * The path is hard coded to the WRITEPATH/uploads.
 *
 * USAGE:
 *
 * renameFile($oldFileName, $newFileName)
 *
 * $oldFileName 'robots.txt'
 * $newFileName 'test.txt'
 */
if ( ! function_exists('renameFile'))
{
    
/**
     * renameFile ()
     * -------------------------------------------------------------------
     *
     * @param  string $oldFileName
     * @param  string $newFileName
     * @return bool
     */
    
function renameFile(string $oldFileName ''string $newFileName '')
    {
        if (
$oldFileName == '' or $newFileName == '') {
            return 
false;
        }

        return 
rename(
            
WRITEPATH "/uploads/" $oldFileName,
            
WRITEPATH "/uploads/" $newFileName
        
);
    }

(10-14-2020, 11:26 PM)InsiteFX Wrote: [ -> ]Here, I just wrote this real quick works but could use some more error checking.

Place this in a helper and load it in the BaseController helpers array.

PHP Code:
/**
 *  renameFile ()
 * -----------------------------------------------------------------------
 * The path is hard coded to the WRITEPATH/uploads.
 *
 * USAGE:
 *
 * renameFile($oldFileName, $newFileName)
 *
 * $oldFileName 'robots.txt'
 * $newFileName 'test.txt'
 */
if ( ! function_exists('renameFile'))
{
 
/**
 * renameFile ()
 * -------------------------------------------------------------------
 *
 * @param  string $oldFileName
 * @param  string $newFileName
 * @return bool
 */
 
function renameFile(string $oldFileName ''string $newFileName '')
 {
 if (
$oldFileName == '' or $newFileName == '') {
 return 
false;
 }

 return 
rename(
 
WRITEPATH "/uploads/" $oldFileName,
 
WRITEPATH "/uploads/" $newFileName
 
);
 }


Thank you for your help. I greatly appreciate it. Maybe can use it somewhere later.


-----


Problem solved. We need to put third parameter to true. But it is not written in our user guide  Smile 

PHP Code:
$file->move(WRITEPATH.'uploads'$fileNametrue);