quick upload script question. solved

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

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

quick upload script question. solved

Post by scarface222 »

Hey guys I have this upload script and

Code: Select all

$filesize = $_FILES['uploadedfile']['size'];
$mimetype = $_FILES['uploadedfile']['type'];
[type] is consistently blank when echoed so for some reason is not registering and [size] is 0. Is this a php bug? Any have an idea?

Code: Select all

$filename = $_FILES['uploadedfile']['name'];
is defined for some reason.
Last edited by scarface222 on Wed Oct 07, 2009 3:00 pm, edited 1 time in total.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: quick upload script question. php bug?

Post by cpetercarter »

The PHP Manual suggests that the 'type' element will contain a value only if the mime type is sent by the browser. There are alternative ways of finding the mime type of a file, and which one you use will depend partly on the version of php which you have -try googling for "php find mime type".
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: quick upload script question. php bug?

Post by jackpf »

Check $_FILES['uploadedfile']['error']...there may have been an error uploading.
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: quick upload script question. php bug?

Post by N1gel »

You could try printing out the whole array to see what it looks like

Code: Select all

 
print_r($_FILES);
 
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick upload script question. php bug?

Post by scarface222 »

Yeah I did this is what I get.

Code: Select all

[name] => song.mp3
            [type] => 
            [tmp_name] => 
            [error] => 1
            [size] => 0
this is form

<form name="titleform" action="" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadedfile" size="40" />
<input type="submit" value="Go!" />
</form>

and if it helps this is upload php script

Code: Select all

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"];
 
echo "<pre>";
echo "FILES:";
print_r($_FILES);
echo "</pre>";
 
$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 > 6920600){
//File is too large
 
if($flag == 0){
$error = "The file you are trying to upload is too large!  
Your file can be up to 6.5 MB in size only.  
Please upload a smaller MP3 file or encode your file with a lower bitrate.";
}
 
$flag = $flag + 1;
}
 
if($filesize < 1048600){
//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 1 MB and smaller than 6.5 MB.";
}
 
$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;
return;
}
}
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: quick upload script question. php bug?

Post by cpetercarter »

You are getting ['error'] = 1, which means that the file has not uploaded because it exceeds the upload maximum filesize. You can find the upload maximum filesize with

Code: Select all

echo ini_get('upload_max_filesize');
You can change the value for upload_max_filesize in php.ini, httpd.conf or .htaccess. If you do not have access to any of these, you will need to look at alternatives, like cgi file uploading.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick upload script question. php bug?

Post by scarface222 »

Thank you so much for your help bro. Just one more question. If I upload my site to a shared server, I will not have access to the php ini file will I so the server company will control my upload size right?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: quick upload script question. php bug?

Post by jackpf »

Yup. You can try to modify it with ini_set() or with a htaccess file, although I'm not sure if it works with this setting since it's kind of security related.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: quick upload script question. php bug?

Post by scarface222 »

K man thanks.
Post Reply