Page 1 of 1

Moving uploaded files

Posted: Sat Jun 29, 2013 5:06 am
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?

Re: Moving uploaded files

Posted: Sat Jun 29, 2013 2:41 pm
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

Re: Moving uploaded files

Posted: Thu Jul 11, 2013 2:56 pm
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)

Re: Moving uploaded files

Posted: Thu Jul 11, 2013 3:08 pm
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

Re: Moving uploaded files

Posted: Fri Jul 12, 2013 9:53 am
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