File upload error unable to create a temporary file in unknown on line 0

Warning: File upload error - unable to create a temporary file in Unknown on line 0, php, javascript, file-upload, image-uploading

Am getting the following error everytime I try to upload a file .

«Warning: File upload error — unable to create a temporary file in Unknown on line 0»

Here is my HTML form,

<form action="./inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
<table width="625" border="1" cellpadding="5">
  <tr>
    <td width="84">Product Name</td>
    <td width="399"><input type="text" name="product_name" id="product_name"></td>
  </tr>
  <tr>
    <td>Product Price</td>
    <td><label for="textfield2">Rs:</label>
      <input type="text" name="price" id="price"></td>
  </tr>
  <tr>
    <td>Category</td>
    <td><select name="category" id="category">
        <option value="" selected="selected"></option>
        <option value="Bedroom ">Bedroom </option>
        <option value="Living">Living room</option>
        <option value="Dining">Dining</option>
      </select></td>
  </tr>
  <tr>
    <td>Sub - Category</td>
    <td><select name="subcategory" id="subcategory">
        <option value="" selected="selected"></option>
        <option value="dinet">Dining tables</option>
        <option value="shoe">shoe racks</option>
        <option value="wardrobe">wardrobes</option>
        <option value="sofa">sofa</option>
      </select></td>
  </tr>
  <tr>
    <td>Product Details</td>
    <td><textarea name="details" cols="50" rows="10" id="details"></textarea></td>
  </tr>
  <tr>
    <td>Product Image</td>
    <td><label>
        <input type="file" name="fileField" id="fileField"/>
      </label></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Add this Item now"></td>
  </tr>
</table>
</br>
</form>

Here is my PHP code ,

if(isset($_POST["product_name"]))
{
    $product_name = mysql_real_escape_string($_POST["product_name"]);
    $price= mysql_real_escape_string($_POST["price"]);
    $category= mysql_real_escape_string($_POST["category"]);
    $subcategory= mysql_real_escape_string($_POST["subcategory"]);
    $details= mysql_real_escape_string($_POST["details"]);

    //see if duplicate product exists
    $sql = mysql_query("select id from products where product_name='$product_name' limit 1");
    $product_match = mysql_num_rows($sql);   //count the output

    if($product_match>0)
    {
        echo "The product name already exists";
        exit();
    }
    $sql= mysql_query("INSERT INTO `mystore`.`products` (`product_name`, `price`, `details`, `category`, `subcategory`, `date_added`) VALUES ( '$product_name', '$price', '$details', '$category', '$subcategory', now());")or die(mysql_error());
    $pid = mysql_insert_id();
    $newname = "$pid.jpg";

    move_uploaded_file($_FILES['fileField']['tmp_name'],'../inventory_images/$newname');
}

Am trying to upload on localhost ,
Test Server:XAMPP ,
OS : MAC 10.8

Am stuck on this from a long time , I tried a lot of things but nothing is working .

So you have a PHP website and out of sudden, when users send POST requests or upload files, you get the error message:

PHP Warning: Unknown: POST data can't be buffered; all data discarded in Unknown on line 0

Well there are different reasons why you may get such an error. This articles goes through the common causes and solutions for this error.

Understanding the error

The error says that PHP was unable to buffer the post data, but it doesn’t really say why. Looking at the code, we see that the actual cause of the error is that the number of bytes written to the buffer is different to the number of bytes read from the input. Unfortunately if the logs don’t tell us anything additional, it is just outputting the symptom not the cause.

To make things worse, it is a warning a not a real error, meaning that your code will continue the flow on a corrupted state.

Possible causes to the error and how to fix them

Permissions on the temporary folder

The most likely cause for this error is that the user running the PHP process doesn’t have the right to save a file in the temporary folder.

Usually if the problem is in the temporary folder, you should also see the warning message:

Warning: File upload error - unable to create a temporary file in Unknown on line 0

The temporary folder for file uploads is defined by the php.ini directive upload_tmp_dir. When empty it uses the system default temporary directory (in Linux usually /tmp).

You can type this in the command line to find out what is the temporary dir.

php -r 'echo ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();'

Once you know the folder you can change the permissions with chmod (in the below example for the /tmp folder)

chmod -R 777 /tmp

Out of disk space or quota

Similar to the error above, if PHP is receiving big post requests and there is not enough capacity for the server or the user to save a temporary file, you should get the same warning as above. To check the available space in the temporary folder you can try the command diskfree (df). The example below assumes that the temporary folder is /tmp

$ df /tmp
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 4062912 76344 3760472 2% /tmp

If you are out of disk space, well… get more space 🙂

Out of memory

If your process runs out of memory, you might get the same error. In this case, an out of memory message should precede the POST warning.

You have to check if the server (or virtual machine) is running out of memory or if the PHP reached its memory limit. PHP memory limits are configured by the directive memory_limit.

Upload file limits

PHP has several directives to control how big files can be and how many files an user can upload. If you hit any of these limits you might receive this error messages. You can adjust the upload limits through the php.ini directives:

  • post_max_size
  • max_input_time
  • max_input_nesting_level
  • max_input_vars
  • upload_max_filesize

Other possible errors

Some other documented cases:

  • A server reboot solved the problem, because of a pending update.
  • A wordpress plugin was writing big files, filling up the tmp folder.

This is a problem that keep recurring.

We have servers with PHP FPM and for some strange reason this problem keep happening:

Got error ‘PHP message: PHP Notice: Unknown: file created in the system’s temporary directory in Unknown on line 0nPHP message: PHP Warning: File upload error — unable
to create a temporary file in Unknown on line 0n’.

Our user php conf looks like the following when it first happened:

[DAUSER]
user = $pool
group = $pool
listen = /usr/local/php72/sockets/$pool.sock
listen.owner = $pool
listen.group = apache
listen.mode = 660
pm = ondemand
pm.max_children = 100
pm.process_idle_timeout = 20
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f [email protected]
php_admin_value[session.save_path] = /home/DAUSER/tmp
php_admin_value[open_basedir] = /home/DAUSER/:/tmp/:/var/tmp/:/usr/local/php72/lib/:/usr/local/php54/lib/:/usr/local/php55/lib/:/usr/local/php56/lib/:/usr/local/php70/lib/:/usr/local/php71/lib/:/usr/local/php72/lib/:/usr/local/lib/php/
php_admin_value[mail.log] = /home/DAUSER/.php/php-mail.log
security.limit_extensions = .php .php52 .php53 .php54 .php55 .php56 .php60 .php70 .php71 .inc .php72

It was thought that the actual fix was, by adding the tmp variables in the user php fpm conf:

env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Restart php-fpm, the problem seemed fix at first. But, our websites still cannot upload files.

The temporary fix seems to restart php fpm. But, after a while the problem happens again.
And the conf still has the /tmp variables.

We added, in all the php.ini we could find, the upload_tmp_dir = /tmp, and that alse was a temporary fix.

EDIT:

—— ——

This has happened on multiple shared and dedicated servers, with multiple websites.

I did a test on our test server:
`sys_get_temp_dir()`: gives the

`/tmp`

`ini_get(‘upload_tmp_dir’)`: gives no value:

`string(0) «»`

But my test files uploaded fine

Test code used from:https://www.w3schools.com/php/php_file_upload.asp

`PrivateTmp=true` is set to true on the PHP FPM instance. This should not have any effect, because we are giving /tmp as the specified dir…?

— CentOS Linux release 7.5.1804 (C
— DirectAdmin server
— PHP-FPM
— PHP 7.2.10 (FPM)

—— ——

Is there any way how to permanently fix this issue? The keeps happening on multiple server of our customers, and we can’t just keep restarting PHP-FPM

Понравилась статья? Поделить с друзьями:
  • Fifa mobile как изменить название клуба
  • Fatal error allowed memory size of 1610612736 bytes exhausted tried to allocate 20480 bytes
  • Fg wilson dcp 10 ошибки
  • Fatal error 190 too many error messages on one line
  • Ffxiv error code i2501