As promised, this is the code I use for an image upload script - it's highly cut down because I do a helluva lot more with mine such as working out pixel counts etc (demo here:
http://www.richardm.co.uk/gallery/?c...on=Screenshots)
Also, I think this code is an older version than what I'm using now but it should work.
HTML, important bits are marked in bold:
Quote:
<form action="./addimage.php" method="post" enctype="multipart/form-data">
<p>Choose file:</p>
<input type="file" name="ImageUploadedFile" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
|
PHP Code:
PHP Code:
// Variables
$ImageDestination = $_POST['ImageDestination'];
$ImageFiletype = $HTTP_POST_FILES['ImageUploadedFile']['type'];
$ImageFilesize = $HTTP_POST_FILES['ImageUploadedFile']['size'];
$ImageUploadedFile = $HTTP_POST_FILES['ImageUploadedFile']['tmp_name'];
$ImageFilename = $HTTP_POST_FILES['ImageUploadedFile']['name'];
// Test Image - nothing allowed apart from jpg, gif and png.
if ($ImageFiletype == 'image/jpeg')
{
$ImageFiletype = 'jpg';
}
elseif ($ImageFiletype == 'image/gif') {
$ImageFiletype = 'gif';
}
elseif ($ImageFiletype == 'image/png') {
$ImageFiletype = 'png';
}
else {
echo '<html><body><p>The file was not an image!</p></body></html>';
exit;
}
echo '<html><body>';
// File handler
if(!is_array ($HTTP_POST_FILES['ImageUploadedFile'])) {
echo '<p>A file was not uploaded...<br />';
}
$f =& $HTTP_POST_FILES['ImageUploadedFile']; // saves typing
$dest_dir = "../gallery/" . $ImageDestination;
$dest = $dest_dir . '/' . $f['name'];
if (!is_uploaded_file ($f['tmp_name'])) {
echo 'Error: No file uploaded.<br />';
}
if (!file_exists ($dest_dir)) {
echo "Error: Destination directory $dest_dir does not exist!<br />";
}
if (!is_dir ($dest_dir)) {
echo "Error: Destination directory $dest_dir is not a directory!<br />";
}
if (file_exists ($dest)) {
echo "<html><body>Error: File $dest already exists! I will try to overwrite it...<br />";
}
// all clear, move the file to its permanent location
$r = move_uploaded_file ($f['tmp_name'], $dest);
if ($r == false)
{ // something went wrong
echo "Error: Could not copy file to $dest <br />";
}
else {
echo 'File uploaded OK...';
}
echo '</body></html>';
If you want to credit me just link to
www.richardm.co.uk - cheers.