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

15

4

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 .

Novelistic answered 23/8, 2013 at 5:12 Comment(3)
How much free space do you have in /tmp (or whatever the MAC equivalent of that is)? What about permissions? Does the web server user have permission to write to it?Klina
@Klina how do i find that?Novelistic
Try ls -ld /tmp to find the permissions. Then figure out what the web server user is and see if it has permission to write there based on the output of that command. You can get free space with the df -h command. If /tmp is not listed there, then it doesn't have its own partition and you will have to look at the root file system's free space instead.Klina
H
14

In a terminal type :

chmod -R 777 /your/upload/folder

you can know the folder where your php uploads files by executing this code :

$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
die($tmp_dir);

In my case I get /var/... so when I sett 777 permission I get no error.

Edit

Setting 777 to /var is dangerous so you have to set it only to the folder where php uploads the file (which you can get buy the code above).

Holmes answered 19/10, 2013 at 14:37 Comment(4)
@brunocascio the upload folder varies from platform to another, in my case it was in /var/....Holmes
Uncommenting sys_temp_dir = "/tmp" in /etc/php/7.2/apache2/php.ini did the trick for me.Fluidextract
Thanks pbies. For me it was in /etc/php.ini sys_temp_dir = "/tmp" in /etc/php.ini php7.3 on Centos7Dewittdewlap
didn't work for me can you have any other solution ?Intemperate
M
7

You should check your php.ini and look for the 'upload_tmp_dir' option. It is empty by default. After that, check the permission of your tmp dir. If you want to know what upload_tmp_dir your server is using, you can simply use this snippet of code

$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
die($tmp_dir);

Finally in a terminal shell you can type : chmod -R 777 'your_tmp_upload_dir'

Mcginley answered 18/3, 2014 at 14:16 Comment(0)
T
4

I was getting the same error when uploading files from WordPress. I got this error in the apache error log file:

"PHP Warning: File upload error - unable to create a temporary file in Unknown".

I fixed the error by commenting out upload_tmp_dir in /etc/php.ini. The upload_tmp_dir was set to /tmp. I also set the date.timezone. After rebooting the server I was able to upload media files from WordPress.

It seems php did not have permissions to write to the /tmp folder.

Trevelyan answered 16/10, 2015 at 13:57 Comment(0)
D
3

Recently while working in Laravel and Wordpress (php version 7.3) I got this error "File upload error - unable to create a temporary file in Unknown on line 0"

Solution that worked for me is. Hope this helps anyone facing such issue.

Laravel

  1. Create a tmp folder in project root folder. Make sure it has write permissions properly set.
  2. Create php.ini file in public folder.

Wordpress

  1. Create a tmp folder in project root folder. Make sure it has write permissions properly set.
  2. Create php.ini file in wp_admin folder.

Add below lines specifically in php.ini in addition to your other ini settings.

upload_tmp_dir = on

upload_tmp_dir = "/path/to/tmp/folder"

here path will be like /home/cpanel username/public_html/folder/project/tmp

NOTE: In case you are working in Laravel.. do not forget to link storage folder if you are storing files in storage folder. For this run below command at terminal.

php artisan storage:link

Directive answered 18/3, 2021 at 12:13 Comment(0)
B
2

If you don't know what it is, it is SeLinux. It is enabled as default in certain distributions like CentOS.

Check if you have SELinux running:

sestatus

In the case you have SELinux enabled, fix your permissions with the chcon command.

This is just an example for nginx:

chcon -R -h -t httpd_sys_script_rw_t /usr/share/nginx/tmp
chown -R nginx:nginx /usr/share/nginx/tmp
chmod -R ug+rw /usr/share/nginx/tmp

tmp dir, upload dir in chroot need be set to /tmp not full path.

Boggers answered 5/8, 2016 at 18:14 Comment(0)
P
1

Possibly it is /tmp directory problem. Check that /tmp has correct access privileges and enough space size.

Prehistory answered 23/8, 2013 at 5:23 Comment(1)
I was debugging for a day, /tmp is writable but sometimes I cannot write and get that warning. After i checked with large file found the issue which is size of content we write into that. Thanks @Prehistory you saved my dayWherefrom
O
0

use double quotes in ../inventory_images/$newname.

change this line

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

AS

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

NOTE:

in php with single quotes $newname will be display as string.

for displaying value of a variable use double quotes

Orange answered 23/8, 2013 at 5:21 Comment(2)
I have posted same answer before you. Don't duplicate answers.Cantaloupe
at the time of staring there is no ans.. its a co-incident.Orange
H
0

Change permissions of the 'upload' directory. You'll need to give the web server access to it before it can move files into it..

Change your upload folder permission "Read only" to "Read & Write"

**Edited**

Read this link and also read this links. Its guide you how to change file permission.

Haughty answered 23/8, 2013 at 5:31 Comment(2)
How do I do this on MAC , I am not able to fing the /tmp folder. Sorry If this is a stupid question ,Novelistic
Check this links. and change your inventory_images folder permissionHaughty
K
0

You don't say what your platform is, but if it's a later version of Fedora (17+), the setting of PrivateTmp=true in the service file can cause this issue. Creating a service file in /etc with PrivateTmp=false is at least one way to fix that, although it involves rolling back a security feature.

Kristalkristan answered 28/10, 2013 at 18:39 Comment(1)
You missed it, it's at the bottom: Test Server:XAMPP , OS : MAC 10.8 :)Scientific
V
0

In my case it was caused by running httpd.exe Apache server from console instead of service.

Vulcanology answered 15/2, 2016 at 23:20 Comment(0)
A
0

For me this issue manifested itself due to lack of Inodes on the server:

df -ih
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/xvda1       512K  512K     0  100% /  

Identified the culprit directory using: https://mcmap.net/q/92552/-how-to-free-inode-usage and removed undesired files.

Alina answered 16/4, 2020 at 11:15 Comment(0)
I
0

If none of the existing answers work, the issue I had was that Windows folder is limited to 65,560 items. Once I cleared the folder, it started re-working.

Innuendo answered 19/4, 2020 at 18:2 Comment(0)
D
0
  1. In php.ini open_basedir=/tmp:/rootdir Restart apache: systemctl restart apache2
  2. tmp has full access : chmod 777 /tmp
  3. Files folder should have full permission: chmod -Rf a+x sites/default/files
Douzepers answered 23/7, 2020 at 14:29 Comment(0)
W
0

I had the same problem when I mounted an external disk as /tmp on WP server. I also saw some articles that suggested full /tmp cleanup (via rm -rf /tmp/*) can cause something similar. It took me a while to figure out how to fix it because the main suspects are wp-config.php and php.ini. But as it turned out those were red herrings, and not setting WP_TEMP_DIR or creating your custom php.ini(my image had only php.ini-development and php.ini-production) is totally Ok. Wordpress has sensible default behaviour to identify temporary dir.

Before manipulating any configs make sure your /tmp dir is intact and doesn't have anything unusual about it(like a different filesystem, symlinking, etc.)

Watchman answered 26/2, 2021 at 10:26 Comment(0)
P
0

I am using PHP on windows server and my soluition is:

open "elFinderVolumeDriver.class.php" and find the line:

// maximum number of chunked upload connection. `-1` to disable chunked upload
    'uploadMaxConn' => 3,

and change values 3 to -1 :

// maximum number of chunked upload connection. `-1` to disable chunked upload
    'uploadMaxConn' => -1,

In my case chunked upload cause the problem.

Prather answered 17/2, 2022 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.