Jump to content
GarryOne

Ajutor PHP

Recommended Posts

Posted

upload_file.php

<?php

include('utilities.php');

$folder="data/images/";
$foto=$folder.'file.txt';
$tmp=$folder.'tmp.txt';


if ($_FILES['foto']['tmp_name'])
{
move_uploaded_file($_FILES['foto']['tmp_name'], $tmp);
$ext=pathinfo($tmp, PATHINFO_EXTENSION);
$need='txt';

if ($ext == $need)
{
if (file_exists($foto))
{
unlink($foto);
}
copy($tmp,$foto);
redir('page.php?rs=ok&pag=home');

}
else
{
$_SESSION['error']='<p>Eroare. Incercati din nou.</p>';
unlink($foto);
redir('page.php?pag=home');
}
}
else
{
$_SESSION['error']='<p>Uplodati fisierul</p>';
redir('page.php?pag=home');
}
?>

tin sa mentionez ca redirect e o functie creata de mine in fisieru utilities care e inclus la inceputul paginii, asa ca sa nu apara suspiciuni

si home.php

<table cellspacing="0" cellpadding="0" border="0"  width="940">
<tr>
<td align="center">
<?php
if(isset($_GET['rs']))
{
$result=$_GET['rs'];
if($result=='ok')
{
$result=$_GET['rs'];
INCLUDE('data/images/file.txt');
}
}
else
{
if(!empty($_SESSION['error']))
{
echo $_SESSION['error'];
$_SESSION['error']="";
}
echo '<form action="upload_file.php" method="post" enctype="multipart/form-data" name="upload" >
<input type="file" name="foto" />
<br />
<input type="submit" value="Uploadeaza" />
</form>';
}

?>
</td>
</tr>
</table>

Cand uploadez un fisier cu extensia .txt imi incarca fisieru cu succes si mi-l vizualizeaza, bun, pana aici totul e bine, insa cand incerc sa uploadez un fisier de exemplu cu extensia .jpg , nu imi apare mesajul de eroare pe care l-am indicat la else, ci imi da un mesaj de eroare de la server, care o fi problema de nu-mi apare mesajul de eroare?

Posted

Pentru ca in script-ul tau extensia e tot timpul txt

Ii dai move in data/images/tmp.txt

move_uploaded_file($_FILES['foto']['tmp_name'], $tmp);

E txt mereu

$ext=pathinfo('data/images/tmp.txt', PATHINFO_EXTENSION);
Posted

Da rattrap are dreptate , dupa ce schimbi acolo porcaria care ai facut-o , fa si un checker sa nu bagi fisiere php , phtml , tot felu :) ,


<?php
$exceptions = array('php' , 'phtml');
$files_checker = $_POST['files'];
$ok = true;
foreach($files_cheker as $file){
$tokens = explode(" " , $file['name']);
$number = count($tokens) - 1; // exista si positia 0;
if(in_array($exceptions , $tokens[$number])){
$ok = false;
}
}
if($ok == false){
// nu face upload
$error = 'nu se accepta : '.implode(',' , $exceptions);
}else{
// fa upload
}
?>

Cu placere :)

Posted

@robert1995, checkerul il am

@rattrap, am incercat ceva din imaginatie insa nu merge

asta:

$tmp=$folder.'tmp.txt';

am schimbat in asta

$uploaded=$_FILES['foto']['tmp_name'];
$tmp=$folder.'$uploaded';

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.   Paste as plain text instead

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