Moving uploaded files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Moving uploaded files

Post by chris98 »

Hello, I was wondering if it is possible to move uploaded files from one location on the webserver to another?

What I want to do is have people upload their files, then after they have been checked by a moderator, I want the moderator to be able to move them to a different location, whilst still on the site.Does such a code exist or not?
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Moving uploaded files

Post by mecha_godzilla »

Hi,

I think you can use rename() for this purpose:

http://uk1.php.net/manual/en/function.rename.php

Note that the file itself does not have to be renamed, but you can rename the absolute path to the file (as with the "mv" command in Un*x).

HTH,

Mecha Godzilla
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Moving uploaded files

Post by chris98 »

Thanks, I've got it working now.Do you know of any way to use an if statement to echo whether it moved or not?

So far, I have this, but what would make this work?

if (!rename("uploader/uploads/".$file_name.".zip", "downloads/".$file_name.".zip"));
{
echo "Not successful"
}
else
{
echo " Moved successfully"
}


(Sorry for not replying earlier)
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Moving uploaded files

Post by mecha_godzilla »

You could use file_exists() to do this after your rename() operation - from the PHP manual:

Code: Select all

$filename = "downloads/".$file_name.".zip";

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
If the file exists at the new location then everything went ok. Some scripts will also use filesize() to check the file size as well (is the file size greater than 0 bytes?) to make sure that PHP didn't just create an empty file.

Using rename() in an if() test should work, but you might want to reverse the condition - test to see whether the result is TRUE (the rename operation completed successfully) rather than FALSE.

M_G
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Moving uploaded files

Post by litebearer »

You might consider adding a column to your db table
ie view_file - set it to 0 if not view then 1 to view
admin can then check all files set to 0, and if approved change to 1
only files with column set to 1 can be accessed by visitors
also all files can be in the same folder with no need to move them elsewhere
Post Reply