[php] PHP - Move a file into a different folder on the server

I need to allow users on my website to delete their images off the server after they have uploaded them if they no longer want them. I was previously using the unlink function in PHP but have since been told that this can be quite risky and a security issue. (Previous code below:)

if(unlink($path.'image1.jpg')){ 
     // deleted
}

Instead i now want to simply move the file into a different folder. This must be able to be done a long time after they have first uploaded the file so any time they log into their account. If i have the main folder which stores the users image(s):

user/

and then within that a folder called del which is the destination to put their unwanted images:

user/del/

Is there a command to move a file into a different folder? So that say:

user/image1.jpg

moves to/becomes

user/del/image1.jpg

This question is related to php directory move unlink

The answer is


use copy() and unlink() function

$moveFile="path/filename";
if (copy($csvFile,$moveFile)) 
{
  unlink($csvFile);
}

If you want to move the file in new path with keep original file name. use this:

$source_file = 'foo/image.jpg';
$destination_path = 'bar/';
rename($source_file, $destination_path . pathinfo($source_file, PATHINFO_BASENAME));

Use the rename() function.

rename("user/image1.jpg", "user/del/image1.jpg");

Use file this code

function move_file($path,$to){
   if(copy($path, $to)){
      unlink($path);
      return true;
   } else {
     return false;
   }
 }

Create a function to move it:

function move_file($file, $to){
    $path_parts = pathinfo($file);
    $newplace   = "$to/{$path_parts['basename']}";
    if(rename($file, $newplace))
        return $newplace;
    return null;
}

Some solution is first to copy() the file (as mentioned above) and when the destination file exists - unlink() file from previous localization. Additionally you can validate the MD5 checksum before unlinking to be sure


shell_exec('mv filename dest_filename');


I using shell read all data file then assign to array. Then i move file in top position.

i=0 
for file in /home/*.gz; do
    $file
    arr[i]=$file
    i=$((i+1)) 
done 
mv -f "${arr[0]}" /var/www/html/

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to directory

Moving all files from one directory to another using Python What is the reason for the error message "System cannot find the path specified"? Get folder name of the file in Python How to rename a directory/folder on GitHub website? Change directory in Node.js command prompt Get the directory from a file path in java (android) python: get directory two levels up How to add 'libs' folder in Android Studio? How to create a directory using Ansible Troubleshooting misplaced .git directory (nothing to commit)

Examples related to move

Gradle - Move a folder from ABC to XYZ Moving all files from one directory to another using Python Move column by name to front of table in pandas PHP - Move a file into a different folder on the server How to move table from one tablespace to another in oracle 11g Batch file to move files to another directory Windows batch script to move files How to move files using FTP commands Moveable/draggable <div> How to move a marker in Google Maps API PHP - Move a file into a different folder on the server permission denied - php unlink Unlink of file Failed. Should I try again?