Page 1 of 1

Uploading a file

Posted: Wed Aug 17, 2011 11:06 am
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]

Re: Uploading a file

Posted: Wed Aug 17, 2011 12:00 pm
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?

Re: Uploading a file

Posted: Wed Aug 17, 2011 12:25 pm
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.

Re: Uploading a file

Posted: Wed Aug 17, 2011 12:30 pm
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

Re: Uploading a file

Posted: Wed Aug 17, 2011 12:39 pm
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.

Re: Uploading a file

Posted: Wed Aug 17, 2011 12:52 pm
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