SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
-
Nov 7, 2007, 12:36 #1
- Join Date
- Nov 2004
- Location
- Canada
- Posts
- 373
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can anyone see anything wrong with this code?
The code below is an upload script, which essentially uploads a file and checks the file type and file size.
For the most part this works fine, it detects invalid file file extensions, however if the file is over 2mb the script doesnt do what it should, and doesnt output any kind of result or do the upload.
Is this a memory issue on the server, or is it a issue with coding?
PHP Code:define('MAX_FILE_SIZE', 8388608); // 8 MB
$allowed_types = array('image/gif', 'image/pjpeg', 'image/jpeg', 'image/jpg', 'image/png', 'image/bmp');
$target_path = $uploadLocation . basename( $_FILES['file']['name']);
$extension = substr(strrchr($_FILES['file']['type'], "/"), 1);
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_NO_FILE:
$error = 'No file selected to upload!';
echo $error;
break;
case UPLOAD_ERR_OK:
if ($_FILES['file']['size'] > MAX_FILE_SIZE) {
$error = 'File is too large!';
echo $error;
break;
}
if (!in_array($_FILES['file']['type'], $allowed_types)) {
$error = 'That file type is not allowed!';
echo $error;
break;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file: ". basename( $_FILES['file']['name']). " has been uploaded!"; }
else {
$error = 'Could not move file, upload failed!';
echo $error;
break; }
}
-
Nov 7, 2007, 12:41 #2
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
It's because of many different settings in PHP.ini and httpd.conf - there is usually a server upload limit of 2mb.
Do you have access to php.ini and httpd.conf? Note that if you upload the files later to a server, you may have problems.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 7, 2007, 12:58 #3
default max execution time is 30 seconds. if the upload takes longer than 30 seconds it will fail.
-
Nov 7, 2007, 13:33 #4
- Join Date
- Nov 2004
- Location
- Canada
- Posts
- 373
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your right the upload limit is 2M in the php config file. Is there a way to return this as a error message instead of displaying nothing?
Also, when I attempt to upload 70MB file to test (my limit is 8M so im expecting a error from the script) the script craps out, does PHP load the file into memory first even before it transfers it?
-
Nov 7, 2007, 13:38 #5
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
yup:
PHP Code:define('MAX_FILE_SIZE', 2097152);
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 7, 2007, 13:50 #6
- Join Date
- Nov 2004
- Location
- Canada
- Posts
- 373
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I know that, wondering if I could output that it was a server error as opposed to a script error... Also, that is of no help because if I try to upload a 25mb file, the script works for a while, then just does nothing, I dont even get an error message stating the file is too large.
Its as if PHP attempts to digest the entire file and ultimately craps out not letting the script do its thing.Last edited by tdob; Nov 7, 2007 at 14:37.
-
Nov 7, 2007, 13:57 #7
-
Nov 7, 2007, 16:07 #8
- Join Date
- Jan 2002
- Location
- Australia
- Posts
- 2,634
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
HTTP isn't all that reliable for uploading large files. Even if you get everything configured right I think it's still going to be hit and miss with files more that a few MB
-
Nov 12, 2007, 09:59 #9
- Join Date
- Nov 2004
- Location
- Canada
- Posts
- 373
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The thing is, I dont want to upload large files. Just .pdf files or image files.
The limit on the PHP Server is 2mb, which is fine. If a user tries to upload a file up to 7mb they get the appropiate error based on the PHP limit.
My issue is, if a user tries (for whatever reason) to upload a larger file, nothing happens and the page kind of reloads itself seemingly ignoring all code/error checking. I suppose this is just something you have to deal with PHP/Uploads?
-
Nov 12, 2007, 10:34 #10
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Shouldn't the class you use deal with files larger than MAX_FILE_SIZE?
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 12, 2007, 10:47 #11
- Join Date
- Nov 2004
- Location
- Canada
- Posts
- 373
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 12, 2007, 11:12 #12
That should yeld a message on browser side. If the browser does that is another question.
If file size exceeds this value, a respective error code will be set to $_FILES['userfile']['error'] and upload will be considered as failed.
If you use this html field attribute, you must handle the error in php too.Saul
-
Nov 12, 2007, 12:48 #13
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
No, i mean in the class, it checks for the file size:
PHP Code:if($_FILES['file']['size'] > MAX_FILE_SIZE)
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Bookmarks