» »

PHP upload obrazec včasih dela, včasih ne?

PHP upload obrazec včasih dela, včasih ne?

Housy ::

Zdravo :D

Spisat skušam obrazec za nalaganje datotek, ki včasih dela, včasih pa ne. Kako je to sploh možno?

Lahko nekdo prosim pregleda kodo in prosim pove, kaj delam narobe? Delam pa to kr preko Wampa na svojem računalniku.

<?php

if(isset($_POST["upload"])) {

	// VARIABLES
	// default values
	$error_msg = "";
	$error 	   = false;

	// SUPPORTED FILE EXTENSIONS
	// users can upload only MP3 files 
	$allowed_ext = array("audio/mp3");

	if(in_array($_FILES["song"]["type"], $allowed_ext)) {
		$error = false;
	}
	else {
		$error = true;
		$error_msg .= "<p style='color: red'>You can upload only files with .mp3 extension!</p>";
	}
	
	// SUPPORTED FILE SIZE
	// max file upload size is 12 MB
	if($_FILES["song"]["size"] <= 12582912) {
		$error = false;
	}
	else {
		$error = true;
		$error_msg .= "<p style='color: red'>Maximum upload file size is 12 MB!</p>";
	}
	
	if($error) echo $error_msg;

}

?>

Hvala in lp,
Housy

jype ::

Kaj piše v error logu?

Housy ::

V apache error logu so naslednje napake:
[Sat Jul 27 12:42:52.996866 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:50925] PHP Notice: Undefined index: file in C:\\wamp\\www\\sound\\index.php on line 5, referer: http://localhost/sound/index.php
[Sat Jul 27 12:42:52.996866 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:50925] PHP Stack trace:, referer: http://localhost/sound/index.php
[Sat Jul 27 12:42:52.996866 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:50925] PHP 1. {main}() C:\\wamp\\www\\sound\\index.php:0, referer: http://localhost/sound/index.php
[Sat Jul 27 12:45:54.740261 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:50937] PHP Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\\wamp\\www\\sound\\index.php on line 18
[Sat Jul 27 12:51:59.970151 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51024] PHP Warning: POST Content-Length of 44707167 bytes exceeds the limit of 31457280 bytes in Unknown on line 0, referer: http://localhost/sound/index.php
[Sat Jul 27 12:52:05.137446 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51024] PHP Notice: Array to string conversion in C:\\wamp\\www\\sound\\index.php on line 15, referer: http://localhost/sound/index.php
[Sat Jul 27 12:52:05.138446 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51024] PHP Stack trace:, referer: http://localhost/sound/index.php
[Sat Jul 27 12:52:05.138446 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51024] PHP 1. {main}() C:\\wamp\\www\\sound\\index.php:0, referer: http://localhost/sound/index.php
[Sat Jul 27 12:53:22.310860 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51037] PHP Notice: Undefined variable: explode_ext in C:\\wamp\\www\\sound\\index.php on line 17, referer: http://localhost/sound/index.php
[Sat Jul 27 12:53:22.310860 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51037] PHP Stack trace:, referer: http://localhost/sound/index.php
[Sat Jul 27 12:53:22.310860 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51037] PHP 1. {main}() C:\\wamp\\www\\sound\\index.php:0, referer: http://localhost/sound/index.php
[Sat Jul 27 12:56:23.504224 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51054] PHP Warning: POST Content-Length of 489693796 bytes exceeds the limit of 31457280 bytes in Unknown on line 0, referer: http://localhost/sound/index.php

paco ::

V zgornji kodi nimaš funkcije ki naloži datoteko na server.

Prav tako lahko pogojne stavke spremeniš v;
if(!in_array($_FILES["song"]["type"], $allowed_ext)) {
        $error = true;
        $error_msg .= "<p style='color: red'>You can upload only files with .mp3 extension!</p>";
    }


V error logu pa piše, da velikost datoteke presega max velikost. Poglej v php.ini.
It iz aj.

Housy ::

Ta pogojni stavek sem zamenjal, tnx :)

Ne razumem, zakaj skripta spusti skozi druge datoteke in jih vseeno naloži, če sem pa omejil samo na MP3 datoteke?

TRENUTNA KODA
<?php

if(isset($_POST["upload"])) {

	// VARIABLES
	// default values
	$upload_dir = "upload/";
	$error_msg  = "";
	$error 	    = false;

	// SUPPORTED FILE EXTENSIONS
	// users can upload only MP3 and AAC (m4a) files 
	$allowed_ext = array("audio/mp3");

	if(!in_array($_FILES["song"]["type"], $allowed_ext)) {
		$error = true;
		$error_msg .= "<p style='color: red'>You can upload only files with .mp3 and .m4a extensions!</p>";
	}
	
	// SUPPORTED FILE SIZE
	// max file upload size is 12 MB
	if($_FILES["song"]["size"] > 12582912) {
		$error = true;
		$error_msg .= "<p style='color: red'>Maximum upload file size is 12 MB!</p>";
	}
	
	// FILE ALREADY EXIST
	if(file_exists($upload_dir.$_FILES["song"]["name"])) {
		$error = true;
		$error_msg .= "<p style='color: red'>File already exist!</p>";
	}
	
	// UPLOAD FILES
	// if there aren't any errors, user can upload a file
	if($error) echo $error_msg;
	else {
	
		move_uploaded_file($_FILES["song"]["tmp_name"], $upload_dir.$_FILES["song"]["name"]);
		echo "SUCCESS!";
	
	}

}

?>

V php.ini mam pa nastavljen "post_max_size" in "upload_max_filesize" na 30M, medtem ko v sami skripti dovoljujem samo upload datotek z maximalno velikostjo 12M.

Še vedno mi ni jasno, kaj delam narobe. Please help me :'(

Hvala in lp,
Housy

Zgodovina sprememb…

  • spremenil: Housy ()

usoban ::

[Sat Jul 27 12:52:05.137446 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51024] PHP Notice: Array to string conversion in C:\\wamp\\www\\sound\\index.php on line 15, referer: http://localhost/sound/index.php

Zdaj v 15. vrstici (in_array) so argumenti pravilno podani, tako da zna preveri z
var_dump($_FILES)
da nimas slucajno arraya, ker potlej moras do informacij o datoteki dostopat se z indeksom
$_FILES['song']['type'][0]

Zgodovina sprememb…

  • spremenil: usoban ()

Housy ::

Če naložim npr. Word datoteko z .docx končnico in izvedem funkcijo var_dump, dobim ven naslednje podatke:
array (size=1)
'song' =>
array (size=5)
'name' => string 'BLABLA.docx' (length=11)
'type' => string 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' (length=71)
'tmp_name' => string 'C:\wamp\tmp\phpA9DE.tmp' (length=23)
'error' => int 0
'size' => int 13947

Kar se pa tiče arraya[0], pa ne pride v poštev, saj imam input značko takole naštimano:
<input type="file" name="song" />

Drugač pa mi je zdaj spet skripta normalno delovala, vsi pogojni stavki so bili upoštevani oz. so se izvedli ustrezno. Nimam pojma, kaj se to dogaja.

Je mogoče WAMP kaj kriv?

Lp, Housy

usoban je izjavil:

[Sat Jul 27 12:52:05.137446 2013] [:error] [pid 2796:tid 1544] [client 127.0.0.1:51024] PHP Notice: Array to string conversion in C:\\wamp\\www\\sound\\index.php on line 15, referer: http://localhost/sound/index.php

Zdaj v 15. vrstici (in_array) so argumenti pravilno podani, tako da zna preveri z
var_dump($_FILES)
da nimas slucajno arraya, ker potlej moras do informacij o datoteki dostopat se z indeksom
$_FILES['song']['type'][0]


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

WiFi včasih dela, včasih ne

Oddelek: Omrežja in internet
316454 (1219) alfa_fan
»

Vrednost denarja včasih (strani: 1 2 3 )

Oddelek: Loža
13230666 (26951) GGGGG
»

elektrika včasih dela včasih ne

Oddelek: Elektrotehnika in elektronika
81001 (813) mirator
»

Grafična včasih dela, včasih ne

Oddelek: Zvok in slika
51270 (1164) rzhektor
»

Racunalnik vcasih zalaufa vcasih ne

Oddelek: Pomoč in nasveti
5763 (654) jansko

Več podobnih tem