php 警告:文件上传错误 - 无法在第 0 行的 Unknown 中创建临时文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18395163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:28:43  来源:igfitidea点击:

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

phpjavascriptfile-uploadimage-uploading

提问by CleanX

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"

“警告:文件上传错误 - 无法在第 0 行的未知中创建临时文件”

Here is my HTML form,

这是我的 HTML 表单,

<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 ,

这是我的 PHP 代码,

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

我正在尝试在本地主机上上传,测试服务器:XAMPP,操作系统:MAC 10.8

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

很长一段时间都被困在这个问题上,我尝试了很多东西,但没有任何效果。

回答by zizoujab

In a terminal type :

在终端类型中:

chmod -R 777 /your/upload/folder

chmod -R 777 /your/upload/folder

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

您可以通过执行以下代码知道您的 php 上传文件的文件夹:

$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.

在我的情况下/var/...,当我设置 777 权限时,我得到了没有错误。

Edit

编辑

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

将 777 设置/var为危险,因此您只需将其设置为 php 上传文件的文件夹(您可以购买上面的代码)。

回答by hellbreak

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

您应该检查您的 php.ini 并查找“upload_tmp_dir”选项。默认为空。之后,检查您的 tmp 目录的权限。如果你想知道你的服务器正在使用什么 upload_tmp_dir,你可以简单地使用这段代码

$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'

最后,您可以在终端 shell 中键入:chmod -R 777 'your_tmp_upload_dir'

回答by Nadir Latif

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

从 WordPress 上传文件时,我遇到了同样的错误。我在 apache 中遇到了这个错误error log file

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

“PHP 警告:文件上传错误 - 无法在未知中创建临时文件”。

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

我通过upload_tmp_dir/etc/php.ini. 将upload_tmp_dir被设置为/tmp。我还设置了date.timezone. 重新启动服务器后,我能够从 WordPress 上传媒体文件。

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

似乎 php 没有写入/tmp文件夹的权限。

回答by user956584

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

如果你不知道它是什么,那就是SeLinux。它在某些发行版(如 CentOS)中默认启用。

Check if you have SELinux running:

检查您是否正在运行 SELinux:

sestatus

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

如果您启用了 SELinux,请使用chcon命令修复您的权限。

This is just an example for nginx:

这只是一个例子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.

tmp 目录,chroot 中的上传目录需要设置为 /tmp 不是完整路径。

回答by ITemius

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

可能是 /tmp 目录问题。检查 /tmp 是否具有正确的访问权限和足够的空间大小。

回答by Nathan Srivi

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

更改“上传”目录的权限。您需要先授予 Web 服务器访问权限,然后才能将文件移入其中。

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

将您的上传文件夹权限“ Read only”更改为“ Read & Write

**Edited**

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

阅读此链接并阅读此链接。它指导您如何更改文件权限。

回答by Rejayi CS

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

在 ../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.

在带有单引号的 php 中 $newname 将显示为字符串。

for displaying value of a variable use double quotes

用于显示变量的值使用双引号

回答by lukyer

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

在我的情况下,它是由从控制台而不是服务运行 httpd.exe Apache 服务器引起的。

回答by stinkoid

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.

你没有说你的平台是什么,但如果是更高版本的 Fedora (17+),服务文件中的 PrivateTmp=true 设置可能会导致这个问题。使用 PrivateTmp=false 在 /etc 中创建服务文件至少是解决此问题的一种方法,尽管它涉及回滚安全功能。

回答by MarkoPee

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://stackoverflow.com/a/9387415/7637065and removed undesired files.

使用以下方法确定罪魁祸首目录:https: //stackoverflow.com/a/9387415/7637065并删除不需要的文件。