Upload image in PHP

Good, I'm a bit rusty in PHP so, in the middle of refreshing concepts, I'm making a form to upload images to a database (the name and the path, really) and then, in case that said image is in the server folder specified in the path, display it by Screen.

The code that performs the data insertion is this:

if ($_POST['enviar']) {
$nombre = $_REQUEST['nombre'];
$nombrer = strtolower($_REQUEST['nombre']);
$origen= $_FILES['foto']['tmp_name'];
$destino = "img/" . $nombrer . ".jpg";
copy ($origen,$destino);
$subida = mysqli_query($conexion,"INSERT INTO imagenes VALUES ('". $nombre ."','" . $destino . "')");   
if (@mysqli_query($conexion,$subida)) {
    echo "La foto se ha subido con éxito";
}

Inserting data into the table is done correctly, the problem I am having to copy the file that I upload, the image, to the /img folder of the server. As far as I can remember, that was done using the copy function, which was passed as parameters the source URL in /tmp and the destination URL.

I have tried to print the result of the origen source variable and it returns me a path Type:

C:\xampp\tmp\php719F.tmp

When accessing that folder I find that file does not exist... Can that be the basis of the problem?

 2
Author: user12555, 2016-08-08

2 answers

What happens is that you are not uploading the image to the server, replicate your theme environment and I made some minimal modifications and if it works ; Remember that origen origin= $_FILES ['photo'] ['tmp_name']; it must be equal to the name of your input type file;

Index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form action="foto_post.php" method="POST" enctype="multipart/form-data">
            <table width="350" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000">
                <tr>
                    <td height="85" align="center" valign="middle" bgcolor="#FFFFFF">
                        <div align="center">
                            <input name="imagen" type="file" maxlength="150">
                            <br><br>                                     
                            <input type="submit" value="Agregar" name="enviar" style="cursor: pointer">
                        </div>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

Foto_post.php

<?php

require_once("conexion.php");

$nombre = $_FILES['imagen']['name'];
$nombrer = strtolower($nombre);
$cd=$_FILES['imagen']['tmp_name'];
$ruta = "img/" . $_FILES['imagen']['name'];
$destino = "img/".$nombrer;
$resultado = @move_uploaded_file($_FILES["imagen"]["tmp_name"], $ruta);

if (!empty($resultado)){

                @mysqli_query($conexion,"INSERT INTO fotos VALUES ('". $nombre."','" . $destino . "')"); 
                echo "el archivo ha sido movido exitosamente";

                }else{

                    echo "Error al subir el archivo";

                    }
?>

Connection.php

<?php

    $hostname_cn = "localhost";
    $database_cn = "imagen";
    $username_cn = "root";
    $password_cn = "";
    $conexion = mysqli_connect($hostname_cn, $username_cn, $password_cn,$database_cn) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

enter the description of the image here

And this is the database enter the description of the image here

And this is the result when you upload the photo enter the description of the image here

If you want you could put your input that retrieves the name of the photo, I did not put it because I did not look like that, it seems to me that it should stay with its name of the Upload file.

 4
Author: Kpeski2814, 2016-08-08 21:15:02

This is the code I use. The name of "cover", is the name of my input

if (!isset($_POST['portada'])){
        $nombre_archivo =$_FILES['portada']['name'];
        $tipo_archivo = $_FILES['portada']['type'];
        $tamano_archivo = $_FILES['portada']['size'];
        $archivo= $_FILES['portada']['tmp_name'];
    } else{
        $nombre_archivo="";
    }

    if ($nombre_archivo!="")
    {
        //Limitar el tipo de archivo y el tamaño    
        if (!((strpos($tipo_archivo, "gif") || strpos($tipo_archivo, "jpeg") || strpos($tipo_archivo, "png")) && ($tamano_archivo  < 50000000))) 
        {
            echo "El tamaño de los archivos no es correcta. <br><br><table><tr><td><li>Se permiten archivos de 5 Mb máximo.</td></tr></table>";
        }
        else
        {
            $file = $_FILES['portada']['name'];
            $res = explode(".", $nombre_archivo);
            $extension = $res[count($res)-1];
            $nombre= date("YmdHis")."." . $extension; //renombrarlo como nosotros queremos
            $dirtemp = "../../upload/temp/".$nombre."";//Directorio temporaral para subir el fichero

            if (is_uploaded_file($_POST['portada']['tmp_name'])) {
                copy($_FILES['portada']['tmp_name'], $dirtemp);

                unlink($dirtemp); //Borrar el fichero temporal
               }
            else
            {
                echo "Ocurrió algún error al subir el fichero. No pudo guardarse.";
            }

        }
    }
 1
Author: Jose Javier Segura, 2016-08-08 20:49:36