Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:39:17+00:00 2026-05-31T08:39:17+00:00

I’m having an image upload function I did create for few weeks ago –

  • 0

I’m having an image upload function I did create for few weeks ago – it has an variable called $newname, which contains the path and file.

I’m then using the imageupload() function in another function called EditFrontPage(), which is used to ‘update’ some content.

If I update the image, it runs the imageupload function, which is great, it resize and optimize the image, and it moves it to the folder I specified.

What I then want, is within’ my EditFrontPage() function, is to echo out the $newname variable from the imageUpload function.

is there a way to do this? in a smart way? 😀

Here is my code:

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

    }
}

?>

Thank you a lot guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:39:18+00:00Added an answer on May 31, 2026 at 8:39 am

    Have the imageupload() function return the $newname variable, and then set it like so on the EditFrontPage() function:

    $newName = imageUpload(10000, 100, 100, '', 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.