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
p3rk5
Forum Commoner
Posts: 34 Joined: Thu Jan 29, 2009 10:19 pm
Post
by p3rk5 » Mon Feb 16, 2009 11:32 am
After clicking submit on this upload script, the page hangs for a few seconds and then displays the form without uploading the file.
Code: Select all
<? session_start();?>
<?php
$logged_in = $_SESSION['logged_in'];
$level = $_SESSION['level'];
$submit = $_POST['submit'];
if ($logged_in && $level <= 2) {
if (isset($submit)) {
$upload_dir = '/music/songs/';
$upload_file = $upload_dir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
echo "File is valid, and was successfully uploaded";
} else {
echo "Upload failed";
}
} else {
echo '<form enctype="multipart/form-data" action="index.php?id=music&page=add" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520" />
<h3>Add Music</h3>
<table border="0" align="center">
<tr>
<td>
<b>Artist:</b>
</td>
<td>
<input type="text" name="artist" />
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<input type="text" name="title" />
</tr>
<tr>
<td>
<b>Genre:</b>
</td>
<td>
<select name="genre"><option value="" selected><option value="Abstract">Abstract</option><option value="Ambient">Ambient</option><option value="Breakbeat">Breakbeat</option></option><option value="Drum and Bass">Drum and Bass</option><option value="Electronic">Electronic</option><option value="Experimental">Experimental</option><option name="Hardcore">Hardcore</option><option value="Hip-Hop">Hip-Hop</option><option value="House">House</option><option value="Techno">Techno</option><option value="Trance">Trance</option></select>
</td>
</tr>
<tr>
<td>
<b>Length:</b>
</td>
<td>
<input type="text" name="min" size="1" maxlength="2" /><span>:</span><input type="text" name="sec" size="1" maxlength="2" />
</td>
</tr>
<tr>
<td>
<b>Quality:</b>
</td>
<td>
<input type="text" name="quality" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="file" name="file" class="file" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Submit" />
</td>
</tr>
</table>';
}
} else {
echo '<h1>Forbidden</h1><p class="logintxt">You are not authorized to view this page</p>';
}
?>
Last edited by
p3rk5 on Tue Feb 17, 2009 2:19 pm, edited 1 time in total.
highjo
Forum Contributor
Posts: 118 Joined: Tue Oct 24, 2006 1:07 pm
Post
by highjo » Mon Feb 16, 2009 12:09 pm
Dude when posting you code make sure you use
php tag [code = php]
Ok what i do first of all when i face those kind of problem is to
and see what is happening.so comment everything and put what i suggested where php process data of you form.
sparrrow
Forum Commoner
Posts: 81 Joined: Mon Oct 20, 2008 12:22 pm
Post
by sparrrow » Mon Feb 16, 2009 12:30 pm
Well if the form is displaying when you submit, and you aren't seeing the upload results echo, then it looks like the problem may be with the check on line 7. Try echoing $submit or $_POST['submit'] and see if anything is in there. I don't see why it wouldn't be set off hand, but I agree with highjo that you should echo and dump everything to the screen that you can to get a visual on what's going on behind the scenes.
p3rk5
Forum Commoner
Posts: 34 Joined: Thu Jan 29, 2009 10:19 pm
Post
by p3rk5 » Mon Feb 16, 2009 12:42 pm
My code now looks like this:
Code: Select all
<? session_start();?>
<?php
$logged_in = $_SESSION['logged_in'];
$level = $_SESSION['level'];
$submit = $_POST['submit'];
if ($logged_in && $level <= 2) {
if (isset($submit)) {
/*$upload_dir = '/music/songs/';
$upload_file = $upload_dir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
echo "File is valid, and was successfully uploaded";
} else {
echo "Upload failed";
}*/
} else {
echo '<form enctype="multipart/form-data" action="index.php?id=music&page=add" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520" />
<h3>Add Music</h3>
<table border="0" align="center">
<tr>
<td>
<b>Artist:</b>
</td>
<td>
<input type="text" name="artist" />
</tr>
<tr>
<td>
<b>Title:</b>
</td>
<td>
<input type="text" name="title" />
</tr>
<tr>
<td>
<b>Genre:</b>
</td>
<td>
<select name="genre"><option value="" selected><option value="Abstract">Abstract</option><option value="Ambient">Ambient</option><option value="Breakbeat">Breakbeat</option></option><option value="Drum and Bass">Drum and Bass</option><option value="Electronic">Electronic</option><option value="Experimental">Experimental</option><option name="Hardcore">Hardcore</option><option value="Hip-Hop">Hip-Hop</option><option value="House">House</option><option value="Techno">Techno</option><option value="Trance">Trance</option></select>
</td>
</tr>
<tr>
<td>
<b>Length:</b>
</td>
<td>
<input type="text" name="min" size="1" maxlength="2" /><span>:</span><input type="text" name="sec" size="1" maxlength="2" />
</td>
</tr>
<tr>
<td>
<b>Quality:</b>
</td>
<td>
<input type="text" name="quality" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="file" name="file" class="file" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Submit" />
</td>
</tr>
</table></form>';
var_dump($_FILES);
echo $_POST['submit'];
}
} else {
echo '<h1>Forbidden</h1><p class="logintxt">You are not authorized to view this page</p>';
}
?>
When I press submit, the form comes up and under it says "array(0) { }". However, when I press submit without a file specified with the first post's code, it echo's the error "Upload failed".
sparrrow
Forum Commoner
Posts: 81 Joined: Mon Oct 20, 2008 12:22 pm
Post
by sparrrow » Mon Feb 16, 2009 10:49 pm
Good news...your code is fine. Bad news...your issue is caused by a php misconfiguration. How large of files are you trying to upload? Run
phpinfo(); and look for the following values. Post them for us to look at if you don't see anything obvious.
file_uploads
memory_limit
post_max_size
upload_max_size
upload_tmp_dir
Also, when you find out for sure what the temp directory is, make sure it has at least 0770 (drwxrwx---). You can check by running fileperms().
Code: Select all
echo substr(sprintf('%o', fileperms('/php_uploads')), -4);
And set it by running chmod().
p3rk5
Forum Commoner
Posts: 34 Joined: Thu Jan 29, 2009 10:19 pm
Post
by p3rk5 » Tue Feb 17, 2009 2:18 pm
Thanks so much for the solution. The files I was trying to upload were .mp3's so they were larger than the max sizes allowed in my php.ini. After changing the values, the script worked flawlessly. Thanks a lot!