Upload image in PHP

1

Hi, I'm a bit rusty in PHP so, in the middle of refreshing concepts, I'm doing a form to upload images to a BD (the name and the path, really) and then, if that image is in the folder of the server specified in the route, show it on the 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";
}

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

I have tried to print the result of the variable $ origin and it returns a type path:

C:\xampp\tmp\php719F.tmp

By accessing that folder I find that this file does not exist ... Could that be the basis of the problem?

    
asked by user12555 08.08.2016 в 18:20
source

2 answers

2

What happens is that you are not uploading the image to the server, replicate your environment of the theme and I made a few minor modifications and if it works ; remember that $ source = $ _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";

                    }
?>

conexion.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); 
?>

and this is the database

and this is the result when the photo comes up

If you want you could put your input that retrieves the name of the photo, I did not put it because it did not seem like that, I think you should keep your name from the upload file.

    
answered by 08.08.2016 в 23:15
0

This is the code that I use. The name "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.";
            }

        }
    }
    
answered by 08.08.2016 в 22:49