Reputation: 4111
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
Upvotes: 239
Views: 527801
Reputation: 11
function xmove($src,$dst)
{
if(is_dir($src)===true)
{ $dir=opendir($src);
while (($file = readdir($dir)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
if (is_dir($src."/".$file) === true) {
if (is_dir("$dst/$file") === false) {
mkdir("$dst/$file");
}
//echo basename("$file")."<br>";
xmove($src."/".$file,$dst."/".$file);
}
else {
copy("$src/$file", "$dst/$file");
unlink("$src/$file");
}
}
closedir($dir);
rmdir($src);
return true;
}else
return false;
}
Upvotes: 1
Reputation: 19
Use file this code
function move_file($path,$to){
if(copy($path, $to)){
unlink($path);
return true;
} else {
return false;
}
}
Upvotes: 2
Reputation: 29
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/
Upvotes: 2
Reputation: 129
use copy() and unlink() function
$moveFile="path/filename";
if (copy($csvFile,$moveFile))
{
unlink($csvFile);
}
Upvotes: 0
Reputation: 178
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;
}
Upvotes: 1
Reputation: 691
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
Upvotes: 2
Reputation: 10714
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));
Upvotes: 33
Reputation: 7762
The rename
function does this
rename('image1.jpg', 'del/image1.jpg');
If you want to keep the existing file on the same place you should use copy
copy('image1.jpg', 'del/image1.jpg');
If you want to move an uploaded file use the move_uploaded_file
, although this is almost the same as rename
this function also checks that the given file is a file that was uploaded via the POST
, this prevents for example that a local file is moved
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
code snipet from docs
Upvotes: 532
Reputation: 32127
Use the rename() function.
rename("user/image1.jpg", "user/del/image1.jpg");
Upvotes: 123