Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

getimagesize

Featured Replies

I'm submitting a form where a user can add an image file...


<form method="post" action=".\sendContact.php" enctype="multipart/form-data">

<input type="TEXT" name="email">

<input type="file" name="file">

<input type="submit" value="Submit">


to a php ...


echo 'file test ';

echo $_POST['email'];

echo ' ';

echo $_FILES['file']['tmp_name'];

echo ' ';

if ($_FILES['file']['error'] == 0) {

echo 'file ok';

}

echo ' ';


and get


file test 3@3.com C:\Windows\Temp\php99DD.tmp file ok


but if I include


if(getimagesize($_FILES['file']['tmp_name'])){

echo 'image ok';

}


I get 500- Internal server error


php.ini

file_uploads = On

upload_max_filesize = 2M

max_file_uploads = 20


any ideas?


this is my 3rd post but so far no response, can someone confirm that I'm not doing something wrong and that the site is still active?

Edited by fnets

Hi Matey,

 

I think it's because you are trying to get the size of an error and getimagesize() only works on images. The following will check it exists...

if(file_exists($_FILES['file']['tmp_name'])) {
echo "I Exist";
}

and so if it exists we can work out how big it is ...

if(file_exists($_FILES['file']['tmp_name'])) {
    if(filesize($_FILES['file']['tmp_name'])>2097152) { // Uses Bytes
        echo "<b>Error!</b> - The file is bigger than 2MB.";
    }
}

Edited by BrowserBugs

  • Author

thank you.

 

if(file_exists($_FILES['file']['tmp_name'])) {
echo "I Exist";
}

 

didn't echo so it doesn't exist? does that mean file_exists($_FILES['file']['tmp_name']) is incorrect. if so what should I do??

Try ...

if(!empty($_FILES['file']['name'])) {
   echo "There is a file";
   list($source_width, $source_height, $source_type) = getimagesize($_FILES['file']['tmp_name']);
   echo $source_width . " x " . $source_height . " type " . $source_type;
} else {
   echo "No File";
}

This should let you know about the image. Thing is you'd be better to look into file info if it's enabled, much safer.

 

Edit; e.g.

$fileinfo = finfo_open(FILEINFO_MIME_TYPE);
$temp_type = finfo_file($fileinfo, $_FILES['file']['tmp_name']);
finfo_close($fileinfo);
if($temp_type=="image/png") {
   $ext = "png";
} elseif($temp_type=="image/jpeg") {
   $ext = "jpg";
} else {
   echo "<b>Error!</b> - The file is not a png or jpg.";
}

Edited by BrowserBugs

  • Author
	if(!empty($_FILES['file']['name'])) {
   		echo 'There is a file';
	}
	
	if(!empty($_FILES['file']['tmp_name'])) {
   		echo 'There is a file tmp_name';
	}

neither of the above echo's get called. Does that mean perhaps the file is deleted by the server before my code gets to it?

 

BTW. thanks for your help, very much appreciated

Edited by fnets

  • Author
echo '<pre>'; print_r($_FILES);echo '</pre>';

prints

 

Array
(
[file] => Array
(
[name] => sn.jpg
[type] => image/jpeg
[tmp_name] => C:\Windows\Temp\php9BED.tmp
[error] => 0
=> 34186
)
)
;

Edited by fnets

	if(!empty($_FILES['file']['name'])) {
   		echo 'There is a file';
	}
	
	if(!empty($_FILES['file']['tmp_name'])) {
   		echo 'There is a file tmp_name';
	}

neither of the above echo's get called. Does that mean perhaps the file is deleted by the server before my code gets to it?

 

BTW. thanks for your help, very much appreciated

 

 

The temp file will be deleted after the full php execution, if(!empty()) { should fire if there was a file uploaded. Bit baffled mate as your print_r($_FILES); says there was a file.

 

What happen with the following?

<?php if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_FILES['image_up']['name']) && intval($_POST['uploadFile'])==1) {
   echo "I Got a file";
} ?>

<form method="post" enctype="multipart/form-data">
<input name="image_up" type="file">
<button type="submit" name="uploadFile" value="1">Upload</button>
</form>

This in a blank page should work fine. On a side note i'm always cautious of using reserved words for field names.

  • Author

 

The temp file will be deleted after the full php execution, if(!empty()) { should fire if there was a file uploaded. Bit baffled mate as your print_r($_FILES); says there was a file.

 

What happen with the following?

<?php if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_FILES['image_up']['name']) && intval($_POST['uploadFile'])==1) {
   echo "I Got a file";
} ?>

<form method="post" enctype="multipart/form-data">
<input name="image_up" type="file">
<button type="submit" name="uploadFile" value="1">Upload</button>
</form>

This in a blank page should work fine. On a side note i'm always cautious of using reserved words for field names.

 

ran on new page and got

 

405 - HTTP verb used to access this page [.html] is not allowed.

 

any ideas?

Edited by fnets

Just checking the page you made was a .php and not a .html? Is this an IIS server? Digging up that error code it says you require an action so ...

<?php if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_FILES['image_up']['name']) && intval($_POST['uploadFile'])==1) {
   echo "I Got a file";
} ?>

<form method="post" action="/thepage.php" enctype="multipart/form-data">
<input name="image_up" type="file">
<button type="submit" name="uploadFile" value="1">Upload</button>
</form>
  • Author

my mistake. now a .php and it worked "I got a file". Yes IIS server.

 

learning so much here, thank you.

 

if I separate into .html and .php they also work. so now I have something to build on. cheers.

 

and this works ...

<?php 

   echo '<pre>'; print_r($_FILES);echo '</pre>';

   if(file_exists($_FILES['image_up']['tmp_name'])) {
        echo "file exists";
   }
   
?>

Edited by fnets

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.