He pensado en crear UploadFile1, UploadFile2 etc..y asi las guarda donde quiero, pero lo veo un poco chapucero y demasiado codigo repetido, no?
Código PHP:
if ($_FILES['wallpaper1']['name']!=NULL) {
$Wallpaper_1=UploadFile("wallpaper1");
}
if ($_FILES['wallpaper2']['name']!=NULL) {
$Wallpaper_2=UploadFile("wallpaper2");
}
if ($_FILES['wallpaper3']['name']!=NULL) {
$Wallpaper_3=UploadFile("wallpaper3");
}
if ($_FILES['wallpaper4']['name']!=NULL) {
$Wallpaper_4=UploadFile("wallpaper4");
}
if ($_FILES['wallpaper5']['name']!=NULL) {
$Wallpaper_5=UploadFile("wallpaper5");
Código PHP:
function UploadFile($uname)
{
global $_FILES;
global $path;
$type = substr($_FILES[$uname]['type'],0,6);
if(!isset($_FILES)){
$error = "You must select a valid file.";
} else if($_FILES[$uname]['error']!=0){
$error = "An unexpected error occurred.";
} else if($type != 'image/')
{
$error = "The file you have selected is not a valid image file.";
}
if (! $error)
{
$_IMAGE = array();
$_IMAGE['name'] = $_FILES[$uname]['name'];
$_IMAGE['size'] = $_FILES[$uname]['size'];
$_IMAGE['date'] = date("D d F Y");
$_IMAGE['type'] = substr($_FILES[$uname]['type'],6);
$_IMAGE['code'] = microtime();
$_IMAGE['name'] = htmlentities($_IMAGE['name']);
$_IMAGE['size'] = htmlentities($_IMAGE['size']);
$_IMAGE['date'] = htmlentities($_IMAGE['date']);
$_IMAGE['type'] = htmlentities($_IMAGE['type']);
$_IMAGE['code'] = htmlentities($_IMAGE['code']);
$type = htmlentities($type);
$type = strtolower($_IMAGE['type']);
if (strtolower($type) == "pjpeg") { $type = "jpeg"; }
if (strtolower($type) == "x-png") { $type = "png"; }
if(checktype($type) == false)
{
$error = "That file type is not allowed.";
} else if($_IMAGE['size']>5024000){
$error = "That image is too large.";
}
if (! $error)
{
$target_path = $path."imagenes/wallpapers/";
$_IMAGE['name'] = $_FILES[$uname]['name'];
$rnd = rand(0,1000000);
$name = substr($_IMAGE['name'],0,strpos($_IMAGE['name'], ".")).'-'.$rnd;
$name .= ".".$type;
$target_path = $target_path . basename($name);
if(move_uploaded_file($_FILES[$uname]['tmp_name'], $target_path))
{
return $name;
} else
{
echo $_FILES[$uname]['tmp_name']."<br />".$target_path."<br />t";
return false;
}
}
} else
{
echo $error;
return false;
}
}
Código PHP:
$target_path = $path."imagenes/wallpapers/";