-1

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

此图像将转到该用户的网站背景。到目前为止一切都很好,但问题是:我怎么知道你爬了多长的图像?


如何工作:

我用用户“VITOR”爬上一个名为“AULA01-342.jpg”的文件,它会将图像重命名为“vitor.jpg”

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

以下是代码:

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

形式:

<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">
?>

行动:更新颜色

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');

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

提前致谢!

4

3 回答 3

5

尝试这个:

$ext = pathinfo($filename, PATHINFO_EXTENSION);
于 2013-10-29T03:48:40.220 回答
2

你应该使用pathinfo

这样您就可以获得文件的名称和扩展名。或者你可以用另一个函数来做:

$extension = substr($file_path,strpos($file_path,'.')+1);
于 2013-10-29T03:52:31.257 回答
0

尝试这个,

$ext=end(explode('.',$file_path))
于 2013-10-29T04:17:15.950 回答