• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

psiico

dados de array $_POST nao passam correctamente.

3 mensagens neste tópico

Boas pessoal, este codigo q vou postar é um form com 5 "copias", num array, para fazer upload de um ficheiro, eu utilizo um while para buscar os dados de cada posição do array que é passado por POST,mas ele nao apresenta os dados.

O codigo é o seguinte (isto é de uma parte de administração de um componente no Joomla!:

<table width="500" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><p><a href="?option=com_downloads">[back]</a> </p>
          <h1>Add Download</h1>
          <hr style="width:40%" align="left" />
          
            <?php
          if ($_POST['addnew_button']) {
	  $db = & JFactory::getDBO();
	  //PARSE USER INFO
	  $query = "SELECT username from #__session where session_id='" . session_id() . "' limit 1";
$db->setQuery($query);
$result = $db->loadRowList();
foreach ($result as $users) {
$data->uploader = $users[0];
}
$upload_errors = array(
			'1' => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
			'2' => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
			'3' => 'The uploaded file was only partially uploaded.',
			'4' => 'No file was uploaded.',
			'6' => 'Missing a temporary folder.',
			'7' => 'Failed to write file to disk.',
			'8' => 'File upload stopped by extension.'
		);
	  #HANDLE FILES
	  
	  $count = 0;
	  while ($count < sizeof($_POST['addnew_title'])) {
		  echo 'COUNT: ' . $count . '<br>' . sizeof($_POST['addnew_title']) . '<br>';
		  if ($_FILES['addnew_file']['error'][$count] != '0') {
	  		if ($_FILES['addnew_file']['error'][$count] != 4) {
		 	 	echo 'Error while uploading the file: ' . $upload_errors[$_FILES['addnew_file']['error'][$count]];
	  		} else {
	 			 $title = $db->getEscaped($_POST['addnew_title'][$count]);
	 			 $description = $db->getEscaped($_POST['addnew_description'][$count]);
				 $category = $db->getEscaped($_POST['addnew_cat'][$count]);
	 			 $published = $db->getEscaped($_POST['addnew_published'][$count]);
				 $size = ($_FILES["addnew_file"]["size"][$count] / 1024);
	 			 $approved = 1;
	  			if ($published == "on") {
	 				 $published = "1";
				} else {
	  				$published = "0";
	  			}
			if ($title != "" && $description != "" && $category != "") {
				$file = '../components/com_downloads/files/' . date("d_m_y_h_m_s") . '_' . $_FILES['addnew_file']['name'][$count];
				$filename = 'components/com_downloads/files/' . date("d_m_y_h_m_s") . '_' . $_FILES['addnew_file']['name'][$count];
				move_uploaded_file($_FILES['addnew_file']["tmp_name"][$count], $file);				


			if ($_FILES['addnew_screen']['error'][$count] != 0 && $_FILES['addnew_screen']['error'][$count] != 4) {
		  		echo 'Error while uploading the image: ' . $upload_errors[$_FILES['addnew_screen']['error'][$count]];
	  		} else {
				$file2 = '../components/com_downloads/imgs/' . date("d_m_y_h_m_s") . '_' . $_FILES['addnew_screen']['name'][$count];
				$screenshot = 'components/com_downloads/imgs/' . date("d_m_y_h_m_s") . '_' . $_FILES['addnew_screen']['name'][$count];
				move_uploaded_file($_FILES['addnew_screen']["tmp_name"][$count], $file2);				
		echo 'VALORES PARA A DB:<br>' . 
			'title: ' . $title . '<br>' . 
			' description ' . $description . '<br>';
			$insert_download = mysql_query("INSERT INTO jos_downloads_list (`title`, `description`, `screenshot`, `filename`, `uploader`, `category`, `published`, `size`) VALUES ('" . $title . "', '" .$description . "', '" .$screenshot. "', '" .$filename. "', '" . $uploader . "', '" .$category. "', '" .$published. "', '" .$size. "')");
				if ($insert_download) {
					echo '<strong>File ' . $count + 1 . ' uploaded sucessfully!</strong><br>';	
				} else {
					echo 'Error: ' . mysql_error() . '<br>';	
				}
			} 
			}
	}
	}
	$count++;
	//END WHILE
	}
	  //END IF POST
	  }
	  ?>
            <br /><?php
		if (is_writable("../components/com_downloads/files/")) {
			echo '<strong>The files directory is: <font color="green">writable</font></strong><br>';
		} else {
			echo '<strong>The files directory is: <font color="red">Unwritable</font></strong><br>';
		}
        
		if (is_writable("../components/com_downloads/imgs/")) {
			echo '<strong>The images directory is: <font color="green">writable</font></strong><br>';
		} else {
			echo '<strong>The images directory is: <font color="red">Unwritable</font></strong><br>';
		} ?>
            <br />
            The file type checking is not activate here.
            
            <form action="" method="post" enctype="multipart/form-data" name="form2" id="form2">
            <input name="addnew_button" type="submit" value="Upload files" id="addnew_button" /><br /><p>File 1</p>
            <table width="400" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="181" align="right"><strong>Title:</strong></td>
              <td width="219"><label>
                <input name="addnew_title[]" type="text" id="addnew_title[]" maxlength="20" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Description:</strong></td>
              <td><?php echo $editor->display( 'addnew_description[]',"" , '100%', '150', '75', '20' ) ;?></td>
            </tr>
            <tr>
              <td align="right"><strong>File:</strong></td>
              <td><label>
                <input type="file" name="addnew_file[]" id="addnew_file[]" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Screenshot:</strong></td>
              <td><input type="file" name="addnew_screen[]" id="addnew_screen[]" /></td>
            </tr>
            <tr>
              <td align="right"><strong>Category:</strong></td>
              <td><select name="addnew_cat[]" id="addnew_cat[]">
              <option value="" selected="selected">Select one...</option>
                <?php 
			  $db = & JFactory::getDBO();
$query = "SELECT name FROM #__downloads_cats order by name";
$db->setQuery( $query );
$result = $db->loadRowList();
foreach ($result as $cats) { 
?>
                <option value="<?php
				echo $cats[0];
				?>">
                <?php 
				echo $cats[0];
				?>
                </option>
                <?php }?>
              </select></td>
            </tr>
            <tr>
              <td align="right"><strong>Published:</strong></td>
              <td><label>
                <input name="addnew_published[]" type="checkbox" id="addnew_published[]" checked="checked" />
              </label></td>
            </tr>
          </table>
        <?php
          $db->setQuery("SELECT * FROM #__downloads_cfg");
	$db->Query();
	$result = $db->LoadRowList();
	foreach ($result as $config) {
		$cfg->max_size = $config[0];
	}
	  ?><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $cfg->max_size; ?>" />
               
            <p>File 2</p>
            <table width="400" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="181" align="right"><strong>Title:</strong></td>
                <td width="219"><label>
                  <input name="addnew_title[]" type="text" id="addnew_title[]" maxlength="20" />
                </label></td>
              </tr>
              <tr>
                <td align="right"><strong>Description:</strong></td>
                <td><?php echo $editor->display( 'addnew_description[]',"" , '100%', '150', '75', '20' ) ;?></td>
              </tr>
              <tr>
                <td align="right"><strong>File:</strong></td>
                <td><label>
                  <input type="file" name="addnew_file[]" id="addnew_file[]2" />
                </label></td>
              </tr>
              <tr>
                <td align="right"><strong>Screenshot:</strong></td>
                <td><input type="file" name="addnew_screen[]" id="addnew_screen[]" /></td>
              </tr>
              <tr>
                <td align="right"><strong>Category:</strong></td>
                <td><select name="addnew_cat[]" id="addnew_cat[]">
                  <option value="" selected="selected">Select one...</option>
                  <?php 
			  $db = & JFactory::getDBO();
$query = "SELECT name FROM #__downloads_cats order by name";
$db->setQuery( $query );
$result = $db->loadRowList();
foreach ($result as $cats) { 
?>
                  <option value="<?php
				echo $cats[0];
				?>">
                    <?php 
				echo $cats[0];
				?>
                  </option>
                  <?php }?>
                </select></td>
              </tr>
              <tr>
                <td align="right"><strong>Published:</strong></td>
                <td><label>
                  <input name="addnew_published[]" type="checkbox" id="addnew_published[]" checked="checked" />
                </label></td>
              </tr>
            </table>
            <?php
          $db->setQuery("SELECT * FROM #__downloads_cfg");
	$db->Query();
	$result = $db->LoadRowList();
	foreach ($result as $config) {
		$cfg->max_size = $config[0];
	}
	  ?>
                  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $cfg->max_size; ?>" />
          <p>File 3</p>
          <table width="400" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="181" align="right"><strong>Title:</strong></td>
              <td width="219"><label>
                <input name="addnew_title[]" type="text" id="addnew_title[]" maxlength="20" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Description:</strong></td>
              <td><?php echo $editor->display( 'addnew_description[]',"" , '100%', '150', '75', '20' ) ;?></td>
            </tr>
            <tr>
              <td align="right"><strong>File:</strong></td>
              <td><label>
                <input type="file" name="addnew_file[]" id="addnew_file[]" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Screenshot:</strong></td>
              <td><input type="file" name="addnew_screen[]" id="addnew_screen[]" /></td>
            </tr>
            <tr>
              <td align="right"><strong>Category:</strong></td>
              <td><select name="addnew_cat[]" id="addnew_cat[]">
                <option value="" selected="selected">Select one...</option>
                <?php 
			  $db = & JFactory::getDBO();
$query = "SELECT name FROM #__downloads_cats order by name";
$db->setQuery( $query );
$result = $db->loadRowList();
foreach ($result as $cats) { 
?>
                <option value="<?php
				echo $cats[0];
				?>">
                  <?php 
				echo $cats[0];
				?>
                </option>
                <?php }?>
              </select></td>
            </tr>
            <tr>
              <td align="right"><strong>Published:</strong></td>
              <td><label>
                <input name="addnew_published[]" type="checkbox" id="addnew_published[]" checked="checked" />
              </label></td>
            </tr>
          </table>
          <?php
          $db->setQuery("SELECT * FROM #__downloads_cfg");
	$db->Query();
	$result = $db->LoadRowList();
	foreach ($result as $config) {
		$cfg->max_size = $config[0];
	}
	  ?>
          <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $cfg->max_size; ?>" />

          <p>File 4</p>
          <table width="400" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="181" align="right"><strong>Title:</strong></td>
              <td width="219"><label>
                <input name="addnew_title[]" type="text" id="addnew_title[]" maxlength="20" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Description:</strong></td>
              <td><?php echo $editor->display( 'addnew_description[]',"" , '100%', '150', '75', '20' ) ;?></td>
            </tr>
            <tr>
              <td align="right"><strong>File:</strong></td>
              <td><label>
                <input type="file" name="addnew_file[]" id="addnew_file[]" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Screenshot:</strong></td>
              <td><input type="file" name="addnew_screen[]" id="addnew_screen[]" /></td>
            </tr>
            <tr>
              <td align="right"><strong>Category:</strong></td>
              <td><select name="addnew_cat[]" id="addnew_cat[]">
                <option value="" selected="selected">Select one...</option>
                <?php 
			  $db = & JFactory::getDBO();
$query = "SELECT name FROM #__downloads_cats order by name";
$db->setQuery( $query );
$result = $db->loadRowList();
foreach ($result as $cats) { 
?>
                <option value="<?php
				echo $cats[0];
				?>">
                  <?php 
				echo $cats[0];
				?>
                </option>
                <?php }?>
              </select></td>
            </tr>
            <tr>
              <td align="right"><strong>Published:</strong></td>
              <td><label>
                <input name="addnew_published[]" type="checkbox" id="addnew_published[]" checked="checked" />
              </label></td>
            </tr>
          </table>
          <?php
          $db->setQuery("SELECT * FROM #__downloads_cfg");
	$db->Query();
	$result = $db->LoadRowList();
	foreach ($result as $config) {
		$cfg->max_size = $config[0];
	}
	  ?>
          <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $cfg->max_size; ?>" />

          <p>File 5</p>
          <table width="400" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="181" align="right"><strong>Title:</strong></td>
              <td width="219"><label>
                <input name="addnew_title[]" type="text" id="addnew_title[]" maxlength="20" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Description:</strong></td>
              <td><?php echo $editor->display( 'addnew_description[]',"" , '100%', '150', '75', '20' ) ;?></td>
            </tr>
            <tr>
              <td align="right"><strong>File:</strong></td>
              <td><label>
                <input type="file" name="addnew_file[]" id="addnew_file[]" />
              </label></td>
            </tr>
            <tr>
              <td align="right"><strong>Screenshot:</strong></td>
              <td><input type="file" name="addnew_screen[]" id="addnew_screen[]" /></td>
            </tr>
            <tr>
              <td align="right"><strong>Category:</strong></td>
              <td><select name="addnew_cat[]" id="addnew_cat[]">
                <option value="" selected="selected">Select one...</option>
                <?php 
			  $db = & JFactory::getDBO();
$query = "SELECT name FROM #__downloads_cats order by name";
$db->setQuery( $query );
$result = $db->loadRowList();
foreach ($result as $cats) { 
?>
                <option value="<?php
				echo $cats[0];
				?>">
                  <?php 
				echo $cats[0];
				?>
                </option>
                <?php }?>
              </select></td>
            </tr>
            <tr>
              <td align="right"><strong>Published:</strong></td>
              <td><label>
                <input name="addnew_published[]" type="checkbox" id="addnew_published[]" checked="checked" />
              </label></td>
            </tr>
          </table>
          <br />
          <input name="addnew_button" type="submit" value="Upload files" id="addnew_button" />
          <?php
          $db->setQuery("SELECT * FROM #__downloads_cfg");
	$db->Query();
	$result = $db->LoadRowList();
	foreach ($result as $config) {
		$cfg->max_size = $config[0];
	}
	  ?>
          <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $cfg->max_size; ?>" />
        </form></td>
      </tr>
    </table>

Eu estou farto de rever o codigo e nao vejo ele com erros :S

Veêm alguma coisa q nao esta' bem?

Obrigado desde já.

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Dá jeito saber o que é que estás à procura, qual é o comportamento errado..

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

ok, desculpem.

O problema é as variaveis: $title, $description, ficam vazias, quando deviam conter dados porque foram inseridos, logo ele insere dados em branco mas copia/uploada bem os ficheiros para o sitio certo e tudo mais.

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!


Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.


Entrar Agora