Turns out I have this form:
<form action="index.php" method="post" enctype="multipart/form-data">
<div class="form">
<h3 class="row justify-content-center mt-5">Seleccione el tiempo en minutos que estará su imagen en línea</h3>
<div class="row justify-content-center mt-1">
<div class="btn-group" role="group">
<button type="button" class="btn btn-warning">1</button>
<button type="button" class="btn btn-warning">5</button>
<button type="button" class="btn btn-warning">15</button>
<button type="button" class="btn btn-warning">30</button>
<button type="button" class="btn btn-warning">60</button>
</div>
</div>
<div class="row justify-content-center mt-5">
<input type="file" name="imagen" id="imagen" class="input-file">
<label for="imagen" class="btn btn-lg btn-outline-black js-labelFile">
<i class="icon fa fa-check"></i>
<span class="js-fileName">Elija la imagen a subir</span>
</label>
<input type="submit" class="btn btn-lg btn-red" value="Subir Imagen">
</div>
</div>
</form>
Fortunately, the form is functional, it sends data through a post request to the data.php file, which is as follows:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Imagen</title>
</head>
<body onload="timer()" oncontextmenu="return false" ondragstart="return false" onselectstart="return false" onkeydown="return false">
<?php
// Informacion de la imagen
$nombre = $_FILES['imagen']['name'];
$tipo = $_FILES['imagen']['type'];
$tamano = $_FILES['imagen']['size'];
//Crea la carpeta imagenes
$carpetaImagenes = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'imagenes-momentaneas/imagenes';
if (!is_dir($carpetaImagenes)) {
mkdir($carpetaImagenes, 0775);
}
if (!is_writable($carpetaImagenes)) {
chmod($carpetaImagenes, 0775);
}
//Crea la carpeta Uploads
$carpetaUploads = $carpetaImagenes.DIRECTORY_SEPARATOR.'uploads';
if (!is_dir($carpetaUploads)) {
mkdir($carpetaUploads, 0775);
}
if (!is_writable($carpetaUploads)) {
chmod($carpetaUploads, 0775);
}
if ($_FILES['imagen']['size'] != 0 && $_FILES['imagen']['type'] === 'image/jpeg' || $_FILES['imagen']['type'] === 'image/jpg' || $_FILES['imagen']['type'] === 'image/png' || $_FILES['imagen']['type'] === 'image/gif')
{
// Envia la imagen del directorio temporar al servidor
move_uploaded_file($_FILES['imagen']['tmp_name'], $carpetaUploads.DIRECTORY_SEPARATOR.$nombre);
}else {
}
// Path de donde va a estar la imagen ya subida
$path = $carpetaUploads.DIRECTORY_SEPARATOR.$nombre;
//Muestra la imagen en base 64
echo "<img src='data:image/jpg;base64,".base64_encode(file_get_contents($path))."' />";
?>
</body>
</html>
which is responsible for uploading the image and displaying it, but I want the image to be displayed in the main index.php file. How do I do this? Because when I put the data.php code in the index.php I get this error:
Since the image is not uploaded as is logical, so I want that code to appear when the image is uploaded, so that I can show it right there in the index, rather what I want is for the image to be uploaded, and in the index shows the url so that the user can copy it and send it to another person so that they can see the image. In addition, I want to generate a new random name, so that when another user uploads an image with the same name it is not overwritten, I saw in a forum that the rand function can be used , but I notice that it is only to generate random numbers, and I I want to generate random names, so if someone can give me a guide for this I would appreciate it.
1
wrap the code that processes the file in an if/else that checks if there is a file uploaded
taken/adapted from: https://stackoverflow.com/a/17492283/1423096
2 to add a random number to the name, modify the line where the name is specified:
you change it for
If you get a warning that pathinfo is disabled, use the alternative:
keep in mind that rand() by itself can generate many collisions (many files with the same name will be overwritten) to avoid this you can use a hash that combines the original name with the rand
that way you will have less chance of the file being overwritten