lingo5 Posted July 30, 2012 Share Posted July 30, 2012 Hi, I have this sript to upload one image and save the path to my DB: $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $file['name']; $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath= '../uploads/img/none.jpg'; } else{ $filePath = $uploadDir . $fileName; } $filePath = $uploadDir . $randName . '.' . $ext; $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath_2 = addslashes($filePath_2); } $fileinsert[]=$filePath_2; } $documento_titulo_esp = $_POST["documento_titulo_esp"]; $documento_titulo_cat=$_POST["documento_titulo_cat"]; $query = "INSERT INTO t_documentos (documento_titulo_esp, documento_titulo_cat,documento_path_esp) ". "VALUES ('$documento_titulo_esp','$documento_titulo_cat','$filePath_1')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); header("Location: PC_documentos_display.php?documentoinserted=true"); } But I need to upload 2 imags, one for "documento_path_esp" (which is working fine now and another for "documento_path_cat". How can I do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/ Share on other sites More sharing options...
Barand Posted July 30, 2012 Share Posted July 30, 2012 http://uk.php.net/manual/en/features.file-upload.multiple.php Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365377 Share on other sites More sharing options...
lingo5 Posted July 30, 2012 Author Share Posted July 30, 2012 Thanks Barand, I know how to upload an array of files, but my problem is I can't figure out how to upload one file for one language and another file for a second language. Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365380 Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 What you really want to do, is to upload two files. Then, you want to handle them, according to what language they should be associated with. I recommend breaking up the task into smaller steps, as small as you can get them, and then start to plan how the program should flow. This will help you see and understand all of the details surrounding a problem, which will make it a lot easier to see the solution as well. Trying to tackle everything at once just clouds the solution in the (apparent) complexity of the question. As a little tip: Giving the two files a named index, pertaining to what language they belong to, should help ease things a bit. Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365392 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 Hi guys, I've been trying to fiure out how to solve my problem and have tried this, but nothing goes into my DB.....I am now quite confused....please guide me through this: $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $_FILES['documento_path_esp']['name']; $fileName2 = $_FILES['documento_path_cat']['name']; $tmpName = $_FILES['documento_path_esp']['tmp_name']; $tmpName2 = $_FILES['documento_path_cat']['tmp_name']; $fileSize = $_FILES['documento_path_esp']['size']; $fileSize2 = $_FILES['documento_path_cat']['size']; $fileType = $_FILES['documento_path_esp']['type']; $fileType2 = $_FILES['documento_path_cat']['type']; if($fileName=="") if($fileName2=="") { $filePath_1 = '../uploads/img/none.jpg'; $filePath_2 = '../uploads/img/none.jpg'; } else{ $filePath_1 = $uploadDir . $fileName; $filePath_2 = $uploadDir . $fileName2; } $filePath_1 = $uploadDir . $randName . '.' . $ext; $filePath_2 = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath_1 = str_replace(" ", "_", $filePath_1); $filePath_2 = str_replace(" ", "_", $filePath_2); $result = move_uploaded_file($tmpName, $filePath_1); $result2 = move_uploaded_file($tmpName, $filePath_2); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath_1 = addslashes($filePath_1); $fileName2 = addslashes($fileName2); $filePath_2 = addslashes($filePath_2); } $fileinsert[]=$filePath_1; $fileinsert[]=$filePath_2; } $documento_titulo_esp = $_POST["documento_titulo_esp"]; $documento_titulo_cat=$_POST["documento_titulo_cat"]; $query = "INSERT INTO t_documentos (documento_titulo_esp, documento_titulo_cat,documento_path_esp,documento_path_cat) ". "VALUES ('$documento_titulo_esp','$documento_titulo_cat','$filePath_1','$filePath_2')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); header("Location: PC_documentos_display.php?documentoinserted=true"); } Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365632 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 Could you post a form submission too. Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365636 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 sure jazzman, here it is: <form action="<?php htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data" name="form1" id="form1"> <table width="850" border="0" align="center" cellpadding="8" cellspacing="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="6"> <tr> <td><span class="personalTitulo"> <?=CNT_TXT_ETIQCLOUD_NUEVODOCUMENTO?> </span></td> </tr> <tr> <td class="personalTitulo"> </td> </tr> </table> <table width="100%" border="0" cellpadding="10" cellspacing="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="6"> <tr> <td height="22" bgcolor="#F2F2F2" class="CP_blueTXT"><?=CNT_TXT_VARIOUS_TITULOESP?></td> </tr> <tr> <td><input name="documento_titulo_esp" type="text" class="CP_loginFormFields" id="documento_titulo_esp" value="" size="70" /></td> </tr> </table></td> </tr> </table> <table width="100%" border="0" cellpadding="10" cellspacing="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="6"> <tr> <td height="22" bgcolor="#F2F2F2" class="CP_blueTXT"><?=CNT_TXT_VARIOUS_TITULOCAT?></td> </tr> <tr> <td> <input name="documento_titulo_cat" type="text" class="CP_loginFormFields" id="documento_titulo_cat" value="" size="70" /></td> </tr> </table></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="10"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="6"> <tr> <td colspan="2" bgcolor="#F2F2F2" class="CP_blueTXT"><?=CNT_TXT_FICHACLIENTE_SELECTPDF?> <span class="CP_SiNoText"> <?=CNT_TXT_FICHACLIENTE_PDFSONLY?> </span></td> </tr> <tr> <td width="41%"> <input name="documento_path_esp" type="file" class="CP_loginFormFieldsRED" id="documento_path_esp" /></td> <td width="59%"><input name="documento_path_cat" type="file" class="CP_loginFormFieldsRED" id="documento_path_cat" /></td> </tr> </table></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="6"> <tr> <td width="2%"> </td> <td width="98%"><input name="upload" type="submit" id="upload" value="<?=CNT_TXT_BOTONES_INSERTARDOCUMENTO?>" /> <input type="hidden" name="MM_insert" value="form1" /></td> </tr> </table></td> </tr> </table></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365639 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 Try this: <?php $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { foreach ($_FILES as $file) { $fileName = $_FILES['documento_path_esp']['name']; $fileName2 = $_FILES['documento_path_cat']['name']; $tmpName = $_FILES['documento_path_esp']['tmp_name']; $tmpName2 = $_FILES['documento_path_cat']['tmp_name']; $fileSize = $_FILES['documento_path_esp']['size']; $fileSize2 = $_FILES['documento_path_cat']['size']; $fileType = $_FILES['documento_path_esp']['type']; $fileType2 = $_FILES['documento_path_cat']['type']; if($fileName=="" || $fileName2=="") { $filePath_1 = '../uploads/img/none.jpg'; $filePath_2 = '../uploads/img/none.jpg'; } else{ $filePath_1 = $uploadDir . $fileName; $filePath_2 = $uploadDir . $fileName2; } $filePath_1 = $uploadDir . $randName . '.' . $ext; $filePath_2 = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath_1 = str_replace(" ", "_", $filePath_1); $filePath_2 = str_replace(" ", "_", $filePath_2); $result = move_uploaded_file($tmpName, $filePath_1); $result2 = move_uploaded_file($tmpName, $filePath_2); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath_1 = addslashes($filePath_1); $fileName2 = addslashes($fileName2); $filePath_2 = addslashes($filePath_2); } $fileinsert[]=$filePath_1; $fileinsert[]=$filePath_2; } $documento_titulo_esp = $_POST["documento_titulo_esp"]; $documento_titulo_cat=$_POST["documento_titulo_cat"]; $query = "INSERT INTO t_documentos (documento_titulo_esp, documento_titulo_cat,documento_path_esp,documento_path_cat) ". "VALUES ('$documento_titulo_esp','$documento_titulo_cat','$filePath_1','$filePath_2')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); header("Location: PC_documentos_display.php?documentoinserted=true"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365642 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 Thanks jazzman, none of the file paths get into my DB, instead I get this in he document_path column "../uploads/." :( :( :( Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365648 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 Ok, in this case you need to check the values before mysql_query sends them to DB. For example, add this code immediately before $query = "INSERT INTO t_documentos (documento_titulo_esp, documento_titulo_cat,documento_path_esp,documento_path_cat) ". "VALUES ('$documento_titulo_esp','$documento_titulo_cat','$filePath_1','$filePath_2')"; Give us a result, please. echo '<pre>'.var_dump($filePath_1, $filePath_2).'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365650 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 Add exit in the end, sorry : echo '<pre>'.var_dump($filePath_1, $filePath_2).'</pre>'; exit; Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365652 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 Hi jazzman, here's the result string(12) "../uploads/." string(12) "../uploads/." Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365654 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 What a result, you expect to be ? Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365657 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 string(12) "../uploads/myfile1.pdf" string(12) "../uploads/myfile2.pdf" Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365658 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 string(12) "../uploads/myfile1.pdf" string(12) "../uploads/myfile2.pdf" Hm..... you need learn more of php Put in this code immediately before if($fileName=="" || $fileName2==""), and give us the results: echo '<pre>'.var_dump($fileName,$fileName2,$tmpName,$tmpName2).'</pre>';exit; Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365666 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 Hi jazzman...yes I know I need to learn more and that's what I'm trying to do Here's the result: string(11) "myfile1.pdf" string(11) "myfile1.pdf" string(23) "C:\wamp\tmp\phpF2CF.tmp" string(23) "C:\wamp\tmp\phpF2CF.tmp" where is myfile2.pdf?... Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365668 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 Good for you, I've started to learn php at 37 years old, now I'm 40 Ok, find this code: if($fileName=="" || $fileName2=="") { $filePath_1 = '../uploads/img/none.jpg'; $filePath_2 = '../uploads/img/none.jpg'; } else{ $filePath_1 = $uploadDir . $fileName; $filePath_2 = $uploadDir . $fileName2; } And replace with this. What result did you get ? <?php if($fileName=="" || $fileName2=="") { echo 'False'; exit; $filePath_1 = '../uploads/img/none.jpg'; $filePath_2 = '../uploads/img/none.jpg'; } else{ echo 'True'; exit; $filePath_1 = $uploadDir . $fileName; $filePath_2 = $uploadDir . $fileName2; } Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365669 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 I'm 21 so I hope to have learned a lot by the time I'm 40... ;) ok...the result is true Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365671 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 Ok, everything is fine until here Could you explain me this code $filePath_1 = $uploadDir . $randName . '.' . $ext; I don't see you declare $randName and $ext php varibles, somewhere in your code Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365675 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 sorry jazzman, I forgot to commnent out that piece of code. Still it doesn't change anything, I get the same file path for both files string(11) "myfile1.pdf" string(11) "myfile1.pdf" string(23) "C:\wamp\tmp\phpF2CF.tmp" string(23) "C:\wamp\tmp\phpF2CF.tmp" I need "documento_path_esp = myfile1.pdf" and "documento_path_cat = myfile2.pdf" Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365689 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 You want documento_path_esp to be "documento_path_esp = myfile1.pdf" or "documento_path_esp = ../uploads/myfile1.pdf" ? Get rid of this code, and check what result you get in DB: $filePath_1 = $uploadDir . $randName . '.' . $ext; $filePath_2 = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath_1 = str_replace(" ", "_", $filePath_1); $filePath_2 = str_replace(" ", "_", $filePath_2); $result = move_uploaded_file($tmpName, $filePath_1); $result2 = move_uploaded_file($tmpName, $filePath_2); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath_1 = addslashes($filePath_1); $fileName2 = addslashes($fileName2); $filePath_2 = addslashes($filePath_2); } $fileinsert[]=$filePath_1; $fileinsert[]=$filePath_2; } Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365693 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 You want documento_path_esp to be "documento_path_esp = myfile1.pdf" or "documento_path_esp = ../uploads/myfile1.pdf" ? Get rid of this code, and check what result you get in DB: $filePath_1 = $uploadDir . $randName . '.' . $ext; $filePath_2 = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath_1 = str_replace(" ", "_", $filePath_1); $filePath_2 = str_replace(" ", "_", $filePath_2); $result = move_uploaded_file($tmpName, $filePath_1); $result2 = move_uploaded_file($tmpName, $filePath_2); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath_1 = addslashes($filePath_1); $fileName2 = addslashes($fileName2); $filePath_2 = addslashes($filePath_2); } $fileinsert[]=$filePath_1; $fileinsert[]=$filePath_2; } Sorry yes I want "documento_path_esp = ../uploads/myfile1.pdf" and "documento_path_cat = ../uploads/myfile2.pdf" What I get now after removing the code you suggested is "documento_path_esp = ../uploads/myfile2.pdf" and "documento_path_cat = ../uploads/myfile2.pdf" so filePath_2 is inserted for both languages... Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365696 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 According this result, string(11) "myfile1.pdf" string(11) "myfile1.pdf" string(23) "C:\wamp\tmp\phpF2CF.tmp" string(23) "C:\wamp\tmp\phpF2CF.tmp" you have to images with the same name, but it's no true, right ? Check the name of $fileName2 = $_FILES['documento_path_cat']['name']. There is something wrong Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365704 Share on other sites More sharing options...
lingo5 Posted July 31, 2012 Author Share Posted July 31, 2012 sorry jazzman, with all these changes to the code I had deleted $fileName2 = $_FILES['documento_path_cat']['name']... It works perfect now!!!! Thanks a lot for your help and patience....you've been great help!!! :) :) Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365713 Share on other sites More sharing options...
jazzman1 Posted July 31, 2012 Share Posted July 31, 2012 No problem young lady Instead of your $query use this: $query = sprintf("INSERT INTO `t_documentos` SET `documento_titulo_esp` = '%s',`documento_titulo_cat` = '%s',`documento_path_esp` = '%s',`documento_path_ca` = '%s'", mysql_real_escape_string($documento_titulo_esp), mysql_real_escape_string($documento_titulo_cat), mysql_real_escape_string($filePath_1), mysql_real_escape_string($filePath_2)); Quote Link to comment https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/#findComment-1365717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.