Jump to content

$_FILES is empty


Fukushousha
 Share

Recommended Posts

Hey all,Since I don't want to bump my other topic and since it already has gone offtopic(not about how to upload images anymore,but about why the heck $_FILES is empty) I decided to create a new topic.So. The problem? I am using a file input form based on the one the php manual shows here. When I am checking the $_FILES array I see it is empty. Actually I got an undefined index, then checked the array and then to my surprise saw it was empty. Then I checked phpinfo() for the variables and all I saw was $_REQUEST['userpic'] and not $_FILES['userpic']. After that I checked php.ini to make sure that file uploading is on. It is. I also checked that my files are not above the file limit. They are not. What can I be doing wrong? I have been trying to understand it the whole day today but to no avail. Any help would be really appreciated.Another quick edit for more info in case it might help to work out this problem. Straight from php.ini

; Whether to allow HTTP file uploads.file_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not; specified).upload_tmp_dir = "C:/Program Files/EasyPHP 2.0b1\tmp\"; Maximum allowed size for uploaded files.upload_max_filesize = 2M

Also I tried uploading a 1.80 MB file to see if it would at least delay a little .. but no the new page is loaded immediately. It is just not uploading :)

Link to comment
Share on other sites

If it's not uploading the file then that doesn't have anything to do with PHP, if the browser isn't sending the file with the request then there's a problem with the upload form where the browser isn't being told to send the file. You might also want to change the slashes in your upload_tmp_dir to all be the same.

Link to comment
Share on other sites

I will do that with the php.ini thanks for the info!As for the form I can't see what can be wrong with it. I had posted it in the previous topic too. Here it is again:

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">		<input type="hidden" name="MAX_FILE_SIZE" value="200000" />		Want to change your picture?<input type="file" name="userpic" id="userpic" />		<input name="subpic" type="submit" value="Upload" />		</form>

All I do somewhere later is check if the subpic is set at the $_POST array and if it is so I am trying to retreive $_FILES['userpic']['name] which returns an undefined index userpic.And the $_FILES array is empty.

Link to comment
Share on other sites

This is working fine for me:

<?php if (isset($_POST['subpic']))  print_r($_FILES);?><html>  <head>	<title>upload test</title>  </head>  <body>		<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">		<input type="hidden" name="MAX_FILE_SIZE" value="200000" />		Want to change your picture?<input type="file" name="userpic" id="userpic" />		<input name="subpic" type="submit" value="Upload" />		</form>  </body></html>

Link to comment
Share on other sites

Yeah that is what I was saying too. I had similar code, a little more complicated but ... for testing's sake I tried it like that too. Still $_FILES is an empty array.Unfortunately I can't think of anything else. Something must be wrong with my whole installation? I used easyphp to install php, apache and MySQL. But up to now (everything else apart from file uploading) it worked fine!

Link to comment
Share on other sites

Go into php.ini and make sure that display_errors is on, that error_reporting is set to E_ALL, and that show_startup_errors is on. Change those in php.ini and then try it again. There might be a problem with saving the file in a temporary folder where the error isn't showing up because error messages aren't being shown. Even if you set those options in the script it's already too late, you need to set them in php.ini.

Link to comment
Share on other sites

Thanks for trying to help justsomeguy. I really appreciate it. Unfortunately thought that wasn't it. My php.ini was already configured like that except from the display_startup_errors setting which was set to off. Even with that set to on though I still get no error.It's as if I never click the file upload button.

Link to comment
Share on other sites

Try uploading a larger file. Regardless of how PHP is set up, the browser should still send the file. If you're testing this on a local computer then it's going to go a lot faster than if it was on the internet. Try it with a 10MB file or so and see if you can tell if it's uploading. You might also want to have a window open to PHP's temp upload folder and see if anything gets written there.You might also just want to make a script to try and copy a file to the temp upload folder, PHP might not have permission to access that folder.

Link to comment
Share on other sites

I had tried uploading various sizes of files ... all were "Done" in an instant, even though I am operating on a local server, still it is not normal.BUT I think we might have something with the last thing you said. I just commented everything out from the previous part of code I was working on and put this :

if(isset($_POST['subpic'])){	//the temporary file server directory	//might use this as a global somewhere later	//HAVE TO CHANGE FOR UPLOADING THE SITE IN A WEB SERVER	//$tempdir = 'C:\Program Files\EasyPHP 2.0b1\tmp';	//phpinfo(1);				file_put_contents("C:\eleos.txt","eleos re paidia");...............

This works. No matter where I put it.BUT If I change it to file_put_contents("C:\Program Files\EasyPHP 2.0b1\tmp","eleos re paidia");this is what I get :

Warning: file_put_contents(C:\Program Files\EasyPHP 2.0b1 mp) [function.file-put-contents]: failed to open stream: Invalid argument in C:\Program Files\EasyPHP 2.0b1\www\settings.php on line 112

Not to mention nothing is written in the temp folder. That must be it! Php can't write to that file ... but how come I did not get that error warning when I used the file form? Moreover ... how do I overcome this discrepancy? I tried changing the temporary server file from php.ini but nothing happened :/Anyhow it is becoming late here in greece and tomorrow I got SQL exams so I 'd better catch some sleep. Thanks again and will check for replies tomorrow!

Link to comment
Share on other sites

There's an escape sequence in the path. When it sees \t it inserts a tab character. Look at the error message:Warning: file_put_contents(C:\Program Files\EasyPHP 2.0b1 mp)That should be b1\tmp, but since \t is a tab, it makes it b1<tab>mp. You need to escape the slash like this:file_put_contents("C:\Program Files\EasyPHP 2.0b1\\tmp","eleos re paidia");

Link to comment
Share on other sites

Yeah you are right. I should have seen that tab. Anyway, with that gone I still get an error, only this time as you predicted a permission denied one.BIG EDIT:Well ... I was wrong. I was still sleepy and did not write the path correctly. No ... that was not the problem. I can create a file in that directory. So, it can't be because php can't write in the servers tmp directory since it actually can.Damn ... still at point one.

Link to comment
Share on other sites

I am really sorry for bumping my own post but this is ridiculous. I completely uninstalled EasyPhP and saved all of my work in a separate file. I downloaded the XAMPP php/apache/mysql bundle and installed it. Then moved all of my work in the new server, re-made all of my configurations and was good to go.I really thought that it would work. BUT NO! Still ... $_FILES is empty after uploading. I don't know what to do anymore. This was like the last resort ... and it did not work.I am including the whole .php file for which I am talking about ( the one where the user file upload form is ):

<?php session_start(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/mainSiteTemplate.dwt.php" codeOutsideHTMLIsLocked="false" --><head> <!-- InstanceBeginEditable name="headerstuff" --><title>Settings</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link href="/CSS/mainStyle.css" rel="stylesheet" type="text/css" /><link href="/CSS/profileStyle.css" rel="stylesheet" type="text/css" /><link href="/CSS/settingsStyle.css" rel="stylesheet" type="text/css" /><script type='text/javascript' src='animateScript2.js'></script><!-- InstanceEndEditable --></head><body><div id="pageLogo"></div><div id="pageHead"></div><div id="leftMenu"> <?php//With the page loading we make sure we instantiate a GLOBAL sql connection objectinclude ('sqlConnection.php');global $sql;$sql = new sqlConnection("localhost", "XXXX", "XXXX");include("login.php");displayLogin(); ?></div>						<div id="tabMenu">								<ul>										<!-- CSS Tabs --><li id="current"><a href="index.php"><span><h1>Home</h1></span></a></li><li><a href="register.php"><span><h1>REGISTER</h1></span></a></li><li><a href='#'><span><h1>USERS</h1></span></a></li><li><a href='#'><span><h1>PROJECTS</h1></span></a></li><li><a href='#'><span><h1>ABOUT</h1></span></a></li>								</ul>						</div><div id="centerOfPage"><!-- InstanceBeginEditable name="head" --><?phperror_reporting(E_ALL);global $sql;//if no user is logged in then .. do now allow to edit setting ofcourseif(!isset($_SESSION['username']))	echo"You are not logged in. Please log in or register first";else{	//get the user who is logged in now!	$username = $_SESSION['username'];	$resArray = $sql->selectUser($username);		//if the user has not uploaded an image for himself show the default	if($resArray['userImage'] == null)	{	?>	<div id="userImage">	</div> 	<?php	}	else	{//else .. show the image he uploaded	?>	<div id="userImage">	<img src="getUImage.php?usname=<?php echo($username);?>" />	</div> 	<?php	}	?>	<div id="imageEdit">		<!--The small form to submit a user image,if it is used refresh the settings page-->		<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">		<input type="hidden" name="MAX_FILE_SIZE" value="200000" />		Want to change your picture?<input type="file" name="userpic" id="userpic" />		<input name="subpic" type="submit" value="Upload New Picture" />		</form> 	<?php eleos();?>	</div>	<?php}?>  <!-- InstanceEndEditable --></div></body><!-- InstanceEnd --></html><?phpfunction eleos(){//if the user clicks the submit picture buttonif(isset($_POST['subpic'])){	//phpinfo(1);			print_r($_FILES);	}}?>

And also the new php.ini part for the file uploading which I ofcourse checked if it is okay.

;;;;;;;;;;;;;;;;; File Uploads;;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.file_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not; specified).upload_tmp_dir = "C:\xampp\tmp"; Maximum allowed size for uploaded files.upload_max_filesize = 32M

I tried escaping the 2 slashes the tmp dir which would have caused a problem if I typed it as a path. Specifically \t and \x butit did not work. Seems that php must do that automatically when reading where the server's tmp path is.

Link to comment
Share on other sites

Sorry for re-bumping my post but I finally managed to solve it. I would like to include the solution in this post since other people might find themselves in my situation. So ... here goes:I tried running the form standalone to see if $_FILES got written. It actually did!So the problem was code-related and not configuration. In my code another php script was included. It was login.php.Login.php contained 2 more forms. As one would expect of a login script ofcourse. I spent the last 2 days searching for empty $_FILES reasons so I learned many different reason why this would happen. One of those was that if you have more forms in the same file with your file upload form you have to add the enctype="multipart/form-data" attribute in them too. I just did that and it works now. So yeah ... ahem ... what can I say? I feel stupid for what I went through the last 2 days to find this. Even installing a different apache/php/mysql bundle .... *sigh*At least now it works. Hope this post can help anyone having the same problem and even help people avoid it in the future.

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...