Jump to content
php.lv forumi

Elementārs uploads


ziedinjsh

Recommended Posts

Sveiki!

Ir tāda lieta, ka nesaprotu ka pēc nevar augšupielādēt failu.

 

if(isset($_POST['addSong'])){

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

//This is size condition 
if ($mp3_size > 167772160) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is limit file type condition 
if ($mp3_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 
//File type alowed 
if (!($mp3_type=="audio/mpeg, audio/x-mpeg, audio/mp3, audio/x-mp3, audio/mpeg3, audio/x-mpeg3, audio/mpg, audio/x-mpg, audio/x-mpegaudio")) {
echo "You may only upload MP3 files.<br>";
$ok=0;
} 
//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['mp3']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['mp3file']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
}

}



echo "<form method='POST' action='".$_SERVER['PHP_SELF']."'>";
echo "Song title <input type='text' name='title' class=''>";
echo "Type <select name='type'>";
	echo "<option value='1'>Standart</option>";
	echo "<option value='2'>Experimental</option>";
echo "</select>";
echo "Song file <input type='file' name='mp3' class=''>";
echo "<input type='submit' name='addSong' value='Add song' class=''>";
echo "</form>";

 

Kad augšupielādēju fails nav folderā un rādās sekojoš teksts:You may only upload MP3 files.

Sorry your file was not uploaded.

 

Kapēc viņš neatpazīst mp3 failu? bez file type atpazīšanas rādās šāds teksts:Sorry, there was a problem uploading your file. php.ini nomainīju augšupielādes izmēra limitu uz 200mb, nesaprotu kapēc neiet

 

Paldies jau iepriekš!

Link to comment
Share on other sites

Debugot vajag. Izdrukā $_FILES masīva saturu (kam tad tāds print_r() domāts??), paskaties vai vispār jebkas tiek uploadots.

 

Formai trūkst atribūta enctype="multipart/form-data"

Edited by briedis
Link to comment
Share on other sites

Kas tas par joku -

if (!($mp3_type=="audio/mpeg, audio/x-mpeg, audio/mp3, audio/x-mp3, audio/mpeg3, audio/x-mpeg3, audio/mpg, audio/x-mpg, audio/x-mpegaudio")) {

?

 

Un kur vispār tiek uzsetots $mp3_type ?

Link to comment
Share on other sites

šo joku

if (!($mp3_type=="audio/mpeg, audio/x-mpeg, audio/mp3, audio/x-mp3, audio/mpeg3, audio/x-mpeg3, audio/mpg, audio/x-mpg, audio/x-mpegaudio"))

aizvācu.

 

Briedis enctype="multipart/form-data" neko nemainīja

 

Array ( [title] => [type] => 1 [mp3] => D7 - Cave.mp3 [addSong] => Add song )

Link to comment
Share on other sites

Nu ok, ja jau tas neko nemainīja tad vāc viņu nost, paskatīšos kā tad augšuplādēsi kaut vienu failu ;)

 

Problēma ir tajā, ka tev nav ne mazākās nojausmas, kas tur notiek tajā kodā, jo neesi rakstījis pats nevienu rindiņu...

Link to comment
Share on other sites

Ne visi ir spējīgi veikt "lielu lēcienus". Labākais, ka vismaz mēģini, ir to darīt pa mazām daļām un jā... būtu tā kā jāsaprot kas tur notiek, kāpēc notiek, kam, vispār, ir jānotiek...

 

1) Izveidojam HTML formu, kas var sūtīt failus (briedis jau minēja),

2) Pārbaudām, vai forma aiziet (tiek "submitted"),

3) No masīva $_FILES paņemam failu un saglabājam to kāda servera mapē,

4) Pieliekam validāciju (tur faila izmērs, paplašinājums, viss ko sirds kāro);

 

P.S. Man lielais lēciens bija no PHP uz Java. Tagad taisu "Space Invaders" spēli.

Link to comment
Share on other sites

if(isset($_POST['addSong']))
{

$target = "./mp3/"; 
$target = $target . basename($_FILES['mp3']['name']); 
$ok= false; 
if ($_FILES['mp3']['size'] > 167772160) 
{ 
$ok = "Your file is too large.<br>"; 
} 


if(!$ok)
{
	if(!move_uploaded_file($_FILES['mp3']['tmp_name'], $target)) 
	{ 
		$ok .= "Sorry, there was a problem uploading your file.<br/>"; 
	}
}

if(!$ok)
{
	echo "The file ". basename( $_FILES['mp3']['name']). " has been uploaded!<br/>";
}
else
{
	echo $ok;

}
}
echo "<form method='POST' action=''enctype='multipart/form-data'>";
echo "Song title <input type='text' name='title' class=''>";
echo "Type <select name='type'>";
echo "<option value='1'>Standart</option>";
echo "<option value='2'>Experimental</option>";
echo "</select>";
echo "Song file <input type='file' name='mp3' class=''>";
echo "<input type='submit' name='addSong' value='Add song' class=''>";
echo "</form>";

extension pats parbaudi.

kam title domāts? :)

Link to comment
Share on other sites

nu tā man ir sanācis šitā:

if(isset($_POST['addmp3'])){
$folder = "mp3/";

$title = $_POST['title'];
$type = $_POST['type'];

$song = $_FILES['mp3']['name'];
$size = $_FILES['mp3']['size'];
$tmp = $_FILES['mp3']['tmp_name'];
$date = date('y.m.d');

$mp3 = $folder.basename($_FILES['mp3']['name']); 


 if($type == "audio/mp3" || $size >167772160) {
 die("this format is not allowed or file is to big");
 }
 else
 {

$query = mysql_query("INSERT INTO songs (song, size, title, type, date) VALUES ('$song', '$size', '$title', '$type', '$date') ");
 move_uploaded_file($_FILES['mp3']['tmp_name'], $mp3);
  echo "mp3 uploaded!";
  }

}


echo "<form method='post' action='".$_SERVER['PHP_SELF']."' enctype='multipart/form-data'>";
echo "<input type='text' name='title'>";
echo "<select name='type'><option value='1'>Normal</option><option value='2'>Experimental</option></select>";
echo "<input type='file' name='mp3'>";
echo "<input type='submit' name='addmp3' value='Add'>";
echo "</form>";

 

Bet protams mp3 fails netiek augšupielādēts.. un datubāzē size arī rāda 0 es nevaru iebraukt kas pa vainu

 

P.S. mp3 failu nevar augšupielādēt, bet bilžu faili augšupielādējas.. kapēc tā?

Edited by ziedinjsh
Link to comment
Share on other sites

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.

Loading...
×
×
  • Create New...