Jump to content

[SOLVED] how to uploadonly jpegs and gifs?


shadiadiph

Recommended Posts

this script is great but how would I make it so the form can only upload jpegs and gifs? At the moment it uploads any file type?

 

html form

 

<?

?>

<html>

<title></title>

<head>

</head>

<body>

 

<form method="post" action="upload.php" enctype="multipart/form-data">

Description:<br>

<input type="text" name="form_description" size="40">

<input type="hidden" name="MAX_FILE_SIZE" value="250000">

<br>File to upload:<br>

<input type="file" name="form_data" size="40">

<p><input type="submit" name="submit" value="submit">

</form>

 

</body>

</html>

 

php page

 

<?php

include("global/connection.php");

include("global/mail.class.php");

error_reporting(7);

 

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

$result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES

 

('$form_description1','$data','$form_data_name','$form_data_size','$form_data_type')");

$id= mysql_insert_id();

print "<p>File ID: <b>$id</b><br>";

print "<p>File Name: <b>$form_data_name</b><br>";

print "<p>File Size: <b>$form_data_size</b><br>";

print "<p>File Type: <b>$form_data_type</b><p>";

print "To upload another file <a href=http://wwwwebsite.com/filetest.php> Click Here</a>";

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/137897-solved-how-to-uploadonly-jpegs-and-gifs/
Share on other sites

mm i looked at that page funnily enough yesterday and couldn't get it to work just tried again with

 

 

if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";}

 

changed userfile_type to form_data_type didn't work

 

I just want to allow the user to upload gifs and jpegs only I just tried the above and was able to successfully upload a word document?

 

thanks blade works fine with this.

 

<?php 
include("global/connection.php");
include("global/mail.class.php");
error_reporting(7);

    if ((($_FILES["form_data"]["type"] == "image/gif")
|| ($_FILES["form_data"]["type"] == "image/jpeg")
|| ($_FILES["form_data"]["type"] == "image/pjpeg"))
&& ($_FILES["form_data"]["size"] < 250000))
  {
  if ($_FILES["form_data"]["error"] > 0)
    {
    echo "Error: " . $_FILES["form_data"]["error"] . "<br />";
    }
  else
    {

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); 
$result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES 

('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); 
$id= mysql_insert_id(); 
print "<p>File ID: <b>$id</b><br>"; 
print "<p>File Name: <b>$form_data_name</b><br>"; 
print "<p>File Size: <b>$form_data_size</b><br>"; 
print "<p>File Type: <b>$form_data_type</b><p>"; 
print "To upload another file <a href=http://www.hmtcompany.com/filetest.php> Click Here</a>"; 
    }
  }
else
  {
  echo "You can only upload JPEG or GIF images that are 250KB or less.";
  }


?> 

 

thanks again now i have to learn about the image library and resizing :)

You might be interested in this, just made it a few days ago!

 

<?php
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$uploadedfiletype = $_FILES['uploadfile']['type'];

if (!($uploadedfiletype =="image/pjpeg" OR $uploadedfiletype =="image/jpeg" OR $uploadedfiletype =="image/jpg")){
header ( "Location: uploadindex.php?stat=jpg" ); };


// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
$height_orig = $height;
$width_orig = $width;

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $width_max variable

// maximum height and width
$width_max = "600";
$height_max = "800";

// initialize reduced dimensions
$heightr = $height_orig;
$widthr = $width_orig;

// if height exceeds max, scale down dimensions
if ($height_max < $heightr) {
$heightr = $height_max;
$widthr = ($height_max / $height_orig) * $width_orig;
}

// if width still exceeds max, further scale down dimensions
if ($width_max < $widthr) {
$heightr = ($width_max / $widthr) * $heightr;
$widthr = $width_max;
} 

$tmp = imagecreatetruecolor($widthr, $heightr); 

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $widthr, $heightr, $width_orig, $height_orig); 

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "images". $newname . ".jpg";
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.

echo $width;
echo "<br>";
echo $height;
echo "<br>";

$imagesize= "../Gallery/Large/". $newname . ".jpg";

list($width2,$height2)=getimagesize($imagesize);

echo $width2;
echo $height2;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//watermark - passed onto another php file for a watermark to be placed on
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
header ( "Location: watermark.php?picid=" . $newname );
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.