Jump to content
  • Advertisement
Sign in to follow this  
Ekim_Gram

[web] Uploading Files - General Problems (PHP)

This topic is 4747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I'm currently working on a CMS for my buddy's site and I have come to the part of the project where I have to make a system for uploading music files to a directory and saving various data into a database. Right now I'm creating and testing the entire site and system on my local machine which has Apache and Windows XP Pro installed on it. Here is my source
<?php
	require_once("includes.php");
	session_start();
	
	StyleSheetHeader("Alex Glover - Add A Track","admin_style.css");
	check_valid_user();
	AdminNavbar();
?>

<div id="content">

<?php
	if (isset($_POST['submit']))
	{
		// Declare the variables from the form itself
		$track_name = $_POST['track_name'];
		$track_length = $_POST['track_length'];
		$description = $_POST['description'];
		echo 'Variables initialized';
		
		// Now check for error in uploading the file
		if ($_FILES['file']['error'] > 0)
		{
			echo 'Problem: ';
			switch ($_FILES['file']['error'])
			{
				case 1: echo 'File exceeded upload_max_filesize'; break;
				case 2: echo 'File exceeded max_file_size'; break;
				case 3: echo 'File only partially uploaded'; break;
				case 4: echo 'No file uploaded'; break;
			}
			exit;
		}
		echo 'No errors';

		// Now check the MIME type of the file
		// Accepted types are .midi, .mp2, .mp3, .wav
		if ($_FILES['file']['type'] != 'audio/midi' || $_FILES['file']['type'] != 'audio/mpeg'
	   || $_FILES['file']['type'] != 'audio/x-wav')
	  {
			echo 'Problem: File is not of .midi, .mp2, .mp3, or .wav';
			exit;
		}
		echo 'Correct MIME type';
		
		// Now put the file where it belongs
		$upfile = '../media/tracks/'.$_FILES['file']['name'];
		echo 'Upfile declared';
		
		if (is_uploaded_file($_FILES['file']['tmp_name']))
		{
			if (!move_uploaded_file($_FILES['file']['tmp_name'], $upfile))
			{
				echo 'Problem: Could not move file to the /media/tracks directory';
				exit;
			}
		}
		else
		{
			echo 'Problem: Possible file upload attack. Filename: ';
			echo $_FILES['file']['name'];
			exit;
		}
		
		echo 'File uploaded successfully!<br /><br />';
	}
	else
	{
	?>
	
	<h3>Add A Track</h3>
	<form enctype="multipart/form-data" action="add_track.php" method="post">
		<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
		Track Name: <input type="text" name="track_name" size="50" maxlength="50" /><br />
		Track Length: <i>[mm.ss]</i> <input type="text" name="track_length" size="10" maxlength="10"><br />
		Description: <br /><textarea name="description" cols="80" rows="5"></textarea><br />
		Track File: <i>(10Mb Limit)</i> <input type="file" name="file" /><br />
		<input type="submit" name="submit" value="Submit">
	</form>
	
	<?php
	}
	AdminFooter();
?>

Functions like AdminFooter and header stuff are just generic layout functions I use to keep everything uniform. The main code that isn't working properly is the PHP in the first if statement. I finished writing all this code about an hour prior to this post and within that hour I spent trying to debug everything. I've got echo statements to help me find out what wasn't working but my problem is rather odd. If I just click submit with no file selected, the errors pick up, as well as having the incorrect MIME type of file. If I do select a file of the correct type and within the max filesize the page loads and just reloads with the form and no echo statements, no errors, no uploaded file, just the form. Help is greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
It sounds more like a system setting, then a coding problem.


$ php -i | grep upload
file_uploads => On => On
upload_max_filesize => 2M => 2M
upload_tmp_dir => no value => no value


I'd check the above settings to make sure they are correct. You may want to try explicitly setting the tmp dir for uploads.

Share this post


Link to post
Share on other sites
Yeah, I double checked the system settings in the php.ini file last night and I tried configuring it. This is what I have now copied straight from the file:


;;;;;;;;;;;;;;;;
; 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:\Program Files\Apache Group\Apache\htdocs\temp files"

; Maximum allowed size for uploaded files.
upload_max_filesize = 20M

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!