如何用PHP获取文件和扩展名?

时间:2013-10-29 03:43:15

标签: php mysql

我上传的图片允许jpg,png和gif中的图片。 它会自动将图像重命名为记录的用户名。

此图片将转到该用户所在网站的背景。 到目前为止一切顺利,但问题是: 我怎么知道你爬到图像的长度?


如何工作:

我使用用户“VITOR”登录名为“AULA01-342.jpg”的文件,它会将图像重命名为“vitor.jpg”

但我需要在网站的前端获取该文件。 好像在前端我知道文件是.jpg,.gif还是.png?

以下是代码:

如何检测文件夹中的文件扩展名?

FORM:

<form enctype="multipart/form-data" action="<?php echo "cores.php?acao=updatecolors&amp;id=$id" ;?>" method="post">

  <input type="file" class="file-input" name="userImgBody" id="userImgBody"/>
 <input type="submit" class="btn btn-success span12" value="atualizar" name="atualizar">
?>

行动:UPDATECOLORS

if($startaction == 1){
    if($acao == "updatecolors"){
        $id=$_GET["id"];
        $query=mysql_query("UPDATE vms_cores SET 
        c.userImgBody='$userImgBody', 

        WHERE c.id='u.id'");

        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["userImgBody"]["name"]);

        $extension = end($temp);
        $newfilename = $usuario .".".$extension;
        if ((($_FILES["userImgBody"]["type"] == "image/gif")
        || ($_FILES["userImgBody"]["type"] == "image/jpeg")
        || ($_FILES["userImgBody"]["type"] == "image/jpg")
        || ($_FILES["userImgBody"]["type"] == "image/pjpeg")
        || ($_FILES["userImgBody"]["type"] == "image/x-png")
        || ($_FILES["userImgBody"]["type"] == "image/png"))
        && ($_FILES["userImgBody"]["size"] < 1097152)
        && in_array($extension, $allowedExts))
          {
          if ($_FILES["userImgBody"]["error"] > 0)
            {
            echo "Return Code: " . $_FILES["userImgBody"]["error"] . "<br>";
            }
          else
            {
            echo "Upload: " . $_FILES["userImgBody"]["name"] . "<br>";
            echo "Type: " . $_FILES["userImgBody"]["type"] . "<br>";
            echo "Size: " . ($_FILES["userImgBody"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["userImgBody"]["tmp_name"] . "<br>";

            if (file_exists("../_arquivos/upload/bgs/" . $_FILES["userImgBody"]["name"]))
              {
              echo $_FILES["userImgBody"]["name"] . " already exists. ";
              }
            else
              {
              move_uploaded_file($_FILES["userImgBody"]["tmp_name"],
              "../_arquivos/upload/bgs/" . $newfilename);
              echo "Stored in: " . "../_arquivos/upload/bgs/" .$newfilename;
              }
            }
          }
        else
          {
          echo "Invalid file";
          } 


    }
}

前端

// Get Subdomain
$urlExplode = explode('.', $_SERVER['HTTP_HOST']);
if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') {
    $subdomain = $urlExplode[0];    
//  echo $subdomain;
}


// variable "usuario" == variable "subdomain"
$usuario = $subdomain;


   $sql = "SELECT * FROM vms_textos t INNER JOIN vms_users u ON u.id = t.id left outer JOIN vms_cores c ON u.id = c.id where u.usuario='$usuario'";

    $result = mysql_query($sql); 
    if($result === FALSE) { 
    die(mysql_error());
     // TODO: better error handling 
    }
else {
$row = mysql_fetch_array($result);

// Tabela Textos
$userKeywords = $row['userKeywords'];
$userDesc = $row['userDesc'];
$userTitleSite = $row['userTitleSite'];
$userTelefoneSite = $row['userTelefoneSite'];
$userTextSobre = $row['userTextSobre'];
$userTextContatos = $row['userTextContatos'];
$userTextMaisInfos = $row['userTextMaisInfos'];

// Tabela Cores
$userImgBody = $row['userImgBody'];
}







<body>

<style>
body{
 background-image: url('http://arquivos.minisite.net.br/upload/bgs/<?php $usuario;?>');
}
</style>

最终结果是:

background-image: url('http://arquivos.minisite.net.br/upload/bgs/vitor');

应该是:

background-image: url('http://arquivos.minisite.net.br/upload/bgs/vitor.png');

background-image: url('http://arquivos.minisite.net.br/upload/bgs/vitor.jpg');

background-image: url('http://arquivos.minisite.net.br/upload/bgs/vitor.gif');

根据我上传的图片类型......

提前致谢!

3 个答案:

答案 0 :(得分:5)

试试这个:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

答案 1 :(得分:2)

您应该使用pathinfo

这样您就可以获得文件的名称和扩展名。 或者您可以使用其他功能:

$extension = substr($file_path,strpos($file_path,'.')+1);

答案 2 :(得分:0)

试试这个,

$ext=end(explode('.',$file_path))