Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/266439-please-help-uploading-2-images/
Share on other sites

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. ;)

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

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>

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"); 
}
?>

 

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>';

 

 

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;

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?... :(

Good for you, I've started to learn php at 37 years old, now I'm 40  :D

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;
}

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"

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;
}

 

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...

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  :-[

 

 

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));

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.