7

While trying to upload a file using a form and PHP/IIS 7, I get this message:

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

My form:

<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.";
}

I've set the path for the temp files:

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

And gave permissions for IIS_IUSRS and IUSRS with total control. But every time, I get the error. I don't know what I'm doing wrong. If someone can help me, it would be great.

Community
  • 1
  • 1
aerojun
  • 1,206
  • 3
  • 15
  • 28

3 Answers3

8

Made it work.

Changed

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

Create the folder TEMP in C and gave the permissions. Seems like it only works when connecting to C: directly.

aerojun
  • 1,206
  • 3
  • 15
  • 28
2

I finaly found out, why this error actually happens:

The IUSR Account (or the account the php process impersonates, depending on your authentication settings) needs to be able to enumerate the Parent Folder of the upload_tmp_dir folder.

This behaviour is odd, because this right is not needed for the log or sessions folders.

My Solution is the following (using the paths from above post):

  1. Create the folder "C:/Users/server/Pictures/tmp"
  2. Grant modify rights to IUSR (or other user) on that folder
  3. Create the folder "C:/Users/server/Pictures/tmp/uploads"
  4. Edit php.ini: upload_tmp_dir = "C:/Users/server/Pictures/tmp/uploads"

Alternatively you can grant read only rights on "this folder only" on the parent folder "C:/Users/server/Pictures/tmp". In that case you dont need another subfolder.

japi
  • 101
  • 1
  • 3
0

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

Answer added to this post also... Warning: File upload error - unable to create a temporary file in Unknown on line 0

rakesh
  • 51
  • 5