im trying to make an upload form that only uploads images. so im trying to make it ONLY upload these file types; jpg, gif, bmp, swf and png.
can someone please help me with this.
Moderator: General Moderators
Code: Select all
<?
$uploaded_raw=$HTTP_POST_FILES['image']['name'];
$uploaded_array=explode(".",$uploaded_raw);
$image_extension = strtolower($uploaded_array[1]);
// add acceptable extensions here
if($image_extension !== "jpg") {
die ("WARNING!: This file is not acceptable you dirty rat.");
}
?>Code: Select all
<?
$uploaded_raw=$HTTP_POST_FILES['image']['name'];
$image_extension = substr(strtolower($uploaded_raw), -4, 4));
// this should gif you the extension ... LAST four characters ... example .jpg
// add acceptable extensions here
if($image_extension !== ".jpg") {
die ("WARNING!: This file is not acceptable you dirty rat.");
}
?>That works as long as they don't call their JPEG's .jpegtsg wrote:Code: Select all
<? $uploaded_raw=$HTTP_POST_FILES['image']['name']; $image_extension = substr(strtolower($uploaded_raw), -4, 4)); // this should gif you the extension ... LAST four characters ... example .jpg
Code: Select all
$uploaded_raw=$HTTP_POST_FILES['image']['name'];
$reverseName = strrev(strtolower($uploaded_raw));
$uploaded_array=explode(".",$reverseName);
$image_extension = strrev($uploaded_array[1]);Code: Select all
<?php
if(($image_extension !== ".gif") AND ($image_extension !== "jpeg") AND ($image_extension !== ".jpg")==TRUE) {
die ("WARNING!: This file is not acceptable you dirty rat.");
}
?>Code: Select all
<?
$uploaded_raw=$HTTP_POST_FILES['image']['name'];
$image_extension = substr(strtolower($uploaded_raw), -4, 4));
// this should gif you the extension ... LAST four characters ... example .jpg
// add acceptable extensions here
if(($image_extension !== ".gif") AND ($image_extension !== "jpeg") AND ($image_extension !== ".jpg")==TRUE) {
die ("WARNING!: This file is not acceptable you dirty rat.");
}
?>Code: Select all
<html>
<head>
<title>Image Gallery</title>
</head>
<body background="graphics/bakground.gif" text="#00CC00" link="#00CF00" vlink="#00CF00" alink="#00CF00">
<div align="center">
<br>
<iframe height="400" width="400" src="images.html" scrolling="auto" allowtransparency="YES"></iframe>
</div>
<div align="center">
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
<table width="350" border="1" cellpadding="1" cellspacing="1" bordercolor="#00CC00">
<tr>
<td align="right" valign="top">Your Name: </td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="97" align="right" valign="top"><div align="right">Image Title: </div></td>
<td width="240"><input name="imagename" type="text" id="imagename"></td>
</tr>
<tr>
<td align="right" valign="top"><div align="right">Description:</div></td>
<td><textarea name="description" id="description"></textarea></td>
</tr>
<tr>
<td align="right" valign="top"><div align="right">File:</div></td>
<td><input name="filename" type="file" id="filename">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Send Image">
</div></td>
</tr>
</table>
</form>
</div>
</body>
</html>Code: Select all
<?php
$filename=$_POST['filename'];
$description = $_POST['description'];
$imagename = $_POST['imagename'];
$name = $_POST['name'];
$date = date("n-j-Y, g:i:sa");
//uploads the file//
$path = "/images"; //SET THIS TO THE RELATIVE PATH FROM WHERE THE SCRIPT IS TO WHERE THE FILE SHOULD BE UPLOADED TO,
if($filename){
print("File name: $file_name<P>/n");
print("File size: $file_size bytes<P>/n");
if(copy($file, "$path/$filename")){
print("Your File was uploaded successfully");
}else{
print("ERROR, your file was not successfully uploaded");
}
unlink($file);
}
//adds to the images.html//
$filename = 'images.html';
$fp = fopen($filename, "a");
$string = "
$imagename<i>: </i></b><i>Added by $name on $date</i>
<br>
<b>Description: </b><i>$description</i>
<br>
<a href=images/$filename target=_blank>Click here</a>
<br>
";
$write = fputs($fp, $string);
fclose($fp);
Header ('Location index.html');
?>Code: Select all
<?php
echo '<pre>';
print_r($_FILES);
echo '</pre?';
?>