Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Bad File Descriptor Error

Tags:

php

apache

We've recently upgraded our servers from PHP 5.4.15 to 5.5.1 and have started getting this error in the logs

Fatal Error Unable to create lock file: Bad file descriptor

I've tracked it down to this bit a code that opens another small PHP script which uploads a file to S3 in the background.

// Grab uploaded file and assign a working name
$fileTemp = $_FILES['file']['tmp_name'];
$pathToWorkingFile = tempnam($g_TmpDirectory, "TR-");

// Move the file to our working area        
if (move_uploaded_file($fileTemp, $pathToWorkingFile) === false)
    throw new Exception("Cannot move file to staging area.", 1011);

// Where the file will end up on S3
$s3Bucket = "test.bucket.com";
$uploadDest = "/uploads/image123.jpg";

// Create process to upload file in background
popen("/usr/local/bin/php /path/to/uploadScript.php $pathToWorkingFile $s3Bucket $uploadDest &", 'r');
like image 589
Jared D Avatar asked Apr 13 '26 05:04

Jared D


2 Answers

It turns out that this error was caused by our configuration of OPcache which was enabled during the PHP upgrade process. When I disable it for command line operations by removing this setting from php.ini everything works fine.

opcache.enable_cli=1
like image 85
Jared D Avatar answered Apr 14 '26 18:04

Jared D


I was able to resolve with opcache.enable_cli=1, but for me the underlying problem was wrong permissions on the /tmp directory in MacOS.

This is what I did to fix this:

sudo rm -Rf /tmp
sudo rm -Rf /private/tmp
sudo mkdir /private/tmp
sudo chown root:wheel /private/tmp
sudo chmod 1777 /private/tmp
sudo ln -s /private/tmp /tmp
like image 45
Jeremy John Avatar answered Apr 14 '26 19:04

Jeremy John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!