0

Hi all, i need some help with my php code. I want to create upload form with 4 fields : two dropdown buttons, 1 text field and the browse button. I have problem with validating my files ,because i want only to upload pptx, ppt and pdf files , to check the file name , because it can be written in cyrillic and a don't want this. Also if there is the same file in the DB to change the name ( it' can be done with-- $random_digit=rand(0000,9999); $new_file_name=$random_digit.$file_name;-- or something like that) Can anyone help me, my code till now is :

<body>
<?php
if(isset($_POST['upload'])){
$class=$_POST['class'];
$subject=$_POST['subject'];
$lesson=$_POST['lesson'];

 $target = "upload/"; 
 $target = $target . basename( $_FILES['file']['name']) ; 
 $ok=1; 


/*if($class=='0')
{ print ("Изберете клас");}
else{
	if($subject=='0')
	{ print("Избери предмет"); }
	else{
		if(empty($lesson))
		{ print("Въведи име на урока");}
		else{*/
			$ext= pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
			echo $ext."<br />";
		/*	echo $_FILES['file']['size']. " = razmer <br />";
			echo $_FILES['file']['name']. " = ime <br />";
			echo $_FILES['file']['type']. "   = tip  <br />";
			echo $_FILES['file']['tmp_name']. " =  temp <br />"; */
			if($ext!='pptx' || $ext!="pdf" )
			{$error= "Файлът е с грешно разширение.";
				echo $error;}
			else{
				if ($_FILES['file']['size'] > 10500000) 
				{ 
				echo "Файлът е прекалено голям.<br>"; 
				$ok=0; 
				} 
				if ($ok==0) 
				{ 
				Echo "Файлът не беше качен!"; 
				} 
				else{ 
					if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
					{  
						echo "Фаилът ". basename( $_FILES['file']['name']). " е прикачен успешно.<br /> <br />";
					} 
					else { 
					echo "Възникнал е проблем с прикачването. Опитайте отново."; 
						} 
					}
				}
			}
/*			
		}
	}
}
*/
?>

<div style="width:700px; height:500px;">
<div class="asking" id="upload" >
Прикачи урок<br /><br /><br />
<form name="uploadf" method="post" action="" enctype="multipart/form-data">
<table  width="500px" cellpadding="0" cellspacing="0" border="1" id="form">
	<tr>
		<td ><span style="margin-right:10px;">Клас:</span><select name="class" type="list" size="1" id="class" style=" width:100px; font-size: 13px;">
			<option value='0' selected='yes' ></option><option value='I клас' >I клас</option><option value='II клас' >II клас</option><option value='III клас' >III клас</option><option value='IV клас' >IV клас</option>
			</select> 
			<span style="margin-left:40px; margin-right:10px;">Предмет: <select name="subject" type="list" size="1" id="class" style=" width:100px; font-size: 13px;">
			<option value='0' selected='yes' ></option><option value='Български език' >Български език</option><option value='Математика' >Математика</option><option value='Роден край'>Роден край</option><option value='Домашен бит и техника'>Домашен бит и техника</option><option value='Околен свят'>Околен свят</option><option value='Човек и общество'>Човек и общество</option><option value='Човек и природа'>Човек и природа</option>
			</select>
		</td>
  </tr>
  <tr>
		<td ><span style="margin-right:10px;">Име на урок:</span> <input  type="text" name="lesson"  style="font-size: 13px;">
  </tr>
  <tr>
		<td ><span style="margin-right:10px;">Избери файл:</span><input type="file" name="file"  style="width:230px; height:25px; padding:3px; position:relative;"  /></td>
  </tr>
  <tr>
		<td align="right"><input type="submit" name="upload" value="Прикачи"  class="button" id="submit"/></td>
  </tr>
</table>
</form>
</div>

</div>
</body>

thanks in advance :)

3
Contributors
7
Replies
9
Views
5 Years
Discussion Span
Last Post by ddymacek
0

Ok, this looks like your basic php:

if(isset($_POST['upload'])){
	$class=$_POST['class'];
	$subject=$_POST['subject'];
	$lesson=$_POST['lesson'];
 
	$target = "upload/"; 
	$target = $target . basename( $_FILES['file']['name']) ; 
	$ext= pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
	echo $ext."<br />";
	if($ext!='pptx' || $ext!="pdf" ){
		echo "Файлът е с грешно разширение.";
	}else{
		if ($_FILES['file']['size'] > 10500000){ 
			echo "Файлът е прекалено голям.<br>"; 
			echo "Файлът не беше качен!"; 
		}else{ 
			if(move_uploaded_file($_FILES['file']['tmp_name'], $target)){  
				echo "Фаилът ". basename($_FILES['file']['name']). " е прикачен успешно.<br /> <br />";
			}else{ 
				echo "Възникнал е проблем с прикачването. Опитайте отново."; 
			} 
		}
	}
}

I can't see anything obvious here...

So you want somebody to give you the form code to go with this??
And you want to check filename for cyrillic characters?
And you want to rename a file if it already exists?
And you want some DB functionality?

Anything else??

Edited by diafol: n/a

0

1. You can detect Cyrillic script with this:

function isCyr($text) {
    return preg_match('/[А-Яа-яЁё]/u', $text);
}
if(isCyr($filename)){
  //rename file is true
}

That snippet came from http://stackoverflow.com/questions/3212266/detecting-russian-characters-on-a-form-in-php

2. You can check an existing filename like this:

if(file_exists($filename){
  //rename the file
}

3. Write the filename to DB:

$r = mysql_query("INSERT INTO table SET fname = '$filename'");

That's probably enough to get you going.

0

first i want somebody to help me with the code from line 23 to line 31 from my code where is the validation for the file type, because when i write only

if ($ext!="pptx"){ echo "invalid file type";}

it works and upload only pptx file, but when i write the "if" which is in the code i gave at first place with more $ext and i cant upload anything .

0

use and instead of or on your if comparison line 29.

if($ext!='pptx' && $ext!="pdf" ){
     // error message
} else {
     // check file size code...
}

tested with extensions of pptx, pdf and doc.
pptx and pdf makes it to check file size code, doc fails validation.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.