Uploading a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
karphp
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 12:39 pm

Uploading a file

Post by karphp »

Hello All,

I am new to php and am trying to write a function to upload a file to my server. The function works great with images but fails when I try to upload a file type with no mime type (binary file). How do I fix this? In the code below, the binary file I am trying to upload has the .lst extension and is binary data.

My Php code is as follow:

Code: Select all

<?php 
include('AnalyseListMode.html');

$allowedExtensions = array("lst","png");

$target = "upload/"; 
echo $_FILES['file'];

print "<pre>";
print_r($_FILES);
print "</pre>";

if (!in_array(end(explode(".", strtolower($_FILES["file"]['name']))), $allowedExtensions)){
	die('invalid extenstion');
}

echo '<br>';
echo basename( $_FILES['file']['name']);
$target = $target . basename( $_FILES['file']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
{
	//echo "The file ". basename( $_FILES['file']['name']). " has been file";
	echo "Upload: " . $_FILES["file"]["name"] . "<br />";
	//echo "Type: " . $_FILES["file"]["type"] . "<br />";
	echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
	//echo "Stored in: " . $_FILES["file"]["tmp_name"];
} 
else {
	echo "Sorry, there was a problem uploading your file.";
}
?>
When I upload a .png file, everthing goes well and print_r($_FILES); prints the correct info. However, when uploading a .lst file I get this error:

[text]Notice: Undefined index: file in upload_file.php on line 7
Array
(
)[/text]
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Uploading a file

Post by Jonah Bron »

That's strange. Are you sure the binary file doesn't exceed the max upload size? What happens if you upload a text/plain file?
karphp
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 12:39 pm

Re: Uploading a file

Post by karphp »

Thanks, Bron.

What is the max upload size?

By the way, you are right. I split the files in 1M chunks and it works. The original file was 26 MB.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Uploading a file

Post by phphelpme »

Would this not be your answer:

Code: Select all

 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
You can always look in your php.ini file and see what your max file limit is as servers differ.

Best wishes
karphp
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 12:39 pm

Re: Uploading a file

Post by karphp »

thanks again.

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

would not work for me as a large file does not upload. When the upload fails, this line is never reached.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Uploading a file

Post by phphelpme »

You need to look inside your php.ini file and see what your maximum upload file size is as it can change from server to server etc. Your maximum upload size is determined by your php.ini file.

Best wishes
Post Reply