De PDO a mysqli
Publicado por Pedro (8 intervenciones) el 26/12/2017 21:04:54
Hola por fin he conseguido que me vaya file input por finnnn la cuestión es que utiliza una conexion de pdo y me gustaría utilizar una conexión my_sqli. ¿Cómo lo cambiaría?
Los ficheros son:
index.php
upload.php
borrar.php
View,php
La otra pregunta es: El archivo upload me guarda el archivo en blob y yo lo que quiero es que me lo guarde en la ruta $rutaArchivo=$carpetaAdjunta.$nombreArchivo; Paara no ocupar espacio en la bdd.
A ver si me podéis ayudar.
Los ficheros son:
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Bootstrap File Upload</title>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
<link href="css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="js/fileinput.min.js" type="text/javascript"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<input id="archivos" name="imagenes[]" type="file" multiple=true class="file-loading">
</body>
<?php
$directory = "imagenes_/";
$images = glob($directory . "*.*");
?>
<script>
$("#archivos").fileinput({
uploadUrl: "upload.php",
uploadAsync: false,
minFileCount: 1,
maxFileCount: 20,
showUpload: false,
showRemove: false,
initialPreview: [
<?php
$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
$statement=$pdo->prepare("SELECT id,info FROM archivos");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $result){?>
"<img src='view.php?elid=<?php echo $result['id']; ?>' height='120px' class='file-preview-image'>",
<?php } ?>],
initialPreviewConfig: [<?php foreach($results as $result){ $infoImagenes=$result['info'];?>
{caption: "<?php echo $infoImagenes;?>", height: "120px", url: "borrar.php", key:"<?php echo $result['id'];?>"},
<?php } ?>]
}).on("filebatchselected", function(event, files) {
$("#archivos").fileinput("upload");
});
</script>
</html>
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$carpetaAdjunta="imagenes_/";
// Contar envían por el plugin
$Imagenes = count($_FILES['imagenes']['name']);
for($i = 0; $i < $Imagenes; $i++) {
// El nombre y nombre temporal del archivo que vamos para adjuntar
$nombreArchivo=$_FILES['imagenes']['name'][$i];
$nombreTemporal=$_FILES['imagenes']['tmp_name'][$i];
$rutaArchivo=$carpetaAdjunta.$nombreArchivo;
$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
$statement = $pdo->prepare("INSERT INTO archivos(id,info,imagen) VALUES(NULL,:info,:texto);");
$statement->execute(array("info" => $_FILES['imagenes']['name'][$i],"texto" => file_get_contents($_FILES['imagenes']['tmp_name'][$i])));
move_uploaded_file($nombreTemporal,$rutaArchivo);
$infoImagenesSubidas[$i]=array("caption"=>"$nombreArchivo","height"=>"120px","url"=>"borrar.php");
$ImagenesSubidas[$i]="<img height='120px' src='$rutaArchivo' class='file-preview-image'>";
}
$arr = array("file_id"=>0,"overwriteInitial"=>true,"initialPreviewConfig"=>$infoImagenesSubidas,
"initialPreview"=>$ImagenesSubidas);
echo json_encode($arr);print_r($ID);
?>
borrar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$carpetaAdjunta="imagenes_/";
if($_SERVER['REQUEST_METHOD']=="DELETE"){
parse_str(file_get_contents("php://input"),$datosDELETE);
$key= $datosDELETE['key'];
$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
$statement = $pdo->prepare("DELETE FROM archivos WHERE id=:id");
$statement->execute(array("id" => $key));
//unlink($carpetaAdjunta.$key);
echo 0;
}
?>
View,php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
header('Content-Type: image/jpg');
if(isset($_GET['elid'])){
$pdo=new PDO("mysql:dbname=basededatos;host=127.0.0.1","root","123");
$statement=$pdo->prepare("SELECT * FROM archivos WHERE id=:id");
$statement->execute(array("id" => $_GET['elid']));
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
print_r($results[0]['imagen']);
}
?>
La otra pregunta es: El archivo upload me guarda el archivo en blob y yo lo que quiero es que me lo guarde en la ruta $rutaArchivo=$carpetaAdjunta.$nombreArchivo; Paara no ocupar espacio en la bdd.
A ver si me podéis ayudar.
Valora esta pregunta


0