Jump to content

only some images show up?????


Recommended Posts

Why is it that only certain images come through. I made sure all extensions were there but some jpegs come through others don't some pngs not others. Any thoughts?

 

<?php
$hostname='yobb';
$username='ybbb';
$password='Anbbbb!';
$dbname='b29';
$usertable='bbbb;


$myconn=mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');

if ((($_FILES["file"]["type"] =="image/gif")
|| ($_FILES["file"]["type"] =="image/jpeg")
|| ($_FILES["file"]["type"] =="image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"]< 200000))
{
if ($_FILES["file"]["error"] >0)
{
	echo "Return Code: " . $_FILES["file"]["error"] . "<br/>";
}
else
{
	if (file_exists("uploads/" . $_FILES["file"]["name"]))
	{
		echo "File already exists. Choose another name.";
	}
		else
		{
			move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
		}
}
}
else
{
echo "Invalid file";
}
$path="uploads/" . $_FILES["file"]["name"];
$name=$_POST["name"];
$state=$_POST["state"];
if (!myconn)
{
die ('Could not connect: ' . $mysql_error());
} 
$db_selected=mysql_select_db('your1729',$myconn);
if (!$db_selected) {
    die ('Can\'t use your1729 : ' . mysql_error());
} 
  mysql_query("INSERT INTO photo(name,photopath,state) VALUES ('$name','$path','$state')") or die(mysql_error()); 
  mysql_close($myconn);
?>

Link to comment
Share on other sites

$allowed = array("jpg", "jpeg", "gif", "png");

Try this., you may avoid JPEG issues

 

list($name, $ext) = explode(".", $_FILES['file']['name']);

if(in_array(strtolower(ext), $allowed))

{

 

                      // Your code here

 

}

Link to comment
Share on other sites

The code you have, which you either got directly or indirectly from w3schools, is just about worthless. When they added the combined [type]/ check, they added it before the error check logic and now it reports an Invalid file when upload errors occur and you never see any upload error messages.

 

A) You must always check that the upload worked before you attempt to use any of the uploaded file information. The [type] is empty and won't match anything when there is an upload error.

 

B) You should NOT lump different validation checks into one conditional statement. This hides information about why the test failed. That code will report Invalid file when either the type or size test fails. Wouldn't it be better to output a separate message telling the visitor that the type was not supported or that the file was too large?

 

C) As long as it is not security related, you should tell the visitor exactly why a validation test failed, including outputting the value that failed the validation test as part of the error message so that the visitor can see what the code was using that failed. If you output the actual upload [type] or information that failed a test, it will give the visitor more information about what were doing wrong (perhaps they accidentally selected a non-image file to upload) and it will give you debugging information when you are testing your code (you would be able to see the actual [type] that the browser sends.)

Link to comment
Share on other sites

Thanks guys. Really appreciate it.

 

One more question. I'm building a website where people can upload photos to a main page where you can see all the photos and how many comments they have each have. You click on a photo to review and it opens in a new page with a comment system. Each page has its own comment system. I've been putting this together with bits and pieces and I'll finish it but I want to know of any good books or references to this specific area. I like sample scripts but would rather learn exactly whats going on so I can expand things in the future and customize mysely.

 

Thanks Again.

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.