[英]Custom Name And Overwriting For PHP Image Upload
在我的網站上,用戶的用戶名可以在PHP中顯示為
<?=$c->username?>
我需要將圖像另存為
<?=$c->username?>.png
我還需要在正上方打開圖像。 當嘗試上傳具有相同名稱的另一張圖片時,它只會出錯。
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/example/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/example/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/example/". $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
move_uploaded_file
已經滿足您的要求。
該錯誤很可能是由於您正在寫入絕對路徑 (即,文件系統根目錄中的名為“ example”的目錄)而不是當前Web服務器根目錄中的子目錄“ example”的事實所致。
另外,您可以准備允許類型的數組,並使用in_array
檢查:
$allowed_types = array('image/gif', 'image/jpeg', ...);
if ((in_array($_FILES['file']['type'], $allowed_types)
&& ...) {
...
值得做的另一件事是定義一些變量,這樣您就可以確保始終使用相同的值,因為您只需分配一次即可。 如果您在變量名中輸入錯誤,則會收到通知,而如果您輸入路徑錯誤或在修改后忘記更新多個實例之一,則可能會出現無提示和/或難以診斷的錯誤。
$destination_dir = './example/'; // Also, note the "." at the beginning
// SECURITY: do not trust the name supplied by the user! At least use basename().
$basename = basename($_FILES["file"]["name"]);
if (!is_dir($destination_dir)) {
trigger_error("Destination dir {$destination_dir} is not writeable", E_USER_ERROR);
die();
}
if (file_exists($destination_file = $destination_dir . $basename)) {
echo "{$basename} already exists";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
$destination_file);
echo "Stored in: {$destination_file}";
}
在您的情況下,您希望以<?= $c->username ?>.png
的方式將圖片保存在瀏覽器中 。
為此,您需要做兩件事:文件路徑和圖像格式,必須為PNG。 在告訴瀏覽器您要發送PNG后發送JPEG可能無法正常工作(某些瀏覽器會自動識別格式,而其他瀏覽器則不會)。 您可能還需要將圖像調整為特定大小。
因此,您應該執行以下操作:
// We ignore the $basename supplied by the user, using its username.
// **I assume that you know how to retrieve $c at this point.**
$basename = $c->username . '.png';
$destination_file = $destination_dir . $basename;
if ('image/png' == $_FILES['file']['type']) {
// No need to do anything, except perhaps resizing.
move_uploaded_file($_FILES["file"]["tmp_name"], $destination_file);
} else {
// Problem. The file is not PNG.
$temp_file = $destination_file . '.tmp';
move_uploaded_file($_FILES["file"]["tmp_name"], $temp_file);
$image_data = file_get_contents($temp_file);
unlink($temp_file);
// Now we have image data as a string.
$gd = ImageCreateFromString($image_data);
// Save it as PNG.
ImagePNG($gd, $destination_file);
ImageDestroy($gd);
}
// Optional resize.
if (false) {
list($w, $h) = getImageSize($destination_file);
// Fit it into a 64x64 square.
$W = 64;
$H = 64;
if (($w != $W) || ($h != $H)) {
$bad = ImageCreateFromPNG($destination_file);
$good = ImageCreateTrueColor($W, $H);
$bgnd = ImageColorAllocate($good, 255, 255, 255); // White background
ImageFilledRectangle($good, 0, 0, $W, $H, $bgnd);
// if the image is too wide, it will become $W-wide and <= $H tall.
// So it will have an empty strip top and bottom.
if ($w > $W*$h/$H) {
$ww = $W;
$hh = $ww * $h / $w;
$xx = 0;
$yy = floor(($H - $hh)/2);
} else {
// It will be $H tall, and l.o.e. than $W wide
$hh = $H;
$ww = $hh * $w / $h;
$xx = floor(($W - $ww)/2);
$yy = 0;
}
ImageCopyResampled($good, $bad,
$xx, $yy, 0, 0,
$ww, $hh, $w, $h
);
// Save modified image.
ImageDestroy($bad);
ImagePNG($good, $destination_file);
ImageDestroy($good);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.