Subir arquivo ao servidor con PHP

febrero 19, 2021 Por admin 0

Para subir por exemplo un arquivo de imaxe, primeiro engado un contedor Div no body do HTML así:


<div class="Sube_Fotografía" id="Sube_Fotografía">


Seguidamente, e dentro do contedor, engado un formulario con método POST e envío o resultado ao documento «uploader.php«:


<form enctype="multipart/form-data" action="uploader.php" method="POST">

Agora engado dous botóns, un para seleccionar o
arquivo e o outro para envialo ao documento «uploader.php«:

<input name="uploadedfile" type="file"/>

<input type="submit" value="Subir Fotografía"/>

Pecho o formulario:

</form>

E o contedor:


</div>

Isto no body do HTML. Agora queda o documento «uploader.php«, un exemplo sinxelo é este:
<?php
include("conexion.php");
header("Content-Type: text/html;charset=utf-8");

if(!$Ficheiro=basename( $_FILES['uploadedfile']['name'])){
$FicheiroAux="Nulo";
}
else{
$Ficheiro = Renomear($Ficheiro);

$FicheiroAux=$Ficheiro;
}
$target_path = "Fotos/" . $Ficheiro;

move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
function Renomear($string){

$string = str_replace(" ","",$string);
$string = str_replace("'","`",$string);
$string = str_replace(",","
",$string);

$string = str_replace(";","",$string);
$string = str_replace("& ","&",$string);
$string = str_replace(" &","&",$string);
$string = str_replace("&","_and
",$string);
$string = str_replace("Nº ","nùmero ",$string);

$string = str_replace("Nº"," nùmero",$string);
$string = str_replace("[","",$string);
$string = str_replace("]","",$string);

$string = str_replace("%","",$string);
return $string;
}
?>