Page 1 of 1
PHP Files Array
Posted: Tue Jan 24, 2012 2:37 am
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
Re: PHP Files Arrau
Posted: Tue Jan 24, 2012 5:57 pm
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
Re: PHP Files Array
Posted: Tue Jan 24, 2012 9:57 pm
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
Re: PHP Files Array
Posted: Sat Jan 28, 2012 10:39 pm
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.
Re: PHP Files Array
Posted: Tue Jan 31, 2012 3:42 pm
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
Re: PHP Files Array
Posted: Tue Jan 31, 2012 7:33 pm
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.
}
Re: PHP Files Array
Posted: Wed Feb 01, 2012 12:29 am
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"]);
}
?>
?>
Re: PHP Files Array
Posted: Fri Feb 03, 2012 1:00 am
by jacek
Well that looks okay, it could be that some files are above the servers upload size limit I guess ?