Rolo Tomassi
Rolo Tomassi

Reputation: 27

Upload two/more images with PHP into MySQL

I'm having problems to resolve this issue, basically I have one table with to fields for images (image and image2), I'm reusing a code and is working properly for upload un image in the submission, but the second one is not uploaded and off course not inserted into the DB.

<form action="../imgs/update_code.php" method="post" enctype="multipart/form-data" id="form1">

<input name="image1" type="file" />
<input name="image2" type="file" />

<input name="id" type="hidden" value="<?php echo $row_Recordset1['id']; ?>" />
<input name="submit" type="submit" value="submit" />
</form>

Can anyone tell me if this is possible to upload to different image in one submit or I need to continue uploading one image per form (first form upload/insert "image1" and second form update/upload "image2")

<?php require_once('../admin/Connections/cnx.php'); ?>
<?php
ob_start();
$tabla='models';
$destino='../models.php';
mysql_connect($server,$user,$pass);
mysql_select_db($db);
function modificar($tabla,$id){
$strupdate='';
foreach($_POST as $k => $v){
if($k!='imageField_x' && $k!='imageField_y' && $k!='image1' && $k!='image2' &&          $k!='foto2' && $k!='foto3' && $k!='Submit'){
$v=(get_magic_quotes_gpc()) ? $v : addslashes($v);
$strupdate.= "$k='$v',";
}}
$strupdate=substr($strupdate,0,(strlen($strupdate)-1));
mysql_query("SET NAMES utf8");
mysql_query("update $tabla set $strupdate where id='$id'");
}
function reemplazaarchivo($archivo,$archivotemp,$tabla,$campoarchivo,$error,$id){
if($archivo!=''){
$qryant=mysql_query("select * from $tabla where id='$id'");
$rowant=mysql_fetch_array($qryant);
@unlink($rowant[$campoarchivo]);
$extension200=end(explode(".",strtolower($archivo)));
if($extension200!='jpg' && $extension200!='gif' && $extension200!='png' &&  $extension200!='doc' && $extension200!='zip' && $extension200!='pdf' && $extension200!='xls' && $extension200!='ppt' && $extension200!='swf'){
eval($error);exit;}
$foto2=md5(time()).$archivo;
copy($archivotemp,$foto2);
@chmod($foto2,0755);
mysql_query("update $tabla set $campoarchivo='$foto2' where id='$id'");
}
}
modificar($tabla,$_POST['id']);
reemplazaarchivo($_FILES['image1']['name'],$_FILES['image1']['tmp_name'],$tabla,'image1','',$_POST['id']);
for($i=1;$i<4;$i++){
reemplazaarchivo($_FILES['image1'.$i]['name'],$_FILES['image1'.$i]  ['tmp_name'],$tabla,'image1'.$i,'',$_POST['id']);
}
header("Location:$destino");
ob_end_flush();
?>

Upvotes: 1

Views: 888

Answers (1)

Matheno
Matheno

Reputation: 4172

Do you even google? http://php.net/manual/en/features.file-upload.multiple.php

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

Also, you're code is very unclear and unsafe.. Take a look into security.

Upvotes: 2

Related Questions