today i was working on a php script that uploads the images and makes a thumbnails out of them..
well, i had the idea, i wrote the outline then i finished the script
i had a couple of syntax errors and i fixed them,,

but still there's some thing wrong with the code, no image was uploaded and no thumbnail created !!

here's the code

<?php
    function create_thumbnail ($source, $destination, $thumb_width) {
    $size = getimagesize($source);
    $width = $size[0];
    $height = $size[1];
    $x = 0;
    $y = 0;
    if ($width > $height ) {
        $x = ceil (($width - $height) / 2);
        $width = $height;}

    elseif ($height> $width) { ]
        $y = ceil (($height - $width) / 2);
        $height = $width; }

    $new_image = imagecreatetruecolor($thumb_width,$thumb_width)
                or die ('Cannot intialize new image stream, make sure to read the instructions');
    $extension = get_image_extension($source);
    if($extension=='jpg' || $extension=='jpeg')
        $image = imagecreatefromjpeg($source);
    if($extension=='gif') // for gif
        $image = imagecreatefromgif($source);
    if($extension == 'png') // for png
        $image = imagecreatefrompng($source);

        //now in relative to the upper lines the next few lines....

    imagecopyresampled($new_image,$image,0,0, $x,$y,$thumb_width,$thumb_width,$width,$height);
    if($extension=='jpg' || $extension=='jpeg')
        imagejpeg($new_image,$destination);
    if ($extension == 'gif')
        imagegif($new_image, $destination);
    if($extension=='png')
        imagepng($new_image,$destination);
    }

    function get_image_extension($name) { // gets the image extension
        $name = strtolower($name); // converts the name to lowercase
        $i = strrpos($name, ".");
        if (!$i) {return ""; }
            $l = strlen($name) - $i;
        $extension = strlen($name,$i+1,$l);
        return $extension;
    }

    function random_name($length) {
        $characters = "ABCDEFGHIJKLMNOPQRSUVWXYZ0123456789";
        $name = "";
        for ($i=0; $i < $length; $i++) {
        $name .= $characters [mt_rand(0, strlen($characters) - 1)];    }
        return "image-".$name;
    }



    $image_location = "images/featured/"; // images directory
    $thumbs_location = "images/tn/"; // thumnails directory
    $thumbs_width = 100; // width in pixels
    $maximum_size = 99999999999999; // maximum size in kb (i know, but 99999999999999 looks pretty )
    $result ="click browse to locate the image you want to upload"; // just a message bro.

    if (isset($_POST['submit'])){

        if($_files['image']['name'] == "") { // checks
            $result = "please choose an image !!";} // message


        else{
            $size=filesize($_FILES['image']['tmp_name']);
            $filename = stripslashes($_FILES['image']['name']);
            $extension = get_image_extension($filename    );
            if ($size > $maximum_size) {
                $result = "image is too large, please choose a smaller one.";}

            else  // now checks the extension..
                if (($extension != "jpg") &&     // not a jpg
                    ($extension != "jpeg") &&   // not a jpeg
                    ($extension != "png") &&   // not a png
                    ($extension != "gif")) {  // not a gif !! WTF is it ???
                    $result = "it's not an image file, please choose an image with proper extension";}

                else  

                    $image_random_name= random_name(15).".".$extension ;
                    $copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
                    if (!$copy) { $result = "we're sorry, there's a technical error, please try again, please"; }



                else // you finally reached this (else) and the image will be uploaded Hallelujah

                    { create_thumbnail ($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumbs_width) ;
                     $result = "image has been uploaded"; // it actually refers to me
                }
        }

    }
echo "$result";
?>

<html>
    <head>
        <title>Multi_uploader with thumbnails</title>
    </head>

<body>
    <form action="multi_uploader_with_thumbnails.php" method="POST" encypte="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="99999999999999"> <!-- this is kb but 99999999999999 looks cool -->
        <input type="file" name="image" multiple="multiple" />
        <input name="submit" id="submit" type="submit" value="Upload them">
    </form>


</body>
</html>

Dani AI

Generated

Short expert summary: the upload never completed because small typos and fragile logic prevented PHP from receiving/storing the file and the thumbnail routine never ran. The original post by contains several syntax and naming mistakes (form attribute misspelling, wrong superglobal case, a stray bracket, wrong string function for extracting extensions and inconsistent variable names). correctly pointed out many fixes; below are the root causes, safety checks, and a concise modern example for the upload/validation step that complements the thumbnail routine.

Key problems to address (concise):

  • The form must send multipart data (correct attribute name) and when using the multiple attribute the input name must be treated as an array on the server side.
  • PHP superglobals are case-sensitive: use $_FILES, and check $_FILES['…']['error'] and is_uploaded_file().
  • Avoid silent error suppression (@); use move_uploaded_file() rather than copy() for uploaded files.
  • Extract extension safely (use pathinfo or MIME sniffing) — strlen() cannot extract substrings. Trust the MIME type (finfo/getimagesize) rather than the client filename.
  • Brace all if/else blocks to avoid accidental fall-through; stray characters (like a closing bracket instead of a brace) break parsing.
  • Ensure target directories exist and are writable and that the GD (or ImageMagick) functions used for thumbnails are available.

Practical validation example (adaptable):

if (!empty($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
    if (!is_uploaded_file($_FILES['image']['tmp_name'])) { /* handle */ }
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $mime = $finfo->file($_FILES['image']['tmp_name']);
    $allowed = ['image/jpeg'=>'jpg','image/png'=>'png','image/gif'=>'gif'];
    if (!isset($allowed[$mime])) { /* invalid image */ }
    $ext = $allowed[$mime];
    $target = $uploadDir.'/img-'.bin2hex(random_bytes(8)).'.'.$ext;
    move_uploaded_file($_FILES['image']['tmp_name'], $target) or /* handle */;
    /* call thumbnail routine with $target */
}

Troubleshooting notes: enable full error reporting during debugging (error_reporting(E_ALL); ini_set('display_errors',1)); check webserver/PHP error logs, verify directory permissions, and confirm GD functions exist (function_exists('imagecreatetruecolor')). These checks complete 's suggestions and will make the upload + thumbnail workflow reliable.

its better to use from a php IDE to ensure typing.
u did some typing error.
and the input tag name (file type) must end with [] , to access to the each uploaded file , u must give the file index number to the $_FILES array , then its better to use a for loop.
try this:

<?php
    function create_thumbnail ($source, $destination, $thumb_width) {
    $size = getimagesize($source);
    $width = $size[0];
    $height = $size[1];
    $x = 0;
    $y = 0;
    if ($width > $height ) {
        $x = ceil (($width - $height) / 2);
        $width = $height;}
    elseif ($height> $width) {
        $y = ceil (($height - $width) / 2);
        $height = $width; }
    $new_image = imagecreatetruecolor($thumb_width,$thumb_width)
                or die ('Cannot intialize new image stream, make sure to read the instructions');
    $extension = get_image_extension($source);
    if($extension=='jpg' || $extension=='jpeg')
        $image = imagecreatefromjpeg($source);
    if($extension=='gif') // for gif
        $image = imagecreatefromgif($source);
    if($extension == 'png') // for png
        $image = imagecreatefrompng($source);
        //now in relative to the upper lines the next few lines....
    imagecopyresampled($new_image,$image,0,0, $x,$y,$thumb_width,$thumb_width,$width,$height);
    if($extension=='jpg' || $extension=='jpeg')
        imagejpeg($new_image,$destination);
    if ($extension == 'gif')
        imagegif($new_image, $destination);
    if($extension=='png')
        imagepng($new_image,$destination);
    }
    function get_image_extension($name) { // gets the image extension
        $name = strtolower($name); // converts the name to lowercase
        $i = strrpos($name, ".");
        if (!$i) {return ""; }
            $l = strlen($name) - $i;
        $extension = substr($name,$i+1,strlen($name));
        return $extension;
    }
    function random_name($length) {
        $characters = "ABCDEFGHIJKLMNOPQRSUVWXYZ0123456789";
        $name = "";
        for ($i=0; $i < $length; $i++) {
        $name .= $characters [mt_rand(0, strlen($characters) - 1)];    }
        return "image-".$name;
    }
    $images_location = "images/featured/"; // images directory
    $thumbs_location = "images/tn/"; // thumnails directory
    $thumbs_width = 100; // width in pixels
    $maximum_size = 99999999999999; // maximum size in kb (i know, but 99999999999999 looks pretty )
    $result ="click browse to locate the image you want to upload"; // just a message bro.
    if (isset($_POST['submit'])){
        if($_FILES['image']['name'][0] == "") { // checks
            $result = "please choose an image !!";} // message
        else{
            for($i=0;$i<count($_FILES['image']['name']);$i++){
                $size=filesize($_FILES['image']['tmp_name'][$i]);
                $filename = stripslashes($_FILES['image']['name'][$i]);
                $extension = get_image_extension($filename);
                if ($size > $maximum_size) {
                    $result = "image is too large, please choose a smaller one.";}
                else  // now checks the extension..
                    if (($extension != "jpg") &&     // not a jpg
                        ($extension != "jpeg") &&   // not a jpeg
                        ($extension != "png") &&   // not a png
                        ($extension != "gif")) {  // not a gif !! WTF is it ???
                        $result = "it's not an image file, please choose an image with proper extension";}
                    else  
                        $image_random_name= random_name(15).".".$extension ;
                        $copy = @move_uploaded_file($_FILES['image']['tmp_name'][$i], $images_location.$image_random_name);
                        if (!$copy) { $result = "we're sorry, there's a technical error, please try again, please"; }
                    else // you finally reached this (else) and the image will be uploaded Hallelujah
                        { create_thumbnail ($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumbs_width) ;
                         $result = "image has been uploaded"; // it actually refers to me
                    }
            }
        }
    }
echo "$result";
?>
<html>
    <head>
        <title>Multi_uploader with thumbnails</title>
    </head>
<body>
    <form id="frm1" action="multi_uploader_with_thumbnails.php" method="post" enctype="multipart/form-data" autocomplete="on">
        <input type="hidden" name="MAX_FILE_SIZE" value="99999999999999"> <!-- this is kb but 99999999999999 looks cool -->
        <input type="file" name="image[]" multiple />
        <input name="submit" id="submit" type="submit" value="Upload them">
    </form>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.