【问题标题】:Unable to create a temporary file while uploading上传时无法创建临时文件
【发布时间】:2013-05-05 12:37:41
【问题描述】:

在尝试使用表单和 PHP/IIS 7 上传文件时,我收到以下消息:

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

我的表格:

<form action="acciones.php" id="form3" method="POST" enctype="multipart/form-data">
    <input type="hidden" value="3" name="accion">
    <input type="text" name="nombre" placeholder="Nombre">
        <input type="file" name="imagen" accept="image/x-png, image/gif, image/jpeg" />
    <input type="button" id="envio" class="button azul" value="Agregar" onclick="envios()">
</form>

My PHP code and reference:

$target = "/images/";
$target = $target . basename( $_FILES['imagen']['name']);
//This gets all the other information from the form
$name=$_POST['nombre'];
$pic=($_FILES['imagen']['name']);

//Writes the information to the database
$query = "INSERT INTO Playeras (Nombre, Ruta) VALUES ('$name', $pic')";
mysql_query($query, $conexion -> conn) or die("Error: ".mysql_error()) ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['imagen']['tmp_name'], $target))
{
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else 
{
 //Gives and error if its not
 echo "Sorry, there was a problem uploading your file.";
}

我已经设置了临时文件的路径:

upload_tmp_dir = "C:/Users/server/Pictures/tmp"

并授予 IIS_IUSRS 和 IUSRS 完全控制权限。但每次,我都会得到错误。我不知道我做错了什么。如果有人可以帮助我,那就太好了。

【问题讨论】:

  • 你是否已经创建了tmp目录?尝试创建 tmp 目录来解决问题。
  • 是的,存在并且有权限
  • 尝试从 php 代码中设置临时路径 ini_set("upload_tmp_dir","C:/Users/server/Pictures/tmp");或 ini_set("upload_tmp_dir","server/Pictures/tmp");
  • 只是没有运气。一直显示同样的错误。
  • 你能分享一下$_FILES['imagen']的结果吗

标签: php forms iis upload


【解决方案1】:

让它工作。

改变了

upload_tmp_dir = "C:/Users/server/Pictures/tmp" to "C:\TEMP".

在 C 中创建文件夹 TEMP 并授予权限。似乎它只在直接连接到 C: 时才有效。

【讨论】:

    【解决方案2】:

    我终于找到了,为什么会发生这个错误:

    IUSR 帐户(或 php 进程模拟的帐户,取决于您的身份验证设置)需要能够枚举 upload_tmp_dir 文件夹的 父文件夹

    这种行为很奇怪,因为日志或会话文件夹不需要此权限。

    我的解决方案如下(使用上面帖子中的路径):

    1. 创建文件夹“C:/Users/server/Pictures/tmp”
    2. 授予IUSR(或其他用户)对该文件夹的修改权限
    3. 创建文件夹“C:/Users/server/Pictures/tmp/uploads”
    4. 编辑 php.ini:upload_tmp_dir = "C:/Users/server/Pictures/tmp/uploads"

    或者,您可以在父文件夹“C:/Users/server/Pictures/tmp”上授予“仅限此文件夹”的只读权限。在这种情况下,您不需要另一个子文件夹。

    【讨论】:

      【解决方案3】:

      最近在 Laravel 和 Wordpress(php 版本 7.3)中工作时,我收到此错误“文件上传错误 - 无法在第 0 行的未知中创建临时文件”

      对我有用的解决方案是。希望这可以帮助任何面临此类问题的人。

      Laravel

      1. 在项目根文件夹中创建一个 tmp 文件夹。确保它已正确设置写入权限。
      2. 在公共文件夹中创建 php.ini 文件。

      WordPress

      1. 在项目根文件夹中创建一个 tmp 文件夹。确保它正确设置了写入权限。
      2. 在 wp_admin 文件夹中创建 php.ini 文件。 除了您的其他 ini 设置之外,还专门在 php.ini 中添加以下行。

      upload_tmp_dir = 开启

      upload_tmp_dir = "/path/to/tmp/文件夹"

      这里的路径类似于 /home/cpanel username/public_html/folder/project/tmp

      注意:如果您在 Laravel 中工作。如果您将文件存储在存储文件夹中,请不要忘记链接存储文件夹。为此,请在终端运行以下命令。

      php artisan storage:link

      这个帖子也添加了答案...Warning: File upload error - unable to create a temporary file in Unknown on line 0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-10
        • 1970-01-01
        • 1970-01-01
        • 2013-03-22
        • 2020-01-19
        相关资源
        最近更新 更多