简体   繁体   中英

How to Avoid “Warning: file_get_contents(): Filename cannot be empty” When Checking for an Empty Value

I have a PHP script which reads from a CSV and part of the script has an if statement that will validate a cell within the CSV on whether the cell is empty or not. If the cell is empty, the code within the if statement is executed. If the cell is not empty, then nothing needs to happen.

Although the script works perfectly and does exactly what I need, I am receiving the following warning:

在此处输入图片说明 It looks as though this warning will always show if the code is checking against an empty cell within the CSV. The fact that the cells are empty isn't an issue because some of the cells in the CSV are rightly empty, which is the whole point of having an if statement to check whether the cell is empty or not.

Here is the segment of my script relating to this:

// If the previous row has an empty 'Main Image' field, add the additional image field for that row.
// The line below is line 145 from the screenshot above.
if (!empty(base64_encode(file_get_contents($previousDataLine['Main Image'])))) {
        $client->catalogProductAttributeMediaCreate(
            $sessionId,
            $dataLine['Product SKU'],
            array(
                'file' => array(
                    'content' => base64_encode(file_get_contents($dataLine['All Images (One Per Row)'])),
                    'mime' => 'image/png',
                ),
                'position' => $dataLine['Image Rank Position'],
                'exclude' => '0'
            ),
            0
        );
}

Have I done something wrong? Why does PHP insist on showing me this warning even though I only execute code when the cell is not empty. Thank you for your time.

This required removal of the base64_encode(file_get_contents()) part of the code.

if (!empty(base64_encode(file_get_contents($previousDataLine['Main Image'])))) {

became:

if (!empty($previousDataLine['Main Image'])) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM