Jump to content


upload script


12 replies to this topic

#1 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 23 March 2007 - 01:29 AM

im having alot of trouble with this upload script im making, im still learning php and if anyone could tell me whats wrong O_O

( I'm going to add in security when I actually get the damn file onto the server )

heres the upload form:

 <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
 <tr>
 <form action="mp3upload.php" method="post" enctype="multipart/form-data">
 <td>
 <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
 <tr>
 <td colspan="2"><div align="center"><strong>Upload Music to Mp3 Player </strong></div></td>
 </tr>
 <tr>
 <td>Genre of Music</td>
 <td><select name="genre">
	 <option value="rock">Rock/Metal</option>
	 <option value="rap">Rap</option>
	 <option value="techno">Techno</option>
	 <option value="trance">Trance</option>
	 <option value="habibi">Habibi</option>
	 <option value="misc">Misc.</option>
   </select></td>
 </tr>
 <tr>
 <td> Select MP3</td>
 <td><input name="file" type="file" /></td>
 </tr>
 <tr>
 <td colspan="2" align="center"><input type="submit" name="submit" value="Upload" /></td>
 </tr>
 </table>
 </td>
 </form>
 </tr>
 </table>

heres the actual php script:

 <?php
 
 if($_POST['submit']) {
 
 /*
	 $rock = "mp3s/rock/";
	 $rap = "mp3s/rap/";
	 $techno = "mp3s/techno/";
	 $habibi = "mp3s/habibi/";
	 $trance = "mp3s/trance/";
	 $misc = "mp3s/misc/";
 */
 
	if ($_POST['genre'] == "rock"){
			copy($HTTP_POST_FILES['file']['tmp_name'],"/mp3s/rock/".$HTTP_POST_FILES['file']['name']);
			unlink($HTTP_POST_FILES['file']['tmp_name']);
			echo ' <h1> Thanks for uploading! </h1> ';
 
			}else if ($_POST['genre'] == "rap"){
				  copy($HTTP_POST_FILES['file']['tmp_name'],"/mp3s/rap/".$HTTP_POST_FILES['file']['name']);
				  unlink($HTTP_POST_FILES['file']['tmp_name']);
				  echo ' <h1> Thanks for uploading! </h1> ';
 
			}else if ($_POST['genre'] == "techno"){
				  copy($HTTP_POST_FILES['file']['tmp_name'],"/mp3s/techno/".$HTTP_POST_FILES['file']['name']);
				  unlink($HTTP_POST_FILES['file']['tmp_name']);
				  echo ' <h1> Thanks for uploading! </h1> ';
 
			}else if ($_POST['genre'] == "trance"){
				  copy($HTTP_POST_FILES['file']['tmp_name'],"/mp3s/trance/".$HTTP_POST_FILES['file']['name']);
				  unlink($HTTP_POST_FILES['file']['tmp_name']);
				  echo ' <h1> Thanks for uploading! </h1> ';
 
			}else if ($_POST['genre'] == "habibi"){
				  copy($HTTP_POST_FILES['file']['tmp_name'],"/mp3s/habibi/".$HTTP_POST_FILES['file']['name']);
				  unlink($HTTP_POST_FILES['file']['tmp_name']);
				  echo ' <h1> Thanks for uploading! </h1> ';
 
			}else if ($_POST['genre'] == "misc"){
				  copy($HTTP_POST_FILES['file']['tmp_name'],"/mp3s/misc/".$HTTP_POST_FILES['file']['name']);
				  unlink($HTTP_POST_FILES['file']['tmp_name']);
				  echo ' <h1> Thanks for uploading! </h1> ';
 
			}else
				 echo ' <h1><font color="red"><strong>UPLOAD FAILED!</strong></font></h1> ';
	}
 ?>

any help appreciated :)

edit: the folders have read/write access

Edited by HiJinks, 23 March 2007 - 01:32 AM.


#2 _*Creative Insanity_*

  • Guests

Posted 23 March 2007 - 01:56 AM

If you are uploading a larger file then the default (2MB) make sure you add a php.ini file where they script executes with the line.
upload_max_filesize = 2M // change 2 for the size you want to upload


#3 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 23 March 2007 - 02:13 AM

alright I put php.ini in the same folder with my script,
upload_max_filesize = 20M

script still doesn't work, just says "thanks for uploading!" O_o can someone take a closer look at the copy function to see if everything is alright

Edited by HiJinks, 23 March 2007 - 02:13 AM.


#4 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 23 March 2007 - 02:55 AM

omg i got it working, just had to take out the extra = when it compared the genre :)

#5 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 23 March 2007 - 09:40 AM

View PostHiJinks, on Mar 23 2007, 02:55 AM, said:

omg i got it working, just had to take out the extra = when it compared the genre :ph34r:
odd. :P

generally, $_POST['genre'] = "rock" would assign the value rather than compare the value.

#6 bay

    Young Padawan

  • Members
  • Pip
  • 105 posts
  • Gender:Male
  • Location:Chicago, IL USA

Posted 23 March 2007 - 10:28 PM

View PostSpatialVisionary, on Mar 23 2007, 09:40 AM, said:

View PostHiJinks, on Mar 23 2007, 02:55 AM, said:

omg i got it working, just had to take out the extra = when it compared the genre :D
odd. ^_^

generally, $_POST['genre'] = "rock" would assign the value rather than compare the value.

Exactly. You need both equal signs to compare a value to a string, using only 1 will always return true, because you are setting the value to the variable.

I think the problem is in your php.ini file. You said you changed the value to 20M but did you remember to restart your apache service?

#7 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 24 March 2007 - 01:16 AM

well I have no idea how to restart my apache or php.ini is at, I thought you just created a php.ini and placed it in the folder O_o

the file im uploading is a test file and only a mere 300kbs not anything over 2mb, It just appeared after I changed == to =, so I assumed it worked O_o, but now it doesn't ^_^

I've followed tutorials, many different ones, each one does not work so I assume it's something with my php.ini but I don't know how to configure it or restart my apache :D

#8 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 24 March 2007 - 01:45 AM

i tried $_FILES['userfile']['error']
and it returned 0

0: No error, the file was uploaded successfully

but the file is no where to be found on my ftp ^_^

#9 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 24 March 2007 - 01:45 PM

You should run some error throwing conditions with your block of code.
And you are totally misunderstanding what php.ini is. It isn't like a .htaccess file, letting you just throw it into a directory. It is the configuration file for the PHP core itself, and is installed on the server itself, where you won't be able to access it through your FTP, and can only access it by the server physically (or through your host).

Try this and tell us what happens.

<?php
if($_POST['submit']){
	// Verify Genre
	$genres = array('rock', 'rap', 'techno', 'trance', 'habibi', 'misc');
	if(in_array($_POST['genre'], $genres))
		$genre = $_POST['genre'];
	else
		die('Invalid Genre');
	
	// Attempt to Copy
	if(!move_uploaded_file($_FILES['file']['tmp_name'], "./mp3s/{$genre}/{$_FILES['file']['name']}"))
		die('Error Uploading File');
	else{
		unlink($_FILES['file']['tmp_name']);
		echo '<h1>Upload Successful!</h1>';
	}
}
?>

Granted, this isn't a secure upload script at all, but it is much simpler, and should get you started.

#10 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 24 March 2007 - 07:56 PM

wow it worked thanks alot!

but I get this error when it uploads

Warning: unlink(/tmp/phpDq0LwI) [function.unlink]: No such file or directory in /homepages/40/d178490812/htdocs/mp3upload.php on line 14
Upload Successful!

is there a way to change the php.ini file setting? 2mb is kinda low for songs

#11 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 24 March 2007 - 08:11 PM

Just remove the unlink function, it's not needed since the server will automatically delete the file form it's temp folder.

As for the upload size, put this in your htaccess.

php_value post_max_size 50M
php_value upload_max_filesize 50M

#12 HiJinks

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 24 March 2007 - 10:51 PM

when I do that I get
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webadmin@kundenserver.de and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

which makes everything unusable

#13 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 25 March 2007 - 07:43 AM

Do you have anything else in your htaccess file?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users