PHP Files Array

Ask about a PHP problem here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

PHP Files Array

Post by wrichards8 »

Hi all...


The protblrm I am currently having involves $_FILES. I am using thr multipart/form-data enctype but the files array is not always populated. The array gets populated for some files, but not otherd. Does this mean I need to use the ob_start() function as it is a self submitting form

thznkx
Last edited by jacek on Tue Jan 24, 2012 5:57 pm, edited 1 time in total.
Reason: Fixed title.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Files Arrau

Post by jacek »

It has nothing to do with ob_start at all(). You are probably making a mess of the html somehow so that it doesn't work in some way.

Post your code and we will take a look :)
Image
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Re: PHP Files Array

Post by wrichards8 »

OK, my html code is
<form action="index.php" method="POST" enctype="multipart/form-data"> <input type="file" name="upload"><input type="submit" name="submit">
</form>
My PHP code is

<?php if(isset($_POST["submit"]))
{
	print_r($_FILES);
}
?>
Like I said, sometimes the array is populated and sometimes not I am mainly using this code on a music sharing site where, I guess, files could be quite large
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Files Array

Post by jacek »

Is that the entire code ? I don't really see any problems that would break it. method="POST" should be lower-case but I doubt that will be the cause.
Image
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Re: PHP Files Array

Post by wrichards8 »

Well I did have an allowed extensions array to filter unwanted extensions
$allowed = array("mp3","wma","ogg"); 
but thats the only other thing. I also had
if(isset($_POST['submit']))
{
	print_r($_FILES);
}
and now, when I take out the if(isset($_POST['submit'])); line, I get


Notice: Undefined index: upload in C:\Webroot\htdocs\ypload\index.php on line 10

when the $_FILES array isn't populated
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Files Array

Post by jacek »

wrichards8 wrote:but thats the only other thing. I also had
Could you just post the full script ? I can't really think what it looks like :?
wrichards8 wrote:and now, when I take out the if(isset($_POST['submit'])); line, I get


Notice: Undefined index: upload in C:\Webroot\htdocs\ypload\index.php on line 10

when the $_FILES array isn't populated
That would be because you try to use it before you check to make sure it's there.

Essentially you want
if (isset($_FILES['upload']['name'])){
    // Do file things here.
}
Image
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Re: PHP Files Array

Post by wrichards8 »

Sorry, heres the code in full
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" name="submit">
</form>

<?php
if(isset($_POST["submit"]))
{
	print_r($_FILES);
	$allowed_exts = array("mp3","wma", "ogg");
	$filename = strtolower($_FILES["upload"]["name"]);  
	$filextend = substr($filename, -3);
	$filesize = $_FILES["upload"]["size"];
	if(!in_array($filextend, $allowed_exts))
	{
		echo "I can't upload ", $filextend, " files!";
	}
	if($filesize > 10240000)
	{
		echo "This file can't be larger than 15MB";
	}
	move_uploaded_file($_FILES["upload"]["tmp_name"], "uploads/". $_FILES["upload"]["name"]);
}
?>
?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Files Array

Post by jacek »

Well that looks okay, it could be that some files are above the servers upload size limit I guess ?
Image
Post Reply