Jump to content

Image filter


emediastudios

Recommended Posts

Hi everyone, been working on this code for a while and wish to add a filter that checks for file exist, file size and extensions.

Is there an easy way to add it to my code below.

<?php require_once('../Connections/p2w.php');


//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 

//Writes the photo to the server 
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = $target . basename( $_FILES[$photo]['name']); 
    if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){

	//Tells you if its all ok 
	echo "Success."; 

	//Writes the information to the database 
	mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
} 
else { 

	//Gives and error if its not 
	echo "Sorry, there was a problem uploading your file."; 
} 
}
?>

Link to comment
Share on other sites

Thanks for that.

Sorry for being stupid(still learning php) but if my input fieds are named photo1, photo2, and photo3

how would i change the code ubove to suit my form?

Thanks for all your help.

<?php require_once('../Connections/p2w.php');?>
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>
<?php 
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }?>
  
  <?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("../news/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../news/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Link to comment
Share on other sites

Thanks for that.

Sorry for being stupid(still learning php) but if my input fieds are named photo1, photo2, and photo3

how would i change the code ubove to suit my form?

Thanks for all your help.

<?php require_once('../Connections/p2w.php');?>
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>
<?php 
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }?>
  
  <?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("../news/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../news/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

 

hehe we're all noobs :D.

Link to comment
Share on other sites

<?php require_once('../Connections/p2w.php');


//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 

//Writes the photo to the server 
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = $target . basename( $_FILES[$photo]['name']); 
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000)){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){

		//Tells you if its all ok 
		echo "Success."; 

		//Writes the information to the database 
		mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
	} 
}
else { 

	//Gives and error if its not 
	++$count;
} 
}
if ($count > 0){
echo $count .'failed to upload ';
}
?>

try

Link to comment
Share on other sites

changed the code to

<?php require_once('../Connections/p2w.php');


//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 

//Writes the photo to the server 
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = $target . basename( $_FILES[$photo]['name']); 
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")
&& ($_FILES[$photo]["size"] < 20000)){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){

		//Tells you if its all ok 
		echo "Success."; 

		//Writes the information to the database 
		mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
	} 
}
else { 

	//Gives and error if its not 
	++$count;
} 
}
if ($count > 0){
echo $count .'failed to upload ';
}
?>

Get the same echo

3failed to upload 

We'll get there  ;)

Link to comment
Share on other sites

i get this

Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) Array ( [photo1] => Array ( [name] => Lineup_220907_1024_200792511327.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC93F.tmp [error] => 0 [size] => 213446 ) [photo2] => Array ( [name] => megan_fox_02.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC94F.tmp [error] => 0 [size] => 65963 ) [photo3] => Array ( [name] => megan_fox_05.jpg [type] => image/pjpeg [tmp_name] => C:\Windows\TEMP\phpC950.tmp [error] => 0 [size] => 73388 ) ) 

Link to comment
Share on other sites

ok

for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = $target . basename( $_FILES[$photo]['name']); 
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){

		//Tells you if its all ok 
		echo "Success."; 

		//Writes the information to the database 
		mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
	} 
}
else { 

	//Gives and error if its not 
	++$count;
} 
}
if ($count > 0){
echo $count .'failed to upload ';
}
?>

Link to comment
Share on other sites

<?php require_once('../Connections/p2w.php');


//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 

//Writes the photo to the server 
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = $target . basename( $_FILES[$photo]['name']); 
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){

		//Tells you if its all ok 
		echo "Success."; 

		//Writes the information to the database 
		mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
	} 
}
else { 

	//Gives and error if its not 
	++$count;
} 
}
if ($count > 0){
echo $count .'failed to upload ';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.