|
-
Jan 23rd, 2009, 03:44 PM
#1
Thread Starter
Frenzied Member
$_FILES['uploadedfile']['size']; always eqauls 0
Hello,
i have a form on a html page that is for uploading mp3 files. In the form there is an inputbox with the name uploadedfile.
I am having some difficulties, the below code always echos 0. Even though when i put my mouse over the mp3 file which i want to upload, it showed that it was 2.62 MB.
php Code:
$filesize = $_FILES['uploadedfile']['size'];
echo "Filesize: ".$filesize."<br>";
-
Jan 24th, 2009, 08:24 AM
#2
Re: $_FILES['uploadedfile']['size']; always eqauls 0
well, assuming you didn't (or the forum didn't) screw up the code you posted -- $_FILES['uploadedfile']['size']" is not a correct variable reference. try $_FILES['uploadedfile']['size'] instead.
edit: after further inspection, it seems the forum did screw up the code (use [php][/php] from now on to prevent those issues!). in this case.. is the $_FILES array empty altogether? are the name, tmp_name and other values returning correctly? you could also try using filesize() on the tmp_name:
PHP Code:
$filesize = filesize($_FILES['uploadedfile']['tmp_name']);
Last edited by kows; Jan 24th, 2009 at 08:34 AM.
-
Jan 24th, 2009, 09:34 AM
#3
Thread Starter
Frenzied Member
Re: $_FILES['uploadedfile']['size']; always eqauls 0
Well this code works correctly..
PHP Code:
$filename = $_FILES['uploadedfile']['name'];
EDIT:
I i am not what this code does it may be the reason it is 0, i got this code off a tutorial i am still looking through the code and making sense of it.
After it sets the variable filename and before it does the echo, it does this:
PHP Code:
$filesize = htmlentities($filesize);
Last edited by noahssite; Jan 24th, 2009 at 09:37 AM.
-
Jan 24th, 2009, 09:40 AM
#4
Thread Starter
Frenzied Member
Re: $_FILES['uploadedfile']['size']; always eqauls 0
Actually just so i do not miss anything here is all the code stored in a file called configure.php :
NOTE: I got this code off another forum it was given to someone who had a problem with uploading file..
PHP Code:
<?php
//**********************************************************************************************
echo "Please wait while we attempt to upload your file...<br><br>";
//**********************************************************************************************
$target_path = "uploads/";
$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.
$filename = $_FILES['uploadedfile']['name'];
$filesize = $_FILES['uploadedfile']['size'];
$mimetype = $_FILES['uploadedfile']['type'];
$filename = htmlentities($filename);
$filesize = htmlentities($filesize);
$mimetype = htmlentities($mimetype);
$target_path = $target_path . basename( $filename );
if($filename != ""){
echo "Beginning upload process for file named: ".$filename."<br>";
echo "Filesize: ".$filesize."<br>";
echo "Type: ".$mimetype."<br><br>";
}
//First generate a MD5 hash of what the new file name will be
//Force a MP3 extention on the file we are uploading
$hashedfilename = md5($filename);
$hashedfilename = $hashedfilename.".mp3";
//Check for empty file
if($filename == ""){
$error = "No File Exists!";
$flag = $flag + 1;
}
//Now we check that the file doesn't already exist.
$existname = "uploads/".$hashedfilename;
if(file_exists($existname)){
if($flag == 0){
$error = "Your file already exists on the server!
Please choose another file to upload or rename the file on your
computer and try uploading it again!";
}
$flag = $flag + 1;
}
//Whitelisted files - Only allow files with MP3 extention onto server...
$whitelist = array(".mp3");
foreach ($whitelist as $ending) {
if(substr($filename, -(strlen($ending))) != $ending) {
$error = "The file type or extention you are trying to upload is not allowed!
You can only upload MP3 files to the server!";
$flag++;
}
}
//Now we check the filesize. If it is too big or too small then we reject it
//MP3 files should be at least 1MB and no more than 6.5 MB
if($filesize > 10485760){
//File is too large
if($flag == 0){
$error = "The file you are trying to upload is too large!
Your file can be up to 10 MB in size only.
Please upload a smaller MP3 file or encode your file with a lower bitrate.";
}
$flag = $flag + 1;
}
if($filesize < 51200){
//File is too small
if($flag == 0){
$error = "The file you are trying to upload is too small!
Your file has been marked as suspicious because our system has
determined that it is too small to be a valid MP3 file.
Valid MP3 files must be bigger than 50 kb and smaller than 10 MB.
50 kb is the min. for SmartISound announcements although it should be much larger.";
}
$flag = $flag + 1;
}
//Check the mimetype of the file
if($mimetype != "audio/x-mp3" and $mimetype != "audio/mpeg"){
if($flag == 0){
$error = "The file you are trying to upload does not contain expected data.
Are you sure that the file is an MP3?";
}
$flag = $flag + 1;
}
//Check that the file really is an MP3 file by reading the first few characters of the file
$f = @fopen($_FILES['uploadedfile']['tmp_name'],'r');
$s = @fread($f,3);
@fclose($f);
if($s != "ID3"){
if($flag == 0){
$error = "The file you are attempting to upload does not appear to be a valid MP3 file.";
}
$flag++;
}
//All checks are done, actually move the file...
if($flag == 0){
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
//Change the filename to MD5 hash and FORCE a MP3 extention.
if(@file_exists("uploads/".$filename)){
//Rename the file to an MD5 version
rename("uploads/".$filename, "uploads/".$hashedfilename);
echo "The file ". basename( $filename ). "
has been uploaded. Your file is <a href='uploads/$hashedfilename'>here</a>.";
}
else{
echo "There was an error uploading the file, please try again!";
}
} else{
echo "There was an error uploading the file, please try again!";
}
}
else {
echo "File Upload Failed!<br>";
if($error != ""){
echo $error;
}
}
?>
Here is the code that calls it, this html code is in a file called announcement.html :
HTML Code:
<html>
<body>
<form action="configure.php" method="post"
enctype="multipart/form-data">
<label for="file">Audio File:</label>
<input type="file" name="uploadedfile" id="uploadedfile" />
<br />
<br />
<input type="submit" name="submit" value="Set Announcement:" />
</form>
</body>
</html>
-
Jan 24th, 2009, 11:37 AM
#5
Re: $_FILES['uploadedfile']['size']; always eqauls 0
well, I don't see any reason for the code you're posting to not work. did you try using the code I posted, to see if it would return a value?
-
Jan 25th, 2009, 08:12 AM
#6
Thread Starter
Frenzied Member
Re: $_FILES['uploadedfile']['size']; always eqauls 0
The code you posted returns 0 as well. I don't think this is the problem but i havn't enabled the right permissions of the uploads folder on my server. Though if that was the issue then it would say Upload Failed, it should still be able to retrieve the file properties.
-
Jan 26th, 2009, 03:16 AM
#7
Re: $_FILES['uploadedfile']['size']; always eqauls 0
well, I uploaded the script and it returned the size of the file without issue. I'm not sure why you're having a problem, but it's server related I'm sure. where are you hosting this?
Code:
Please wait while we attempt to upload your file...
Beginning upload process for file named: Picture.jpg
Filesize: 933330
Type: image/jpeg
File Upload Failed!
The file type or extention you are trying to upload is not allowed! You can only upload MP3 files to the server!
-
Jan 27th, 2009, 08:24 PM
#8
Thread Starter
Frenzied Member
Re: $_FILES['uploadedfile']['size']; always eqauls 0
First of all i tried uploading a mp3, second i am hosting at www.godaddy.com.
-
Jan 28th, 2009, 06:04 AM
#9
Re: $_FILES['uploadedfile']['size']; always eqauls 0
a file is a file, so just because your file had the extension mp3 doesn't change anything. PHP doesn't know anything about said file, except its name.
I would ask godaddy why it might not work.. because, like I said, the script works fine.
-
Feb 4th, 2009, 08:09 AM
#10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|