`getimagesize` giving error `Filename cannot be empty` PHP 8 - php

getimagesize giving error Filename cannot be empty PHP 8
if(isset($_FILES["nuevaFoto"]["tmp_name"])) {
list($ancho, $alto) = getimagesize($_FILES["nuevaFoto"]["tmp_name"]);
}
I do not get this error under PHP 7.3. But in PHP 8.0 the error is thrown during the file upload.

This is due to the error re-classification made for PHP8.
I advise you to check for an empty string too in your condition
<?php
if(isset($_FILES["nuevaFoto"]["tmp_name"]) && $_FILES["nuevaFoto"]["tmp_name"] !== ''){
list($ancho, $alto) = getimagesize($_FILES["nuevaFoto"]["tmp_name"]);
}
Alternatively you could catch (ValueError $e) but this error is pretty generic so this is not advised.
For the record you yan check the output for various PHP versions with this link: https://3v4l.org/HgXpT
The change has been made in this Pull Request https://github.com/php/php-src/pull/5902

Related

Why is "#" symbol still causing errors?

I have an old site using Ubuntu and PHP 5.3. There is a cron script that hits an API to push/pull data. Recently I have been hit with an error when these scripts attempt to run: "Unidentified index: UserID" in the "appsettings.php" file.
$conn=db_connect();
if(!#$_SESSION["UserID"])
{
$allowGuest=guestHasPermissions();
$scriptname=getFileNameFromURL();
if($allowGuest && $scriptname!="login.php" && $scriptname!="remind.php" && $scriptname!="register.php" && $scriptname!="registersuggest.php")
{
$_SESSION["UserID"]="Guest";
$_SESSION["GroupID"]="<Guest>";
$_SESSION["AccessLevel"]=ACCESS_LEVEL_GUEST;
$auditObj = GetAuditObject();
if($auditObj)
$auditObj->LogLogin();
if($globalEvents->exists("AfterSuccessfulLogin"))
{
$dummy=array();
$globalEvents->AfterSuccessfulLogin("","",$dummy);
}
}
}
I understand that using the "#" symbol isn't best practice, but I've been given explicit instruction to "just make it work". This has worked in the past, but since pushing up some styling changes I started getting this error.
To check to see if the USERID exists, try changing
if(!#$_SESSION["UserID"])
to
if(!isset($_SESSION["UserID"])){...
...
} else {
...// handle the error
}

PHP 7 error 500 internal

I'm completely lost with an "500 internal server error".
Yesterday I updated to PHP 7.1.2 and then there were a lot of errors.
I solved them almost all except for this strange coding error.
When trying to find the problem I echoed some output to the screen.
And this is what happens:
for($iRecordNumber=0;$iRecordNumber==$iTotalAssignments;$iRecordNumber++){
$iNextRecordNumber = $iRecordNumber+1;
echo $iNextRecordNumber;
echo $aAssignments[$iRecordNumber][2];
echo $aAssignments[$iNextRecordNumber][2];
}
With this code I get the "500-error".
When I comment out the line with echo $aAssignments[$iNextRecordNumber][2]; the error is gone and it does what it should do. Showing me a record from the nested array $aAssignments(PDO query).
for($iRecordNumber=0;$iRecordNumber==$iTotalAssignments;$iRecordNumber++){
$iNextRecordNumber = $iRecordNumber+1;
echo $iNextRecordNumber;
echo $aAssignments[$iRecordNumber][2];
// echo $aAssignments[$iNextRecordNumber][2];
}
I looked through the Backward compatabilty list but did not find anything.
And the tool codechecker says it's good too.
I dont know how this is possible.
What can I do to find an answer? Are there any programs or code checkers to check for more php 7 errors in my code. I have several php sites running and I do not want to check all code manually for errors.
I think $aAssignments[$iNextRecordNumber][2] is not always set.
You could replace:
echo $aAssignments[$iNextRecordNumber][2];
By:
echo $aAssignments[$iNextRecordNumber][2]?? '';
I hope it could help.
In PHP 7 is the error handling completly revised. A fatal error other errors no longer stops the script. To find these kind of errors, use the new error() method in the try and catch class. For example:
try{
code to test on errors
} catch(error $eCatchedError) {
echo get_class($eCatchedError)."<br>".$eCatchedError->getLine()."<br>".$eCatchedError->getFile()."<br>".$eCatchedError->getMessage();
} // End try and catch
By this way you get an more elegant way of showing an error (with css and everything)

Handle errors from external library (simple_html_dom)

I'm building a page to crawl some web pages.
It works, usually, but everyone once in a while, it will fail to grab the page, and throw the following error:
( ! ) Warning: file_get_contents(URLWASHERE): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in Z:\Wamp\www\spider\simple_html_dom.php on line 555
Here is how I'm grabbing the page:
$page->load_file('URLWASHERE');
Is there a way to figure out if that error happens? I don't know how to detect it because it's in the library, not my code.
I can't use if (!$page) because it still returns something. But that something doesn't seem very helpful, though it is significantly shorter.
You can see output here:
$page when successful: http://pastebin.com/CnRVP6SK
$page when failed: http://pastebin.com/t9q6Gwnf
I just want to be able to find out if there was an error so I can have my program try again.
You can use the error_get_last() function to get info about the last error. You might also consider silencing the warning message with the # operator.
#file_get_contents('http://example.com/wjqlshqwd');
$error = error_get_last();
if($error && strpos($error['message'], '404') !== false)
{
echo 'There was an error';
}
Also before running this code you should reset the state of error_get_last(), a comment on the PHP manual page describes a trick to do that:
// var_dump or anything else, as this will never be called because of the 0
set_error_handler('var_dump', 0);
#$undef_var;
restore_error_handler();
// error_get_last() is now in a well known state:
// Undefined variable: undef_var
The concept is just to create a known error.
It seems I can use
if(error_get_last())
to check if an error has been thrown so far.
This will break if other errors are encountered, but my code seems to be free of errors aside from this occasional one, so I will use this.
unfortunately, this will only allow me to try twice, rather than keep trying until it works.

register_shutdown_function() still outputs original error message

I am trying to replace the built in php shutdown_function with a custom one.
It works perfectly, however, it still outputs the original error (built in error) above my new error message.
<?php
function shutdown_output() {
$error = error_get_last();
if($error !== NULL) {
echo "ERROR";
exit();
} else {
echo "NO ERROR";
}
}
// Set the error reporting:
register_shutdown_function('shutdown_output');
// test.php does not exist, just here to get a critical error
require_once("test.php");
?>
Any ideas?
As already mentioned in comments, the use of register_shutdown_function is not going to override built-in error handling (the same way that set_error_handler doesn't either).
If you don't want to see the original message, disable the output of them in your php.ini using display_errors = 0. Or on the fly in your script using ini_set('display_errors', 0);.

PHP 4 try catch alternative?

Could someone help me translate this code to PHP 4?
try
{
$picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); // This is the original statement, this works on PHP4
}
catch(Exception $ex)
{
$msg = "Error opening $imgFile for Product $row['Identifier']";
throw new Exception($msg);
}
Basically when there is a fatal error I need to get the $row['Identifier'] so I know what product is causing the error.
Thanks in advance.
EDIT: I don't know what PHP_open_image_file does, but sometimes I get errors like below, and I need to get the product identifier that is causing the error.
Fatal error: PDFlib error [1016]
PDF_open_image_file: Couldn't open
JPEG file 'picture/b01_le1x.jpg' for
reading (file not found) in
/var/www/html/catalogue/pdf_make.php
on line 618
Am I correct in assuming you are using PDF_open_image_file() from the pdflib PECL extension?
If so, then it will never throw an exception on PHP 4. I would assume error states are reported through the result, which is an int and thus probably < 1 in case of errors.
//try
if (file_exists($imgFile)) {
$picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0);
}
//catch
if (!$picture) {
$msg = "Error opening $imgFile for Product $row['Identifier']";
print $msg;
}
I've updated this with file_exists to prevent your fatal error.
As addendum question, why were you trying to rethrow an exception on PHP4?
You can catch some problems by setting a default error handler (see PHP Manual entry), but that won't let you catch E_ERRORS.
I don't think this will be possible in PHP4, you'll need to upgrade to PHP5 so it throws an exception instead of an E_ERROR. You may be able to catch certain errors before they happen - for example by running file_exists() on your input file, but you are unlikely to be able to think of and catch all the errors that PDFLib will.

Categories