SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Jun 5, 2007, 10:35 #1
- Join Date
- Nov 2005
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Annoying problem with file uploads...
I'm having a problem with uploading files to my shared hosting (UnitedHosting.co.uk). For some reason it won't allow me to upload certain MP3s. When it comes to using the 'move_uploaded_file()' function, it fails. But only fails on certain MP3s. Some MP3s seem to copy fine, but others just fail when it comes to copying them across from the temp area to the server...
Here is my file upload class:
PHP Code:<?
class FileUpload
{
var $db;
var $directoryPrefix;
var $fileLocation;
var $fileName;
var $fileTmpName;
var $fileType;
var $errorMessage;
function FileUpload($db, $directoryPrefix)
{
// Assign all the object's properties with the
// values that were passed to the class.
$this->db = $db;
$this->directoryPrefix = $directoryPrefix;
$this->errorMessage = "";
}
// =========================================================
// This is the processInput function
//
function processInput($fileLocation, $fileName, $fileTmpName, $fileType)
{
$fileName=str_replace(" ", "_",$fileName);
$this->fileLocation = $fileLocation;
$this->fileName = $fileName;
$this->fileTmpName = $fileTmpName;
$this->fileType = $fileType;
// If the file has no name then...
if (trim($this->fileName) == "")
{
$this->errorMessage .= "The Download is required. ";
return false;
}
else
{
// Checks for all MP3s
if (
$this->fileType == 'audio/mpeg' or
$this->fileType == 'audio/x-mpeg' or
$this->fileType == 'audio/mp3' or
$this->fileType == 'audio/x-mp3' or
$this->fileType == 'audio/mpeg3' or
$this->fileType == 'audio/x-mpeg3' or
$this->fileType == 'audio/mpg' or
$this->fileType == 'audio/x-mpg' or
$this->fileType == 'audio/x-mpegaudio' or
$this->fileType == 'audio/x-mpegurl' or
$this->fileType == 'audio/x-mpeg-3' or
$this->fileType == 'application/vnd.rn-rn-taiko-real-mp3' or
$this->fileType == 'audio/wav' or
$this->fileType == 'audio/x-midi' or
$this->fileType == 'music/crescendo' or
$this->fileType == 'video/x-mpeg'
)
{
//--------------------------------------------------------------
// CHECK IF FILE EXISTS ALREADY
$sql_exists = "SELECT filename FROM downloads WHERE filename = '" . $this->fileName . "'";
$result_exists = $this->db->query($sql_exists);
if($result_exists->size() > 0)
{
$this->errorMessage .= "That Download filename already exists. ";
return false;
}
//--------------------------------------------------------------
// Copy file to server
if(move_uploaded_file ($this->fileTmpName, $this->directoryPrefix . $this->fileLocation . $this->fileName) == false)
{
$this->errorMessage .= "The Download failed to upload. " . $this->directoryPrefix . $this->fileLocation . $this->fileName;
return false;
}
}
else
{
$this->errorMessage .= "The Download is not a MP3 file type. ";
return false;
}
}
return true;
}
// =========================================================
}
Here is the other code that calls the class: (its been stripped down to its basic working bits)
PHP Code:<?
require_once 'includes/Database/MySQL.php';
require_once 'includes/Upload/FileUpload.php';
$db = &new MySQL($host, $dbUser, $dbPass, $dbName);
$upl = &new FileUpload($db, $directory_prefix);
// Is the page in process mode?
if ($_REQUEST['process'] == 'true')
{
$upl->processInput($downloads_directory, $_FILES['file']['name'], $_FILES['file']['tmp_name'], $_FILES['file']['type']);
}
?>
<html>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post" enctype="multipart/form-data" >
Choose Your Download:
<input type="file" name="file" value="<?php echo $download_name; ?>" />
<input type="hidden" name="MAX_FILE_SIZE" value="<?=$downloads_maxsize;?>" />
<input type="hidden" name="process" value="true" />
<input type="submit" name="submit" value="Upload">
</form>
</html>
Is it a problem with my code? My shared hosting? On my local development area, it seems to work fine... It's only when it's live it seems to have problems... Like I said before, certain MP3s work fine but others dont.
How do I get move_uploaded_file() to show an error message so I can maybe debug that correctly? Anyone else get this type of problem?
If anyone could help me, that would be great!
-
Jun 5, 2007, 11:36 #2
- Join Date
- May 2005
- Location
- London, ON
- Posts
- 360
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I imagine its a problem with your max file size in php.ini.
Is it only the larger ones that fail?
You might need to use:
PHP Code:ini_set(upload_max_filesize, <some number of your choosing>);
Edit:
I see you're using the MAX_FILE_SIZE in your form. Are you sure $downloads_maxsize is set correctly?
-
Jun 5, 2007, 11:40 #3
- Join Date
- Nov 2005
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hello. Thinking about it, it may well be...
Say I want to set it to 8MB... How exactly would I put this? Do I just use the ini_set() function anywhere in my script to set it? What's the exact number I would use to set it to 8MB?
Thanks!!
-
Jun 5, 2007, 12:37 #4
- Join Date
- Nov 2005
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hello. I've got:
$downloads_maxsize = "8388608";
Is this number correct? Should I set this number in the ini_set() too?
-
Jun 5, 2007, 12:51 #5
- Join Date
- Nov 2005
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you very much for your help Jeanco! Your help has pointed me to the right place for help... I looked deep into the php.ini file and found that the hosting had set the upload limit to just 2M. I changed this to 8M and now it is working!
Thak you very very much for your time on my problem. Thanks again!
-
Jun 5, 2007, 13:14 #6
- Join Date
- May 2005
- Location
- London, ON
- Posts
- 360
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No problem, glad you ironed it out.
Bookmarks