//LOAD IMAGE TABLE

        $filename = $_FILES["file"]["name"];

        $image_info = getimagesize($filename);
        $image_width = $image_info[0];
        $image_height = $image_info[1];

        echo "filename1 : ".$_FILES["file"]["name"];
        echo "filename : ".$filename;
        echo "image info : ".$image_info[0];
        echo "image info : ".$image_info[1];
        echo "image width : ".$image_width;
        echo "image height : ".$image_height;


filename1 : feature1.png
filename : feature1.png
image info :
image info :
image width :
image height :
Image is too small         

Hello,

U wonder why I cannot see width & height being printed out?

Thanks before.

Member Avatar for diafol

$_FILES["file"]["name"] as a file does not exist on the server, try the temp file instead:

$filename = $_FILES["file"]["tmp_name"];

The same problem:

Warning: getimagesize(C:\xampp\tmp\php1F45.tmp): failed to open stream: No such file or directory in C:\xampp\htdocs\SquprimeRevise\administrator\admin\update_image2.php on line 267
filename1 : feature1.png
filename : C:\xampp\tmp\php1F45.tmp
image info :
image info :
image width :
image height :
Image is too small

update_image2.php

//LOAD IMAGE TABLE

            $filename = $_FILES["file"]["tmp_name"];

            $image_info = getimagesize($filename);
            $image_width = $image_info[0];
            $image_height = $image_info[1];

            echo "filename1 : ".$_FILES["file"]["name"].'<br>';
            echo "filename : ".$filename.'<br>';
            echo "image info : ".$image_info[0].'<br>';
            echo "image info : ".$image_info[1].'<br>';
            echo "image width : ".$image_width.'<br>';
            echo "image height : ".$image_height.'<br>';

            $image = $_FILES["file"]["tmp_name"];

line 267: $image_info = getimagesize($filename);

You may want to move the file to a permanant location first using move_uploaded_file. The temp directory may have unusual permissions preventing you from reading the file, and these temp files are deleted very quickly as well. Without the rest of the context for your script it is hard to say if the file will still exist when you are running this.

Member Avatar for diafol

This works for me:

if($_FILES)
{
    $tmp = $_FILES['file']['tmp_name'];
    $data = getimagesize($tmp); 
    print_r($data);
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
</form>
</body>
</html>

This is the output:

Array ( [0] => 480 [1] => 640 [2] => 2 [3] => width="480" height="640" [bits] => 8 [channels] => 3 [mime] => image/jpeg )

As suggested by Isaac_4 try moving your upload file to a new directory.

$upload_path = '/uploads/';
$file = $_FILES['file']['name'];

if(move_uploaded_file($_FILES['file']['tmp_name'],$upload_path.$file)){
    echo $file . ' has been uploaded';
}else{
    echo $file . ' has not been uploaded';
}

The thing is I have to figure out the file size before I copy it to a new location.

I have to limit the file size so that I wouldn't be too small or too large.

So I try this:

Warning: getimagesize(C:\xampp\tmp\php1263.tmp): failed to open stream: No such file or directory in C:\xampp\htdocs\SquprimeRevise\administrator\admin\update_image2.php on line 289

line 289:
$data = getimagesize($tmp);

What version of xampp are you using?

Xampp version 3.2.1.

Member Avatar for diafol

Xampp seems to be version 1.8.3 at the moment - how can it be 3.2.1?

Whoops, I mean xampp: 1.8.2

The thing is I have to figure out the file size before I copy it to a new location.

I have to limit the file size so that I wouldn't be too small or too large.

have you tried?

//Change the file size to whatever you need to be.
if( $_FILES['file'][size] > 98174 ){
    echo $_FILES['file'][error];
    exit;
}

Well, that's the limit the file size (in MB) while I am trying to limit the file width and height (in pixels)

Here is a more complete picture of the codes:

update_image2.php

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

            print_r($_POST['location'], 1);

            if (empty($_GET['image_id']))
                {

                // Picture Upload

                $allowedExts = array("gif", "jpeg", "jpg", "png");
                //$temp = explode(".", $_FILES["file"]["name"]);

                $image_info = getimagesize($_FILES["file"]["tmp_name"]);
                $image_width = $image_info[0];
                $image_height = $image_info[1];

                $extension = pathinfo($_FILES["file"]["name"],PATHINFO_EXTENSION);  
                //$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"] < 41943040)  //40MB
                && in_array($extension, $allowedExts)) {

                if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                } else {
                echo "Successfully upload pictures";
                //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("upload/" . $_FILES["file"]["name"]))
                    {

                    $filename = $_FILES["file"]["name"];
                    $i = (rand());
                    $ii = (rand());
                    $iii = (rand());
                    $newfilename = $ii.$iii.$i.$filename;

                    //echo $_FILES["file"]["name"] . " new file name is $newfilename. ";
                    } else {

                    $filename = $_FILES["file"]["name"];
                    $i = (rand());
                    $newfilename = $i.$filename;


                    move_uploaded_file($_FILES["file"]["tmp_name"],
                    "/images/" . $newfilename);
                    //echo "Stored in: " . "upload/" . $newfilename;
                    }


                } 

                /*$filename = $_FILES["file"]["name"];

                $image_info = getimagesize($_FILES["file"]["name"]);
                $image_width = $image_info[0];
                $image_height = $image_info[1];

                echo "image info : ".$image_info[0];
                echo "image info : ".$image_info;
                echo "image width : ".$image_width;
                echo "image height : ".$image_height;*/

                /*if($_FILES)
                {
                $tmp = $_FILES['file']['tmp_name'];
                $data = getimagesize($tmp);
                print_r($data);
                }*/


                $username = substr($stu_fname, 0, 1).$stu_lname;
                $username = strtolower($username);

                // default password for new student
                $encrypteddefaultpassword = MD5('squprime');

                $image_id = $_GET['image_id'];

                // counter mysql injection
                $image_id = mysql_real_escape_string($image_id);
                $image = mysql_real_escape_string($image);
                $location = mysql_real_escape_string($location);

                $result2 = mysql_query("SELECT * FROM image_upload");

                $data2 = mysql_fetch_array($result2);

                if($image_width > $data2['maxwidth'] or $image_height > $data2['maxheight'])
                    {
                    echo "Image is too big";
                    exit();
                    }       
                elseif($image_width < $data2['minheight'] or $image_height < $data2['minheight'])
                    {
                    echo "Image is too small";
                    exit();
                    }
                else    
                    {
                    $sqlstr = "INSERT INTO image_upload(image, newfilename, location) VALUES('".$image."', '".$newfilename."', '".$location."')";
                    }

                }
            }
        else
            {   

            // Picture Upload 

            $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"] < 41943040)  //40MB
            && in_array($extension, $allowedExts)) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                } else {
                //echo "Successfully upload pictures";
                //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("upload/" . $_FILES["file"]["name"]))
                {

                $filename = $_FILES["file"]["name"];
                $i = (rand());
                $ii = (rand());
                $iii = (rand());
                $newfilename = $ii.$iii.$i.$filename;

                //echo $_FILES["file"]["name"] . " new file name is $newfilename. ";
                } else {

                $filename = $_FILES["file"]["name"];
                $i = (rand());
                $newfilename = $i.$filename;

                move_uploaded_file($_FILES["file"]["tmp_name"],
                "images/" . $newfilename);
                //echo "Stored in: " . "upload/" . $newfilename;
                }
                }
            } /*else {
                echo "No picture uploaded";
            } */

            //$filename = $_FILES["file"]["name"];

            //$username = substr($stu_fname, 0, 1).$stu_lname;
            //$username = strtolower($username);

            $image_id = $_REQUEST['image_id'];

            // Query to get student picture filename
            $result = mysql_query("SELECT * FROM image_upload WHERE image_id =".$image_id) or die(mysql_error());
            $data1 = mysql_fetch_assoc($result);
            $pic_filename = $data1['newfilename'];

            if($result){
                unlink('images/'.$pic_filename);
            }


            //LOAD IMAGE TABLE

            $filename = $_FILES["file"]["tmp_name"];

            $image_info = getimagesize($filename);
            $image_width = $image_info[0];
            $image_height = $image_info[1];

            echo "filename1 : ".$_FILES["file"]["tmp_name"].'<br>';
            echo "filename : ".$filename.'<br>';
            echo "image info : ".$image_info[0].'<br>';
            echo "image info : ".$image_info[1].'<br>';
            echo "image width : ".$image_width.'<br>';
            echo "image height : ".$image_height.'<br>'; 

            if($_FILES)
            {
            $tmp = $_FILES['file']['tmp_name'];
            $data = getimagesize($tmp);
            print_r($data);
            }


            $image = $_FILES["file"]["tmp_name"];

            //echo $_POST['location'];die();
            $location = $_POST['location'];
            //$location = $_GET['location'];

            $image_id = $_GET['image_id'];

            //counter mysql injection           

            $image = mysql_real_escape_string($image);

            $result2 = mysql_query("SELECT * FROM image_upload");

                $data2 = mysql_fetch_array($result2);

                /*
                if( $_FILES['file'][size] > $data2['maxwidth'] ){
                        echo $_FILES['file'][error];
                        exit;
                }
                else    
                    {
                    $sqlstr = "UPDATE image_upload SET image_id='".$image_id."', image='".$image."', newfilename='".$newfilename."', location='".$location."' WHERE image_id='".$image_id."'";
                    }
                */


                if($image_width > $data2['maxwidth'] or $image_height > $data2['maxheight'])
                    {
                    echo "Image is too big";
                    exit();
                    }       
                elseif($image_width < $data2['minheight'] or $image_height < $data2['minheight'])
                    {
                    echo "Image is too small";
                    exit();
                    }
                else    
                    {
                    $sqlstr = "UPDATE image_upload SET image_id='".$image_id."', image='".$image."', newfilename='".$newfilename."', location='".$location."' WHERE image_id='".$image_id."'";
                    }

            echo $sqlstr;   
            } 

        $result = mysql_query($sqlstr) or die(mysql_error());

        //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
        //if (empty($_REQUEST['id']))   kirimEmail($idKategori, $judul, $news);
        $confirmation = ($result) ? "Data has been saved." : "Fail to save data.";



        }


        ?>          



  <!-- end extra --> 



<br><br><br><br>    

                <?php
                //LOAD IMAGE TABLE

                $result = mysql_query("SELECT * FROM image_upload WHERE image_id='".$_GET['image_id']."'") or die(mysql_error());

                $data = mysql_fetch_array($result);

                $image_id = $_GET['image_id'];
                $newfilename = $data['newfilename'];
                $location = $data['location'];
                $minwidth = $data['minwidth'];
                $maxwidth = $data['maxwidth'];
                $minheight = $data['minheight'];
                $maxheight = $data['maxheight'];

                ?>

                <form method="post" action="<?php echo $_SERVER['PHP_SELF'] .'?image_id='. $image_id;?>" enctype="multipart/form-data">

                        <?php
                        echo '<div id="location"><b>Location: <input type="text" size="50px" name="location" value="'.$location.'" disabled></b></div><br><br><br>';

                        echo '<input type="text" size="30px" name="location" value="'.$location.'" hidden>';

                        echo '<div id="size"><b>min width: <input type="text" size="10px" name="size" value="'.$minwidth.'px" disabled></b></div><br><br><br>';

                        echo '<div id="size"><b>max width: <input type="text" size="10px" name="size" value="'.$maxwidth.'px" disabled></b></div><br><br><br>';

                        echo '<div id="size"><b>min height: <input type="text" size="10px" name="size" value="'.$minheight.'px" disabled></b></div><br><br><br>';

                        echo '<div id="size"><b>max height: <input type="text" size="10px" name="size" value="'.$maxheight.'px" disabled></b></div><br><br><br>';
                        echo $data['image'];

                        echo '<div id="updateimage"><img src="images/'.$data['newfilename'].'" height="250px"></updateimage>';

                        echo '<br><br><br>.<input type="file" value="upload" name="file"/><br><br>';

                        ?>

            <td><br><input type="submit" name="ok" value="Save" class="abutton"/></td>

            </form>

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

<p>&nbsp;</p>

</body>
</div>

            <!-- End Insert -->  

Warning: unlink(images/27590feature1.png): No such file or directory in C:\xampp\htdocs\SquprimeRevise\administrator\admin\update_image2.php on line 267

Warning: getimagesize(C:\xampp\tmp\php6924.tmp): failed to open stream: No such file or directory in C:\xampp\htdocs\SquprimeRevise\administrator\admin\update_image2.php on line 275
filename1 : C:\xampp\tmp\php6924.tmp
filename : C:\xampp\tmp\php6924.tmp
image info :
image info :
image width :
image height :

Warning: getimagesize(C:\xampp\tmp\php6924.tmp): failed to open stream: No such file or directory in C:\xampp\htdocs\SquprimeRevise\administrator\admin\update_image2.php on line 289
Image is too small

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.