Hi, I'm pretty new this whole PHP thing, so I need your help. I got this code from somewhere to upload files using two PHP files (a form and an upload file), but I need it to be able to restrict to only jpg, png, gif, and jpeg files. Everything else works great. Can anyone point me in the right direction? Code posted below:
<?php
//upload.php
// Upload directory
$dir = 'uploadpics/';
$num = $_POST['num'];
$messages = array();
for ($x = 1; $x <= $num; $x++) {
$file = $_FILES['file'.$x];
if (! is_uploaded_file($file['tmp_name'])) {
$messages[$x-1] = 'File '.$x.': No file selected.';
continue;
}
if (! move_uploaded_file($file['tmp_name'],$dir.$file['name'])) {
$messages[$x-1] = 'File '.$x.': Unable to move file.';
continue;
} else {
$messages[$x-1] = 'File '.$x.': Uploaded...';
}
}
foreach ($messages as $msg) {
echo $msg.'<br />';
}
?>
and, just in case, the code for the form
<?php
// If the number of files have not been set, show the first form
if ((!isset($_GET['num'])) || (empty($_GET['num']))) {
?>
<html><head><title>Upload Files</title></head><body bgcolor="#0099CC"><table width="640" border="0" align="center"><tr><td><div align="center">
<font size="6" color="#FFFFFF">Upload Pictures</font><br><br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<font color="#FFFFCC">Number of files to upload:</font>
<select name="num">
<?php
for ($x = 1; $x <= 20; $x++) {
echo '<option value='.$x.'>'.$x.'</option>';
}
?>
</select>
<input type="submit" value="Submit" />
</form></div>
</td></tr></table></body></html>
<?php
} else {
// else, show the upload form
// get the integer value of $_GET['num']
$num = intval($_GET['num']);
?>
<html>
<head><title>Upload Pictures</title></head><body bgcolor=#0099CC><table width="640" border="0" align="center"><tr><td>
<div align="center"><font size="6" color="#FFFFFF">Upload Pictures</font>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input type="hidden" name="num" value="<?php echo $num; ?>" />
<?php
// show the file input field $num times
// name the fields file1,file2,file3...
for ($x = 1; $x <= $num; $x++) {
echo 'File '.$x.': <input type="file" name="file'.$x.'" /><br />';
}
?>
<input type="submit" value="Upload Files" />
</form>
<em>Please be patient while your files upload. This may take several minutes. Do not click refresh.</em>
</div>
</td>
</tr>
</table>
</body>
</html>
<?php
}
?>
Thanks in advance for any help