Hello, I was looking for a solution to my doubt in the manual and I found it with the function rename(). The only thing that concerns me is that the manual says "Attempts to rename oldname to newname", specifically using the word "attempts" as if they were taking in account a large possibility for failure so I was wondering how reliable rename() really is... Given that the directory where the files are in have the correct permissions, is it possible that the "attempts" of the function to rename a file might not be successful???
In my particular case I have to give a specific name to a file I am uploading from a form so I can do either this (less code but using this supposingly unreliable "rename" function):
$path_images = 'mypath';
$name_file = 'something';
if (!is_dir($DOCUMENT_ROOT.'/'.$path_images.'/'.$name_file)) {
mkdir ($DOCUMENT_ROOT.'/'.$path_images.'/'.$name_file, 0777);
}
$uploaddir = $DOCUMENT_ROOT.'/'.$path_images.'/'.$name_file;
if (move_uploaded_file($_FILES['file_logo']['tmp_name'], $uploaddir.'/'.$_FILES['file_logo']['name'])) {
if (eregi("jpg", $_FILES['file_logo']['type']) || eregi("jpeg", $_FILES['file_logo']['type'])) {
@unlink($uploaddir.'/'.$name_file.'_logo.jpg');
rename($uploaddir.'/'.$_FILES['file_logo']['name'],$uploaddir.'/'.$name_file.'_logo.jpg');
} else if (eregi("gif", $_FILES['file_logo']['type'])) {
@unlink($uploaddir.'/'.$name_file.'_logo.gif');
rename($uploaddir.'/'.$_FILES['file_logo']['name'],$uploaddir.'/'.$name_file.'_logo.gif');
} else if (eregi("png", $_FILES['file_logo']['type'])) {
@unlink($uploaddir.'/'.$name_file.'_logo.png');
rename($uploaddir.'/'.$_FILES['file_logo']['name'],$uploaddir.'/'.$name_file.'_logo.png');
}
echo '<br><div align="center"><b>Your image was successfully uploaded!</b></div>';
}
or I can do this (which is more code intensive but doesn't use file-related functions such as rename() or unlink() at all:
$path_images = 'mypath';
$name_file = 'something';
if (!is_dir($DOCUMENT_ROOT.'/'.$path_images.'/'.$name_file)) {
mkdir ($DOCUMENT_ROOT.'/'.$path_images.'/'.$name_file, 0777);
}
$uploaddir = $DOCUMENT_ROOT.'/'.$path_images.'/'.$name_file;
if (eregi("jpeg", $_FILES['file_logo']['type'])) {
$suffix = eregi_replace(".jpeg","_logo.jpg",$_FILES['file_logo']['name']);
$suffix = eregi_replace(".jpg","_logo.jpg",$_FILES['file_logo']['name']);
} else if (eregi("gif", $_FILES['file_logo']['type'])) {
$suffix = eregi_replace(".gif","_logo.gif",$_FILES['file_logo']['name']);
} else if (eregi("png", $_FILES['file_logo']['type'])) {
$suffix = eregi_replace(".png","_logo.png",$_FILES['file_logo']['name']);
}
if (move_uploaded_file($_FILES['file_logo']['tmp_name'], $uploaddir.'/'.$name_file.$suffix)) {
echo '<br><div align="center"><b>Your image was successfully uploaded!</b></div>';
}
Which one is the better solution in your opinion? Or do you know an even better solution?
By the way, I was just wondering can the argument 'file_logo' inside $_FILES['file_logo']['tmp_name'] be a changable variable? I am asking because I am planning to put the upload code in a function to use the same function with different forms where the file name field is named differently.
Thanks.