Upload multiple files using php

descarga.jpgOK,  so i am developing this new site and my client tells me that she needs to upload some photos, like eight photos in every case or item of the system, i want keep this post short so lets go right into the code:


[code type=HTML]
<form name=”myform” id=”myform” action=”creala.php” method=”post” enctype=”multipart/form-data”>
<p>
Nombre de la propiedad:<br>
<input type=”text” name=”np” size=”30″ Value=”” ><br>
</p>
<p>
Precio:<br>
<input type=”text” name=”precio” size=”30″ Value=”” ><br>
</p>
<input type=”file” value=”” name=”upload[]” multiple>
<input type=”submit” value=” Crear Propiedad “>
</form>
</p>
[/code]

Ok, with this code we can upload multiple files in a single hit, we use Javascript to handle the file selection, now i give you the PHP code that makes the things work, like always if you can improve this code tell me so others can benefit from that too:

[code type=PHP]

<?php
$error_message[0] = “Unknown problem with upload.”;
$error_message[1] = “Uploaded file too large (load_max_filesize).”;
$error_message[2] = “Uploaded file too large (MAX_FILE_SIZE).”;
$error_message[3] = “File was only partially uploaded.”;
$error_message[4] = “Choose a file to upload.”;
$upload_dir = ‘../fotos/’;
$num_files = count($_FILES[‘upload’][‘name’]);
$array1=array();
for ($i=0; $i < $num_files; $i++) {
$upload_file = $upload_dir . urlencode(basename($_FILES[‘upload’][‘name’][$i]));

if (!preg_match(“/(gif|jpg|jpeg|png)$/”,$_FILES[‘upload’][‘name’][$i])) {
print “I asked for an image…”;
} else {
if (@is_uploaded_file($_FILES[‘upload’][‘tmp_name’][$i])) {
if (@move_uploaded_file($_FILES[‘upload’][‘tmp_name’][$i],
$upload_file)) {

/* Great success… */
array_push($array1, $upload_file);

//$content = file_get_contents($upload_file);
//print $content;
} else {
print $error_message[$_FILES[‘upload’][‘error’][$i]];
}
} else {
print $error_message[$_FILES[‘upload’][‘error’][$i]];
}
}
}

$foto1=$array1[0];
$foto2=$array1[1];
$foto3=$array1[2];
$foto4=$array1[3];
$foto5=$array1[4];
$foto6=$array1[5];
$foto7=$array1[6];
$foto8=$array1[7];

$dbh = pg_connect(“host=’127.0.0.1′ port=’5432′ dbname=’inmsweet_sistema’ user=’inmsweet_detective’ password=’go-dalit123′”);

if (!$dbh) {
echo “ERROR AL CONECTARn”;
}
$sql = “INSERT INTO propiedades ( foto1, foto2, foto3, foto4, foto5, foto6, foto7, foto8) VALUES ( ‘$foto1′,’$foto2′,’$foto3′,’$foto4′,’$foto5′,’$foto6′,’$foto7′,’$foto8′)”;
pg_query($dbh, $sql);
pg_close($dbh);
echo “<p>EXITOSO</p>”;
echo “<p><a href=’javascript:history.back(1)’>Crear otro caso</a></p>”;
echo “</br>”;
print “<p>La propiedad fue creada!</p>”;

?>
[/code]

Basically what we do here is take the string of file paths and convert them into an array, and then we can treat those variables as we see fit, in this case we use the variables to put the file paths into a Postgresql database (you must modify the SQL statement to fit your requirements), so we can grab the images when we need them in the client side of the system. Hope you find it useful. See you later.

Leave a Comment