我有几周前创建的图片上传功能 - 它有一个名为$ newname的变量,其中包含路径和文件。
然后我在另一个名为EditFrontPage()的函数中使用imageupload()函数,该函数用于“更新”某些内容。
如果我更新图像,它会运行imageupload功能,这很棒,它调整大小并优化图像,并将其移动到我指定的文件夹。
我想要的是在我的EditFrontPage()函数中,是从imageUpload函数中回显$ newname变量。
有没有办法做到这一点?以聪明的方式? :d
这是我的代码:
<?php
function EditFrontPage($db)
{
$stmt = $db->prepare("SELECT `id`, `heading`, `content`, `image` FROM `content` WHERE `page` = 'forside';");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(isset($_POST['EditFrontpageSubmit']))
{
imageUpload(10000, 100, 100, '', 5);
global $newname;
echo $newname;
}
?>
<form class="adminForm" enctype="multipart/form-data" action="" method="post">
<h2>Overskrift</h2>
<input type="text" value="<?=$row['heading']?>" />
<input type="file" name="image[]" />
<h2>Tekst</h2>
<textarea><?=br2nl($row['content'])?></textarea>
<input type="submit" name="EditFrontpageSubmit" value="Opdater nyhed" />
</form>
<?php
}
function imageUpload($maxsize = 2000, $quality = 95, $imgwidth = 400, $imgheight = '', $numOfImages = 5, $path = '/lucas/images/')
{
// Turn on error reporting
error_reporting(-1);
//Set the max upload size in kilobytes
define("MAX_SIZE", "$maxsize");
if(empty($imgheight) && empty($imgwidth))
{
echo "<h1>Fejl: Definer bredde eller højde</h1>";
die();
}
//Makes a function that check the extension
function getExtension($str){
$i = strrpos($str, ".");
if(!$i){return "";}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//Set errors to 0 from standard
$errors = 0;
//Define the size as a variable
$size = '';
//foreach image selected
foreach($_FILES['image']['error'] as $key => $error)
{
//If no errors, return true
if($error == UPLOAD_ERR_OK)
{
//Gets the filename
$filename = stripslashes($_FILES['image']['name'][$key]);
//Gets the extension
$extension = getExtension($filename);
//convert the extension to lowercase
$extension = strtolower($extension);
//if the file extension doesn't match, return error
if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo "<h1>Unknown Extension!</h1>";
$errors = 1;
}
//This check if the amount of images is over 5.
elseif(count($_FILES['image']['name']) > $numOfImages)
{
//If it's over 5 images, return error and exit
echo "Too many images";
exit();
}
else
{
//Get the filesize of the image (total amount if multiple images).
$size += filesize($_FILES['image']['tmp_name'][$key]);
//if the filesize is over the defined amount
if($size > MAX_SIZE*1024)
{
echo "<h1>Du har overskredet maksimum fil-upload størrelse!</h1>";
$errors = 1;
}
//This renames the image, to contain, the microtime, and a unique ID + extension
$image_name = microtime(true) . uniqid('',true) . '.' . $extension;
//This sets the path of the image.
$newname = $_SERVER['DOCUMENT_ROOT'] . $path . $image_name;
//It moves the file(s) to the path defined above!
$copied = move_uploaded_file($_FILES['image']['tmp_name'][$key], $newname);
//Check if the extension is png
//if($extension == "png")
//{
//converts the quality from 'jpeg/gif' quality to png compression method
//$pngquality = round($quality/100 * 9);
//Executes a shell command optimizing the png
//shell_exec("gm mogrify -quality $pngquality -thumbnail ". $imgwidth ."x". $imgheight ."\> $newname $newname");
//}
//else
//Executes a shell command optimizing the jpeg/gif
//shell_exec("gm mogrify -quality $quality -thumbnail ". $imgwidth ."x". $imgheight ."\> $newname $newname");
//If the image isn't copied, return an error
if(!$copied)
{
echo "<h1>Der skete en fejl!</h1>";
$errors = 1;
}
//Creates an array of the images
$array[] = $newname;
}
}
}
//If the submit is set, and errors = 0 return true
if(isset($_POST['Submit']) && $errors != 1)
{
echo "<h1>Fil blev uploaded som den skulle!</h1>";
//Makes a for loop, that echo's out all uploaded images
for($i = 0; $i < count($array); $i++)
{
echo "<img src='{$array[$i]}' /><p>".preg_replace("/.*\//i", '', $array[$i])."</p>";
}
}
}
?>
非常感谢你们!
答案 0 :(得分:2)
让imageupload()
函数返回$newname
变量,然后在EditFrontPage()
函数中将其设置为:
$newName = imageUpload(10000, 100, 100, '', 5);
答案 1 :(得分:1)
return $newname
函数末尾的imageUpload
并修改此部分:
if(isset($_POST['EditFrontpageSubmit']))
{
echo imageUpload(10000, 100, 100, '', 5);
}