我有一個圖像上傳,允許在JPG,PNG和GIF圖像。 它會自動將圖像重命名爲記錄的用戶名。如何獲取文件和擴展名與PHP?
此圖像將轉到用戶所在網站的背景。 到目前爲止這麼好,但問題是: 我怎麼知道你爬過圖像的長度?
如何工作:
我爬上一個名爲「AULA01-342.jpg」對用戶「VITOR」的文件時,它會重命名圖像「vitor.jpg」
但我需要在該網站的前端獲得該文件。 就好像在前端我知道文件是.jpg,.gif還是.png?
這裏是代碼:
如何檢測的文件擴展名是存在的文件夾中?
FORM:
<form enctype="multipart/form-data" action="<?php echo "cores.php?acao=updatecolors&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">
?>
ACTION: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');
根據圖像的類型,我做了上傳...
在此先感謝!
*旁註:*停止使用廢棄的'mysql_ *'功能。改用MySQLi或PDO。 – Raptor
這裏有幾個問題。你想獲得文件擴展名,或圖像的長度?我看到你已經在你的代碼中獲得了文件擴展名。 – Raptor
其實我需要或得到整個文件路徑的例子: http://www.site.com/image.jpg 然後只有服務器上的文件擴展名與給定的名稱。 正在。 JPG ,. gif或。 png –