De voorbije dagen heb ik geprobeerd zelf een uploadscript te schrijven in
PHP. Ik heb aardig wat gevloekt terwijl ik er mee bezig was.
Nu zit ik echter nog altijd met enkele problemen. Ik hoop dat er iemand bij
verder kan helpen.
Mijn vragen:
1) $subject = $_POST['subject']; Is dit nodig om te doen of niet? Maw kan ik
onmiddellijk $subject gebruiken?
2) Wanneer ik het veld "userfile" invul, dus een file selecteer dan blijft
het script beweren dat ik dit veld niet ingevuld heb. Hoe kan ik dit
oplossen?
3) if ($filesize > $max_file_size): deze controle wordt precies niet
uitgevoerd want als ik een file groter dan 5mb probeer te uploaden dan krijg
ik geen melding dat de file te groot is.
Andere tips, suggesties,.. zijn altijd welkom. Bedankt!
<?php
if(file_exists('../../private/open_db.inc') &&
is_readable('../../private/open_db.inc'))
include('../../private/open_db.inc');
$db = OpenDb() or die ("can not connect");
// Define some variables
$author = $_SESSION['user'];
$date = date("d/m/y");
$subject = $_POST['subject'];
$userfile = $_POST['userfile'];
$max_file_size = $_POST['max_file_size'];
$filetype = $_FILES['userfile']['type'];
$filename = $_FILES['userfile']['name'];
$filesize = $_FILES['userfile']['size'];
$uploadfile = $_FILES['userfile']['tmp_name'];
$uploaddir = "/opt/www/warniere/web/chemical.while.be/members/files/";
$filetype_array = array("application/pdf", "application/msword",
"application/vnd.ms-excel", "application/x-zip-compressed", "image/jpeg",
"image/gif", "image/png");
if(($subject == "0") || (!$userfile) ||
(!in_array($filetype,$filetype_array)) || ($filesize > $max_file_size))
{
echo 'It seems that there are some problems:<br />';
if($subject == "0"){
echo "<strong>You didn't choose a subject for the file!</strong> Please
choose one below.<br />";
}
if(!$userfile){
echo "<strong>You didn't select a file to upload!</strong> Please select
a file.<br />";
}
if (!in_array($filetype,$filetype_array)){
echo "<strong>Invalid file type!</strong> Only PDF, Word and Excel
documents are allowed as well as JPEG and GIF image files.";
}
if ($filesize > $max_file_size){
echo "<strong>The file you are trying to upload is too big!</strong> Only
files less than 5 mb are allowed.";
}
include 'add_file_form.php'; // Show the form again!
}
// Choose the directory were the file will be placed
switch($subject)
{
case 1: $subject_dir = "IAP/";
break;
case 2: $subject_dir = "UO2/";
break;
default: $subject_dir = "";
}
$file_destination = $uploaddir.$subject_dir.$filename;
// Put file
if(!is_file($file_destination))
{
print "<pre>";
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
move_uploaded_file($uploadfile, $file_destination);
print "File is valid, and was successfully uploaded. Here's some more
debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
}
else
{
echo "<strong>Sorry, the file you are trying to upload already exists in
the database.</strong><br>Please rename your file and try again.";
}
?>
<form action="add_file2.php" method="post" name="add_file"
enctype="multipart/form-data">
<table>
<tr>
<td align="right">Subject:</td>
<td><select name="subject" class="form">
<option value="0" selected>Choose one</option>
<option value="1">IAP</option>
<option value="2">UO2</option>
</select>
</td>
</tr>
<tr>
<td align="right">File:</td>
<td><input type="hidden" name="max_file_size" value="5000000"><input
name="userfile" type="file" class="form"></td>
<!-- Max File Size of Uploads: 100000 = 100KB, 500000 = 500KB etc etc -->
</tr>
<tr>
<td align="right">Description:</td>
<td><input name="description" type="text" size="50" class="form"></td>
</tr>
<tr>
<td colspan="2" align="right"><input name="clear" type="reset"
value="clear"><input name="submit" type="submit" value="upload"></td>
</tr>
</table>
</form>

Likes:

.
Quote
Op deze manier weet je